Page 1 of 1

Speed, delta-time logic

Posted: Thu Mar 12, 2015 10:06 pm
by Bindie
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:

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
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

Re: Speed, delta-time logic

Posted: Thu Mar 12, 2015 10:46 pm
by Kingdaro
Sounds like a job for tweens! To make a ship's speed go to a certain amount of speed in a certain amount of time, with this library, it'll be something like this:

Code: Select all

flux.to(Ship, 10, { speed = 100 }) -- or whatever speed you want to go to

Re: Speed, delta-time logic

Posted: Fri Mar 13, 2015 3:51 am
by Tesselode
Kingdaro wrote:Sounds like a job for tweens! To make a ship's speed go to a certain amount of speed in a certain amount of time, with this library, it'll be something like this:

Code: Select all

flux.to(Ship, 10, { speed = 100 }) -- or whatever speed you want to go to
Ehhh, I don't know about tweens. The problem with tweens is if the player adds any more input you have to cancel the current tween and come up with a new one. I think tweens are more suited to cosmetic things, like animations, not gameplay.

Re: Speed, delta-time logic

Posted: Fri Mar 13, 2015 6:46 am
by micha
Bindie wrote:I want it to slow down in exactly ten seconds.
You can achieve this in the following way: In the moment when one engine is shut down, compare the current velocity and the target velocity (the one without the engine). Then divide the difference by 10 seconds and you get the acceleration (better: deceleration) that you need to apply.

But from a physical point of view, this approach does not make sense: Going from speed 200 to 100 should take more time than going from speed 150 to 100. So I suggest you choose a fixed acceleration and let the ship take a different amount of time, depending on the actual speed-difference.

Re: Speed, delta-time logic

Posted: Fri Mar 13, 2015 7:46 am
by ivan
I start with the assumption that acceleration is constant so when the player presses a movement button the ship should accelerate as follows:

Code: Select all

velocity = velocity + acceleration*dt
position = position + velocity*dt
As Micha pointed out, deceleration works the same way, just solve for "acceleration":

Code: Select all

acceleration = (final_velocity - initial_velocity)/acceleration_time
-- example: final_velocity = 0, initial_velocity = 200, acceleration_time = 10 means decelerate from 200 to 0 in 10 seconds
velocity = velocity + acceleration*dt
It depends on the effect you are going for.
If your acceleration is constant, the ship will slow down in a linear fashion.

If you are going for a "friction" effect, then you can use damping (which is exponential):

Code: Select all

-- damping
damping = 0.5 -- coefficient between 0 to infinity, but never less than 0
velocity = velocity / (1 + damping*dt)
-- acceleration
velocity = velocity + acceleration*dt
Basically, apply the damping each step and when the acceleration is 0, the ship will slow down on its own.

More or less, here is how the damping coefficient affects velocity:

Code: Select all

initial velocity, damping coefficient = resulting velocity after 1 second
10, 10 = 0.909...
10, 5 = 1.666...
10, 4 = 2
10, 3 = 2.5
10, 2 = 3.333...
10, 1 = 5
10, 0.75 = 5.7142...
10, 0.5 = 6.666...
10, 0.25 = 8
10, 0 = 10

Re: Speed, delta-time logic

Posted: Fri Mar 13, 2015 8:37 am
by Bindie
Acceleration, lol. Of course.

Thanks.