# Moving from A(x,y) to B(x1,y1) with constant speed?

I currently have something like:

float deltaX = point0.getX() - point1.getX();
float deltaY = point0.getY() - point1.getY();


And every 0.01 seconds I refresh my objects position like this:

object.setPosition(object.getX()-deltaX/100,object.getY()-deltaY/100);


So this moves my object from point0 to point1 in 1 second. What I need is having the 2 points, to be able to move the object from point0 ,facing(in the direction of) point1 with a constant speed. Thus, when I have a point closer to my initial point the object will move towards it with the same speed it does if I would have a farther point. Any suggestions are appreciated. Thanks.

I'll use some linear algebra structures since it's easier to describe the operations that way. In case you don't know how to implement these vector operations I'll give a quick explanation at the end.

So let's say you start with these values: startand end mark the end points of the movement, speed is how many pixels it should move by second, and elapsed is the rate at which you'll update your object's position (some engines already provide that value for you):

Vector2 start = new Vector2(x1, y2);
Vector2 end = new Vector2(x2, y2);
float speed = 100;
float elapsed = 0.01f;


The first thing you'll want to calculate is the distance between both points, and a normalized vector containing the direction from start to end. Also, you should "snap" the object position to the start point. This step is done only once, at the beginning:

float distance = Vector2.Distance(start, end);
Vector2 direction = Vector2.Normalize(end - start);
object.Position = start;
moving = true;


Then on your update method, you move the object by adding a multiplication of direction, speed and elapsed to its position. After that, to check if the movement is over, you see if the distance between the start point and the object's current position is greater than the initial distance you calculated. If that's true, we snap the object's position to the end point, and stop moving the object:

if(moving == true)
{
object.Position += direction * speed * elapsed;
if(Vector2.Distance(start, object.Position) >= distance)
{
object.Position = end;
moving = false;
}
}


Quick Vector Operations Reference

Representation

Vector2 A = float aX, aY;


Sum / Subtract

A+B = a.x + b.x; a.y + b.y;
A-B = a.x - b.x; a.y - b.y;


Multiply by Scalar (float)

A*float = a.x*float; a.y*float;


Length / Distance

length(A) = sqrt(a.x*a.x + a.y*a.y)
distance(A,B) = length(B-A)


Normalize

normalize(A) = a.X/length(A); a.Y/length(A);


That should be enough to convert the above code into regular operations if you don't have a Vector class available to you.

Example of Conversion

// Your Variables
float startX, startY, endX, endY;
float speed = 100;
float elapsed = 0.01f;

// On starting movement
float distance = Math.sqrt(Math.pow(endX-startX,2)+Math.pow(endY-startY,2));
float directionX = (endX-startX) / distance;
float directionY = (endY-startY) / distance;
object.X = startX;
object.Y = startY;
moving = true;

// On update
if(moving == true)
{
object.X += directionX * speed * elapsed;
object.Y += directionY * speed * elapsed;
if(Math.sqrt(Math.pow(object.X-startX,2)+Math.pow(object.Y-startY,2)) >= distance)
{
object.X = endX;
object.Y = endY;
moving = false;
}
}

• @Fofole That's why I gave the explanation on vectors at the end. The answer was supposed to be generic. If you don't have a Vector class, then use two separate floats. For instance Vector2 start; becomes float startX, startY;. And you can easily calculate the distance manually, like I explain at the end. I.e. float dX = bX - aX; float dY = bY - aY; float distance = Math.sqrt(dx*dx+dy*dy);. Feb 6 '12 at 9:34
• @Fafole Check the edit, I added an example. Not sure if I missed something though. Feb 6 '12 at 9:48
• 3 years later and you just made me understand how to move objects with vectors. Cheers! Jun 24 '15 at 18:31

Copied and pasted from my answer to: Get points on a line between two points

In Pseudocode:

speed_per_tick = 0.05 //constant speed you want the object to move at
delta_x = x_goal - x_current
delta_y = y_goal - y_current
goal_dist = sqrt( (delta_x * delta_x) + (delta_y * delta_y) )
if (dist > speed_per_tick)
{
ratio = speed_per_tick / goal_dist
x_move = ratio * delta_x
y_move = ratio * delta_y
new_x_pos = x_move + x_current
new_y_pos = y_move + y_current
}
else
{
new_x_pos = x_goal
new_y_pos = y_goal
}

• This answer worked best for my use case. Sep 20 '19 at 1:51

Create a Vector and normalize it. Warning, some pseudo-code with wrong numbers ahead:

Vector = new Vector(point0.getX() - point1.getX(), point0.getY() - point1.getY());


This will give you a Vector like:

25.96; 85.41

0.12; 0.75


From here it's the same movement as with your delta.