Travelling along a vector
Started by Jul 18 2001 12:22 AM
,
8 replies to this topic
#2 Members  Reputation: 2076
Posted 18 July 2001  12:46 AM
You must want to move "at" 56 degrees relative to some existing heading (ie, you want to turn/rotate 56 degrees first). Your direction is the unit vector:
float x_dir = cos( 56 * PI / 180 ); // must convert degrees to rad
float y_dir = sin( 56 * PI / 180 ); // ditto
To make your effective displacement (distance travelled) 20 units, multiply your unit direction vector (which has a length of 1) by 20:
x_dir *= 20;
y_dir *= 20;
Then just add x_dir and y_dir to your current position (25, 45). Wash, rinse, repeat.
P.S the C/C++ func for inverse tangent is atan() (or is it atan2()? I forget  and am too lazy to check MSDN).
float x_dir = cos( 56 * PI / 180 ); // must convert degrees to rad
float y_dir = sin( 56 * PI / 180 ); // ditto
To make your effective displacement (distance travelled) 20 units, multiply your unit direction vector (which has a length of 1) by 20:
x_dir *= 20;
y_dir *= 20;
Then just add x_dir and y_dir to your current position (25, 45). Wash, rinse, repeat.
P.S the C/C++ func for inverse tangent is atan() (or is it atan2()? I forget  and am too lazy to check MSDN).
#3 Members  Reputation: 1377
Posted 18 July 2001  03:53 AM
There are *two* functions for arctangent:
atan(x) gives the arctangent of x, result valued from pi/2 to pi/2 (first or fourth quadrants of the plane).
atan2(x,y) gives the arctangent of y/x. The benefit here is that with two values you can actually determine where you are in any of the four quadrants of the plane. Result here is valued from pi to pi.
Those are doubleprecision functions. I believe there are also floating point equivalents, atanf(x) and atan2f(x,y).
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
atan(x) gives the arctangent of x, result valued from pi/2 to pi/2 (first or fourth quadrants of the plane).
atan2(x,y) gives the arctangent of y/x. The benefit here is that with two values you can actually determine where you are in any of the four quadrants of the plane. Result here is valued from pi to pi.
Those are doubleprecision functions. I believe there are also floating point equivalents, atanf(x) and atan2f(x,y).
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
#5 Members  Reputation: 128
Posted 19 July 2001  12:27 AM
Thanks a heap guys, just one more question though, this doesn''t seem to work, maybe there is an error in my calculations, I''m only working within the first quadrant, and here''s the code, p is the player, pos is where he is, and moveto is set by the mouse cursor as the position he is to walk towards:
plen = sqrt(p.pos[0] + p.pos[1]); //Length of vector position
mlen = sqrt(p.moveto[0] + p.moveto[1]); //Length of Moveto vector
dot = (p.pos[0] * p.moveto[0]) + (p.pos[1] * p.moveto[1]); // Dot product of the two vectors
p.dir = acos((dot)/(plen * mlen)); //Dot product divided by the two unit vector lengths and arccosine''d gives the angle I need
x_dir = cos(p.dir * PI/180); // must convert degrees to rad
y_dir = sin(p.dir * PI/180); // and again
p.pos[0] += ((int)x_dir * p.speed);
p.pos[1] += ((int)y_dir * p.speed);
plen = sqrt(p.pos[0] + p.pos[1]); //Length of vector position
mlen = sqrt(p.moveto[0] + p.moveto[1]); //Length of Moveto vector
dot = (p.pos[0] * p.moveto[0]) + (p.pos[1] * p.moveto[1]); // Dot product of the two vectors
p.dir = acos((dot)/(plen * mlen)); //Dot product divided by the two unit vector lengths and arccosine''d gives the angle I need
x_dir = cos(p.dir * PI/180); // must convert degrees to rad
y_dir = sin(p.dir * PI/180); // and again
p.pos[0] += ((int)x_dir * p.speed);
p.pos[1] += ((int)y_dir * p.speed);
#6 Members  Reputation: 128
Posted 19 July 2001  12:27 AM
Thanks a heap guys, just one more question though, this doesn''t seem to work, maybe there is an error in my calculations, I''m only working within the first quadrant, and here''s the code, p is the player, pos is where he is, and moveto is set by the mouse cursor as the position he is to walk towards:
plen = sqrt(p.pos[0] + p.pos[1]); //Length of vector position
mlen = sqrt(p.moveto[0] + p.moveto[1]); //Length of Moveto vector
dot = (p.pos[0] * p.moveto[0]) + (p.pos[1] * p.moveto[1]); // Dot product of the two vectors
p.dir = acos((dot)/(plen * mlen)); //Dot product divided by the two unit vector lengths and arccosine''d gives the angle I need
x_dir = cos(p.dir * PI/180); // must convert degrees to rad
y_dir = sin(p.dir * PI/180); // and again
p.pos[0] += ((int)x_dir * p.speed);
p.pos[1] += ((int)y_dir * p.speed);
plen = sqrt(p.pos[0] + p.pos[1]); //Length of vector position
mlen = sqrt(p.moveto[0] + p.moveto[1]); //Length of Moveto vector
dot = (p.pos[0] * p.moveto[0]) + (p.pos[1] * p.moveto[1]); // Dot product of the two vectors
p.dir = acos((dot)/(plen * mlen)); //Dot product divided by the two unit vector lengths and arccosine''d gives the angle I need
x_dir = cos(p.dir * PI/180); // must convert degrees to rad
y_dir = sin(p.dir * PI/180); // and again
p.pos[0] += ((int)x_dir * p.speed);
p.pos[1] += ((int)y_dir * p.speed);
#7 Members  Reputation: 2076
Posted 19 July 2001  11:52 AM
quote:
Original post by shalrath
plen = sqrt(p.pos[0] + p.pos[1]); //Length of vector position
mlen = sqrt(p.moveto[0] + p.moveto[1]); //Length of Moveto vector
The length of a vector is the root of the sum of the squares of its coordinates. In other words, your equations should be:

