Public Group

#### Archived

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.

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

vector
{
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 );
movement.normalize();

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.

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

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633703
• Total Posts
3013455
×