Wednesday, August 24, 2016

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



Answer




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

}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...