Jump to content
  • Advertisement


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


Moving from A to B

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right, I have 2 points. I want point A to move toward point B at a set speed. Assuming this is done each frame, and if I have the x,y positions of the two points. As well as the x,y velocity of point A, how can I set up the velocity to move in that direction?

Share this post

Link to post
Share on other sites
first of all: don''t use frame rate dependant movement. if i run your thing on a 50 mhz 486 i''m going to wait me to death until your point arrives

use a vector to represent your movement:

float x, y;

vector(float x, float y)
this->x = x;
this->y = y;

void normalize(void)
float _l = 1.0f/sqrt(x*x + y*y);
x *= _l;
y *= _l;

then, *setup* the vectors orientation to point in the direction from point A to point B and have unit length:

vector movement ( PointB.x - PointA.x, PointB.y - PointA.y );

now, you can add the movement vector to Point A''s coordinates:
(set delta_t to be a constant value if you want to use frame rate dependant motion)

PointA.x += movement.x * movement_speed * delta_t;
PointA.y += movement.y * movement_speed * delta_t;

to change the velocity, change movement_speed.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!