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
Smooth object stopping...
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Smooth object stopping...
- Attachments
-
- TEST.love
- (952 Bytes) Downloaded 99 times
Re: Smooth object stopping...
Hi,
here is what you can do. Put this into the player:update:
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)
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
(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)
Check out my blog on gamedev
Re: Smooth object stopping...
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
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...
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.
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.
Check out my blog on gamedev
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests