## Moving an object from point A to B

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Frostfalk
Prole
Posts: 6
Joined: Tue Feb 10, 2009 9:42 am
Location: Sweden

### Moving an object from point A to B

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?

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
Solstickan
Jeeper
Party member
Posts: 588
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

### Re: Moving an object from point A to B

To change a variable at a constant speed is quite easy;

Code: Select all

function love.load()
changeSpeed = 100
variable = 0
end

function love.update(dt)
variable = variable + changeSpeed * dt
end

Frostfalk
Prole
Posts: 6
Joined: Tue Feb 10, 2009 9:42 am
Location: Sweden

### Re: Moving an object from point A to B

How do you use that method when moving a point from one place to another. in myexample, the distance from x1 to x2 is not the same as y1 to y2. I tried this:

Code: Select all

X = X + dX * speed * dt;
Y = Y + dY * speed * dt;
but I got the same result as before.
Solstickan
BlueWolf
Prole
Posts: 12
Joined: Fri May 29, 2015 7:55 pm

### Re: Moving an object from point A to B

That's because you compute dx and dy based on distance remaining. Thus your object is moving faster at the start and gradually slows down as it comes closer to the destination.
Frostfalk
Prole
Posts: 6
Joined: Tue Feb 10, 2009 9:42 am
Location: Sweden

### Re: Moving an object from point A to B

Oh yeah that's right, thanks for pointing that out Bluewolf.

I did a bit of extra research and found out that I need to normalize the vector before I can move the object along the line the way I want it. I rewrote it and now it works like I want it to:

Code: Select all

function love.load()
X = 250; -- initial X
Y = 250; -- initial Y

speed = 500;
moving = false;
end

function love.update(dt)
if (moving == false) then
if (love.keyboard.isDown("down")) then moveto(X, Y + 32, 100) end
if (love.keyboard.isDown("up")) then moveto(X, Y - 32, 100) end
if (love.keyboard.isDown("left")) then moveto(X - 32, Y, 100) end
if (love.keyboard.isDown("right")) then moveto(X + 32, Y, 100) end
end
if (moving == true) then
X = X + dirX * speed * dt;
Y = Y + dirY * speed * dt;
if ( math.sqrt((X-oldX)^2 + (Y-oldY)^2) >= distance) then
X = newX;
Y = newY;
moving = false;
end
end
end

function love.draw()
love.graphics.setColor(0, 100, 100);
love.graphics.rectangle('fill', X-15, Y-30, 30, 60);
end

function moveto(movex,movey,movespeed)
oldX = X;
oldY = Y;
newX = movex;
newY = movey;

dX = newX - oldX;
dY = newY - oldY;

distance = math.sqrt(dX^2 + dY^2);
dirX = dX / distance;
dirY = dY / distance;

speed = movespeed;

moving = true;
end
Solstickan

### Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 27 guests