I’ve been following the tutorial-movement
, the one with the spaceship, and I decided to make the spaceship gradually slow down when move/accelerate key is released. I got that part working ok for left, right, up and down but the problem comes when moving on a diagonal. Initially it is ok and moves in a straight line but when switching back to non-diagonal it starts moving on a curve as if something isn’t being reset correctly; but I can’t figure out what. I’m sure it’s something obvious but I’ve been banging my head against a wall over this for days now
Thanks in advance!
local function accelerate(self, dt)
local acceleration = self.input * self.max_speed
local dv = acceleration * dt
local v0 = self.velocity
local v1 = self.velocity + dv
local movement = (v0 + v1) * dt * 0.5
local p = go.get_position()
go.set_position(p + movement)
self.velocity = v1
self.input = vmath.vector3()
end
local function brake(self, dt)
local acceleration = self.braking_direction * self.max_speed
local dv = acceleration * dt
local v0 = self.velocity
local v1 = self.velocity + dv
-- this isn't really speed but the result of braking_direction and velocity being in the exact opposite direction
local speed = vmath.dot(self.braking_direction, self.velocity)
if speed >=0 then
pprint("stopping")
self.braking_direction = vmath.vector3()
self.velocity = vmath.vector3()
self.moving = false
return
end
local movement = (v0 + v1) * dt * 0.7
local p = go.get_position()
go.set_position(p + movement)
self.velocity = v1
self.input = vmath.vector3()
end
function update(self, dt)
if vmath.length_sqr(self.input) > 1 then
self.input = vmath.normalize(self.input)
end
if self.moving then
if self.accelerating then
accelerate(self, dt)
else
brake(self, dt)
end
end
self.input = vmath.vector3()
end
function on_input(self, action_id, action)
if action_id == hash("up") then
if action.released then
self.accelerating = false
else
self.input.y = 1
self.braking_direction.y = -1
self.accelerating = true
self.moving = true
end