#### Archived

This topic is now archived and is closed to further replies.

# Travelling along a vector

This topic is 6295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

let''s say I have a vector (2d) at ''25, 45'' and I want to move at an angle of 56 degree''s from here, 20 units each frame, how can I do this??? (also, does anyone know the C/C++ func for inverse tangent?)

##### Share on other sites
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).

##### Share on other sites
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 double-precision functions. I believe there are also floating point equivalents, atanf(x) and atan2f(x,y).

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
Thanks! (Still haven''t checked)

##### Share on other sites
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);

##### Share on other sites
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);

##### Share on other sites
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:
  plen = sqrt( p.pos[0] * p.pos[0] + p.pos[1] * p.pos[1] );mlen = sqrt( p.moveto[0] * p.moveto[0] + p.moveto[1] * p/moveto[1] );

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:
  x_dir = dot/(plen * mlen);y_dir = sqrt( 1 - (x_dir * x_dir ) );

**Edit: sin( acos(x) ). What was I thinking!

Edited by - Oluseyi on July 19, 2001 7:17:48 PM

##### Share on other sites
My next problem, the character can only move in the x-positive, y-positive 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;

##### Share on other sites
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:

  Point dir; float len; dir.x = p1.x - p0.x; dir.y = p1.y - p0.y;. // Normalize dir so it''s unit length. len = sqrtf( dir.x * dir.x + dir.y * dir.y ); dir.x /= len; dir.y /= len;. // move the point p0 to p1... while( p0.x != p1.x && p0.y != p1.y ) { p0.x += dir.x * speed; p0.y += dir.y * speed; // display the scene }

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 easy-to-look-at math notation.

War Worlds - A 3D Real-Time Strategy game in development.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009183
• ### Who's Online (See full list)

There are no registered users currently online

×