The problem is that if I quickly spin around and thrust it keeps the speed from before. If you turn into the opposite direction and start thrusting I would like it to slow down before speeding up again. Kind of like real space. I’m still trying to wrap my head around delta time as well.
function init(self)
msg.post(".", "acquire_input_focus")
self.angle = 0
self.lastangle = 0
self.frame = 0
self.spindir = 0
self.spinspeed = 250
self.thrusting = false
self.speed = 0
self.acceleration = 4
self.deceleration = 2
self.maxspeed = 300
self.direction = vmath.vector3(0, 0, 0)
end
function final(self)
msg.post(".", "release_input_focus")
end
function update(self, dt)
self.angle = (self.angle + (self.spindir * self.spinspeed) * dt) % 360
self.frame = math.floor(self.angle / 360 * 32)
self.spindir = 0
if self.thrusting == true then
self.thrusting = false
go.set("#sprite", "cursor", (self.frame + 32) / 64)
if self.speed < self.maxspeed then
self.speed = self.speed + self.acceleration-- * dt ?
else
self.speed = self.maxspeed
end
self.lastangle = self.angle
else
go.set("#sprite", "cursor", self.frame / 64)
if self.speed > 0 then
self.speed = self.speed - self.deceleration-- * dt ?
else
self.speed = 0
end
end
self.direction = vmath.vector3(math.cos(math.rad(self.lastangle)), -math.sin(math.rad(self.lastangle)), 0)
local pos = go.get_position()
pos = pos + self.direction * self.speed * dt
go.set_position(pos)
end
function on_input(self, action_id, action)
if action_id == hash("left") then
self.spindir = -1
elseif action_id == hash("right") then
self.spindir = 1
end
if action_id == hash("up") then
self.thrusting = true
end
end
I appreciate any help!