Raised This Month: $51 Target: $400
 12% 

Vector hater, always need help for those


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 01-27-2014 , 16:13   Vector hater, always need help for those
Reply With Quote #1

Here it is, the vector question of the day!

I got a position for example A (-1231.0, 123.0, 1337.0) and I want to make A move toward an other point B (-1231.0, 321.0, 7331.0) of X units.

so the question of the day is... well... you know!

Thx

Last edited by Mathias.; 01-27-2014 at 16:14.
Mathias. is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 01-27-2014 , 17:25   Re: Vector hater, always need help for those
Reply With Quote #2

If you're wanting X to be always between 0 and 1: F(X) = A + X(B-A)

For example, in three steps. 0, 0.5, 1:
F(0) = A
F(0.5) = A/2 + B/2
F(1) = B
__________________
11530 is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 01-27-2014 , 17:43   Re: Vector hater, always need help for those
Reply With Quote #3

But I need to make it move toward B for only X units...

Last edited by Mathias.; 01-27-2014 at 17:43.
Mathias. is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 01-27-2014 , 17:58   Re: Vector hater, always need help for those
Reply With Quote #4

For 2D (just modify for 3D..):

1) make vector v with just right length v(0,10) = length is 10 (points are 0,0 and 0,10)
2) rotate vector v (makes vector v2) (viz google: https://www.google.cz/search?=rotate...+around+origin)
3) move vector v2 to point A (or add vector v2 to vector origin -> A)
4) you have point A2 what is what you want

EDIT: vector rotation: http://www.siggraph.org/education/ma...ran/2drota.htm

Last edited by KissLick; 01-27-2014 at 17:59.
KissLick is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 01-27-2014 , 18:00   Re: Vector hater, always need help for those
Reply With Quote #5

Assuming you mean the mathematical definition of "unit", that is a distance of 1, then you can do the following where A = (a₁, a₂, a₃), B = (b₁, b₂, b₃) and C = (c₁, c₂, c₃) is partway along the line.


c₁ = a₁ + X(b₁ - a₁)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))
c₂ = a₂ + X(b₂ - a₂)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))
c₃ = a₃ + X(b₃ - a₃)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))


Just used my first answer, but now used a coefficient which has been divided by the length of A to B (the Pythagoras part of the equation) in order to get unit distances.
__________________

Last edited by 11530; 01-27-2014 at 18:22.
11530 is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 01-27-2014 , 18:21   Re: Vector hater, always need help for those
Reply With Quote #6

Quote:
Originally Posted by 11530 View Post
Assuming you mean the mathematical definition of "unit", that is a distance of 1, then you can do the following where A = (a₁, a₂, a₃), B = (b₁, b₂, b₃).

F(X) = (a₁, a₂, a₃) + X(b₁ - a₁ + b₂ - a₂ + b₃ - a₃)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))

I'll clean that up a bit. Final point on the line between A and B is C: (c₁, c₂, c₃)


c₁ = a₁ + X(b₁ - a₁)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))
c₂ = a₂ + X(b₂ - a₂)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))
c₁ = a₃ + X(b₃ - a₃)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))


Just used my first answer, but now used a coefficient which has been divided by the length of A to B (the Pythagoras part of the equation) in order to get unit distances.
Thanks, that what I needed, will test it out.
Mathias. is offline
blodia
Veteran Member
Join Date: Sep 2009
Location: UK
Old 01-27-2014 , 18:27   Re: Vector hater, always need help for those
Reply With Quote #7

use MakeVectorFromPoints on A and B, NormalizeVector the result then ScaleVector by X and add it to A
blodia is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 01-27-2014 , 20:03   Re: Vector hater, always need help for those
Reply With Quote #8

Let the code speak..
PHP Code:
stock MovePointTowardsOtherPoint(const Float:A[3], const Float:B[3], const Float:XFloat:result[3])
{
  
MakeVectorFromPoints(ABresult);
  
NormalizeVector(resultresult);
  
ScaleVector(resultX);
  
AddVectors(Aresultresult);

Edit: Oh, ^ what blodia said!
__________________

Last edited by Peace-Maker; 01-27-2014 at 20:04.
Peace-Maker is offline
GsiX
gee, six eggs
Join Date: Aug 2012
Location: Land Below The Wind
Old 01-28-2014 , 00:09   Re: Vector hater, always need help for those
Reply With Quote #9

Quote:
Originally Posted by Peace-Maker View Post
Let the code speak..
PHP Code:
stock MovePointTowardsOtherPoint(const Float:A[3], const Float:B[3], const Float:XFloat:result[3])
{
  
MakeVectorFromPoints(ABresult);
  
NormalizeVector(resultresult);
  
ScaleVector(resultX);
  
AddVectors(Aresultresult);

Edit: Oh, ^ what blodia said!
If i understand this, it should change your pos from A to B, yes?
__________________
If i happen to insulted you unintentionally,
it was me and Google Translate who did it.
GsiX is offline
Marcus_Brown001
AlliedModders Donor
Join Date: Nov 2012
Location: Illinois, United States
Old 01-28-2014 , 09:45   Re: Vector hater, always need help for those
Reply With Quote #10


c₁ = a₁ + X(b₁ - a₁)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))
c₂ = a₂ + X(b₂ - a₂)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))
c₃ = a₃ + X(b₃ - a₃)/(√((b₁ - a₁)² + (b₂ - a₂)² + (b₃ - a₃)²))


My brain hurts
Marcus_Brown001 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:57.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode