Jump to content

  • Log In with Google      Sign In   
  • Create Account

Travelling along a vector


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 shalrath   Members   -  Reputation: 128

Like
Likes
Like

Posted 18 July 2001 - 12:22 AM

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?)

Sponsor:

#2 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

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).

#3 grhodes_at_work   Moderators   -  Reputation: 1361

Like
Likes
Like

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 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.

#4 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 18 July 2001 - 09:47 AM

Thanks! (Still haven''t checked)

#5 shalrath   Members   -  Reputation: 128

Like
Likes
Like

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);

#6 shalrath   Members   -  Reputation: 128

Like
Likes
Like

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);

#7 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

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:
    
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

#8 shalrath   Members   -  Reputation: 128

Like
Likes
Like

Posted 20 July 2001 - 08:43 PM

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;

#9 Dean Harding   Members   -  Reputation: 546

Like
Likes
Like

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:

  
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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS