Page 1 of 1
Smooth object stopping...
Posted: Thu May 01, 2014 10:21 pm
by Ser_Mitch
Hey everyone
Getting back into lÖve now that I'm studying, threw this together last night...
How do I stop the entity 'buzzing' around the destination point? Is there a way to make it come to a clean stop?
Thanks guys,
P.S Left click to select blue ball, right click to move blue ball
Re: Smooth object stopping...
Posted: Fri May 02, 2014 12:05 am
by micha
Hi,
here is what you can do. Put this into the player:update:
Code: Select all
local stepsize = self.s * dt
local deltax,deltay = self.dx - self.x, self.dy - self.y
local distance = math.sqrt(deltax^2+deltay^2)
if stepsize > distance then
self.x = self.dx
self.y = self.dy
self.state = 'stop'
else
self.xv = deltax/distance*self.s
self.yv = deltay/distance*self.s
self.x = self.x + (self.xv * dt)
self.y = self.y + (self.yv * dt)
self.state = 'move'
end
To prevent "overshooting" the target, I first calculate the stepsize, the player can make (speed times dt). If the distance from the target is smaller than that, assume the targets position and stop, otherwise, safely move towards the target.
(I also turned around the order of calculating the velocity and applying the velocity. The velocity should be calculated first and then applied. Otherwise, you use the velocity from the previous timestep)
Re: Smooth object stopping...
Posted: Fri May 02, 2014 1:02 am
by Ser_Mitch
Hey thanks,
I thought the issue would be something like this, I got that it was 'overshooting' the destination point, but my implementation was broken and all my attempts to fix it just made the overshoot bigger.
Thanks heaps
Re: Smooth object stopping...
Posted: Fri May 02, 2014 2:35 am
by micha
I am glad, I could help.
Keep in mind, that with floating point numbers, it is usually not enough to test for equality "=". In most cases, you would rather check for equality with a little tolerance, to account for round off errors.