You correctly calculate your direction, but things go awry after that. You need to loop while you increment p.pos or your player will only move once  a fraction of the distance you want to cover. Also, you can optimize away the trig operations because cos( acos( theta ) ) == theta and sin( theta ) == sqrt( 1  cos(theta )^{2} ). So:

**Edit: sin( acos(x) ). What was I thinking!
Edited by  Oluseyi on July 19, 2001 7:17:48 PM
#8 Members  Reputation: 128
Posted 20 July 2001  08:43 PM
My next problem, the character can only move in the xpositive, ypositive direction, no other directions, I think there may be something wrong with this code now... Maybe you guys could help again
float newx, newy;
if (p.moveto[0] != p.pos[0])
{
if (p.moveto[0] > p.pos[0])
{
newx = (x_dir * p.speed);
}
else if (p.moveto[0] < p.pos[0])
{
newx = (x_dir * p.speed);
}
}
if (p.moveto[1] != p.pos[1])
{
if (p.moveto[1] > p.pos[1])
{
newy = (y_dir * p.speed);
}
if (p.moveto[1] < p.pos[1])
{
newy = (y_dir * p.speed);
}
}
p.pos[0] += newx;
p.pos[1] += newy;
float newx, newy;
if (p.moveto[0] != p.pos[0])
{
if (p.moveto[0] > p.pos[0])
{
newx = (x_dir * p.speed);
}
else if (p.moveto[0] < p.pos[0])
{
newx = (x_dir * p.speed);
}
}
if (p.moveto[1] != p.pos[1])
{
if (p.moveto[1] > p.pos[1])
{
newy = (y_dir * p.speed);
}
if (p.moveto[1] < p.pos[1])
{
newy = (y_dir * p.speed);
}
}
p.pos[0] += newx;
p.pos[1] += newy;
#9 Members  Reputation: 546
Posted 22 July 2001  01:00 AM
If you know the start and end points of where you want to move, you don''t need to calculate the angle at all. Say you''re at p0 and you want to go to p1. Here''s the code:
I''ve expanded everything out, but if you''re using C++, you should be able to create overloaded operators for the Point class and turn it all into nice easytolookat math notation.
War Worlds  A 3D RealTime Strategy game in development.

I''ve expanded everything out, but if you''re using C++, you should be able to create overloaded operators for the Point class and turn it all into nice easytolookat math notation.
War Worlds  A 3D RealTime Strategy game in development.