Smooth object stopping...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ser_Mitch
Prole
Posts: 12
Joined: Wed Oct 17, 2012 12:06 am

Smooth object stopping...

Post 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
Attachments
TEST.love
(952 Bytes) Downloaded 99 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Smooth object stopping...

Post 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)
User avatar
Ser_Mitch
Prole
Posts: 12
Joined: Wed Oct 17, 2012 12:06 am

Re: Smooth object stopping...

Post 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 :)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Smooth object stopping...

Post 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.
Post Reply

Who is online

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