Moving an object from point A to B
Posted: Sat Jun 06, 2015 1:02 pm
Hi! I'm currently trying to get into game programming again and I'm having a bit of a problem at the moment. I'm trying to move a rectangle from point A to point B and it sort of works but the rectangle accelerate and deaccelerate instead of moving in a constant speed from A to B. Here's my code in its entirety:
Anyone know how to make the object move at a constant speed instead of accelerating/deaccelerate toward a position?
Thanks in advance!
Anyone know how to make the object move at a constant speed instead of accelerating/deaccelerate toward a position?
Thanks in advance!
Code: Select all
function love.load()
X = 30; -- initial X
Y = 30; -- initial Y
speed = 0.1;
end
function love.update( dt )
if (love.keyboard.isDown("right")) then
Xn = X + 128; -- destination X
Yn = Y + 45; -- destination Y
dX = Xn - X; -- deltaX
dY = Yn - Y; -- deltaY
distance = math.sqrt(dX^2 + dY^2); -- distance from A to B
moving = true;
end
if (moving == true) then
dX = Xn - X; -- update deltaX along path
dY = Yn - Y; -- update deltaY along path
X = X + dX * speed; -- update position
Y = Y + dY * speed; -- update position
if (math.sqrt(dX^2 + dY^2) > distance) then -- check whether object
X = Xn; -- has moved the entire
Y = Yn; -- distance
moving = false;
end
end
end
function love.draw()
love.graphics.setColor(0, 100, 100);
love.graphics.rectangle('fill', X, Y, 30, 30);
end