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:


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.


3 Answers 3


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


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(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;
  • 1
    \$\begingroup\$ @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);. \$\endgroup\$ Commented Feb 6, 2012 at 9:34
  • \$\begingroup\$ @Fafole Check the edit, I added an example. Not sure if I missed something though. \$\endgroup\$ Commented Feb 6, 2012 at 9:48
  • \$\begingroup\$ One small thing to add is that you could use the squared distance to do the check in order to save an a square root op, which should help performance quite a bit. \$\endgroup\$ Commented Aug 19, 2023 at 8:32

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
    new_x_pos = x_goal 
    new_y_pos = y_goal

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

Now normalize the vector, and you'll receive this:

0.12; 0.75

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


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .