swiftmathgame-physicsvelocity

slow down object with easing out


I have object moving with speed 6.66px per frame refresh (400px/s). I know that target stop point is 2341px away. Frame refresh time is 0.01666667. I want it to start slowing down at some point to keep it smooth and then stop. How to calculate how many pixels away should I start slowing down my speed and by how much?

Here is the code with data I have:

func updateOffset(frameDuration: TimeInterval, speed: CGFloat, duration: TimeInterval, distanceToTarget: CGFloat) {
 self.currentOffset += speed
}

Solution

  • Found out correct algorithm, this will produce ease-out effect on object:

    var speed: CGFloat = 400.0
    
    func updateOffset() {
      speed *= 0.9
      currentOffset += speed
    }
    

    to calculate distance it will travel with that speed

    func distanceToSlowDown(initialSpeed: CGFloat) -> CGFloat {
      var distance = 0.0
      var speed = initialSpeed
      while speed > minimumSpeed {
        distance += speed
        speed *= 0.9
      }
      return distance
    }