Acceleration vs Exacto-movement

I’m struggling to write code which smoothly transitions speed from the current/or 0, and to the max, all the while holding a key to move. This provides me with a more fluid feel, instead of jagged, and immediate movement. I’ve read about using go.animate, but I’m confused as to how I implement and change my speed variable with it. As always, thank you for the help. You guys are awesome .

I don’t think go.animate will be very good for that since you want to be able to switch directions at any moment and interpolate it.

You can use vmath.lerp to interpolate manually:

-- t is a value between 0 and 1.
-- If t is 0 then value is startvalue.
-- If t is 1 then value is endvalue.
-- Anything in between is linearly interpolated.
local value = vmath.lerp(t, startvalue, endvalue)

You can use this function to create smooth acceleration, for instance.

1 Like

I would use acceleration and velocity. Just keep track of your velocity and clamp its magnitude to your desired max speed.

Something like this.

local acceleration = 0.01
local max_speed = 50
function update(self, dt)
    self.speed = self.speed + acceleration
    if self.speed > max_speed then
        self.speed = max_speed
    end
end
3 Likes

Yes. Also, if you multiply by dt the movement will be constant even if the FPS drops. And the measurements are easier. Pixels/s etc.

4 Likes