Speed, delta-time logic
Posted: Thu Mar 12, 2015 10:06 pm
Dear lovers. Two days I have tried to get a idea to pop up in my head how to solve this problem:
In my game the spaceship has two engines. Either engine produces 100 speed. Max speed of the ship can be 200. If I were to disable one motor so that the ship travels beyond max speed, I want it to slow down in exactly ten seconds. Say I travel at 180, and suddenly the ship is running on one motor and now it will slow down to 100 in 10 seconds. And then I disable the other engine and the max speed is now 0, and it will again in 10 seconds slow down to 0 to not exceed max speed.
Here is my code:
My approach doesn't work, basically I just want it to slow down to 100 or 0 in 10 seconds from whatever speed that exceeds the max speed.
Need some help with the math and logic.
Bindie
In my game the spaceship has two engines. Either engine produces 100 speed. Max speed of the ship can be 200. If I were to disable one motor so that the ship travels beyond max speed, I want it to slow down in exactly ten seconds. Say I travel at 180, and suddenly the ship is running on one motor and now it will slow down to 100 in 10 seconds. And then I disable the other engine and the max speed is now 0, and it will again in 10 seconds slow down to 0 to not exceed max speed.
Here is my code:
Code: Select all
if Ship.RightEngine.on == true and Ship.LeftEngine.on == true then
Ship.max_speed = Ship.RightEngine.speed + Ship.LeftEngine.speed
elseif Ship.RightEngine.on then
Ship.max_speed = 100
elseif Ship.LeftEngine.on then
Ship.max_speed = 100
else
Ship.max_speed = 0
end
if Ship.speed > Ship.max_speed then
if t[1] < 10 then
t[1] = t[1] + dt
else
t[1] = 0
end
-- Here I need something like:
Ship.speed = 200 - Ship.max_speed*(t[1]/10)
else
Ship.speed = Ship.max_speed
end
Need some help with the math and logic.
Bindie