# What is the point of “delta” in this code? Does it reflect a standard thing in game dev?

I know very little about game programming but would like to learn more. I am trying to understand the code for this game. I am trying to understand why the code is passing a "delta" to Shipcontrols.js, which changes the direction of the ship based on user input.

Basically the game calculates "delta" every loop...

Here is an abbreviated version of the stack that uses delta thru one loop...

var delta = now - this.time;
this.time = now;
this.current.render.call(this.current, delta, this.renderer);


Steps into here...

ctx.manager.add("game", scene, camera, function(delta, renderer)
{
if(delta > 25 && this.objects.lowFPS < 1000) this.objects.lowFPS++;
var dt = delta/16.6;
this.objects.components.shipControls.update(dt);


Steps into here...

bkcore.hexgl.ShipControls.prototype.update = function(dt)
{
var pitchAng;
var yaw;
var roll;
if (undefined != hand){


Which does stuff like this...

if(this.key.forward)
this.speed += this.thrust * dt;
else


and this...

if(this.key.right)
{
angularAmount -= this.angularSpeed * dt;


What is the point of delta here? Is it just trying to introduce an element of randomness? The code for this game is very good. Why did this guy use delta?

• "Is it just trying to introduce an element of randomness?" Actually the exact opposite is true, the purpose of the delta here is to reduce the randomness, and normalize physics in the game engine. – zzzzBov Nov 19 '13 at 15:18

This is the "time delta." It's how much time has elapsed since the previous update. It's necessary to ensure that animations, physics, and so on are running at the right speed.

The code is running once per frame update. However, there's no guarantee that frames are drawn at a constant speed. One frame might take 1/60th of a second and the next might take 1/30th. If you don't measure for and account for this, the game will be jittery and run either too fast or too slow in various circumstances.

Time deltas are often applied in physics as that's how the equations are specified for simple Euler integrations. Integrating velocity into position is defined as x1 = x0 + v * (t1 - t0) which simplifies into code like x += v * dt. Hence, time deltas are required to evaluate physics updates.

It is very, very normal for time deltas to be measured and applied.

• I think this is the best answer because it explains 'dt' can vary a lot, which is why we need to interpolate physics calculations with it. – BiAiB Nov 19 '13 at 16:56
• It may be worth noting that a major problem with time deltas in some contexts is that in many cases they will be "off" by a frame. If something causes one frame update to take twice as long as usual, the delta applied when computing that frame's actions will be the usual one, even though the frame won't be displayed until the time when objects should be shown having moved twice as far as they did. If the time between that frame and the next is smaller than usual (may occur if the timer tries to "catch up", the two frames with less time than usual between them will show... – supercat Nov 19 '13 at 20:46
• ...a larger-than-usual amount of distance between the object's positions). – supercat Nov 19 '13 at 20:46
• @supercat: That's just the usual Fix Your Timestep stuff. You still need per-frame time deltas to know when to step a fixed simulation, though. – Sean Middleditch Nov 19 '13 at 20:48
• @SeanMiddleditch: As someone who has coded games for the Atari 2600, I find it in some ways curious that nobody's lamented the fact that on modern systems there's an unavoidable lag between when the player moves a controller and when the character responds. On many Atari 2600 games, the control is polled every 16.7ms and the player updates occur within 1-16ms of that (depending upon vertical position); modern equipment is incapable of reacting that quickly. – supercat Nov 19 '13 at 20:56

"Delta", "d" or "Δ", means "difference" in a mathematical context. Whenever there's a difference difference between two numbers with similar meanings, that difference may be called a "delta", or a "d".

Deltas are very common in game development. For example, the difference between a character's X-coordinate one second ago and its X-coordinate now can be called "delta x", and is commonly denoted as dx, delta_x or d_x.

Also, it is very common to have the difference between two times, as you have in your code:

var delta = now - this.time;


In this case, that variable is denoting the difference between some time stored in this.time, and the time stored in now.

Deltas are commonly used to represent the change of something in time. So for example, if you know that a player's X coordinate should change 5 pixels every frame, you can store this change as a delta:

var delta_x = 5


And then use that delta to apply the change whenever you need it:

player.x = player.x + delta_x


But remember that this is only a convention. Nobody forces you to name your variables "delta" or "d", but doing so may help somebody else who reads your code, or yourself if you read it in the future, to understand what the variable is supposed to do.

Other common greek letters widely used in programming are:

Epsilon: for a very small value. Commonly used when comparing floating point, or other variables with precision problems:

const epsilon = 0.0001
...
if abs(some_float - other_float) < epsilon then
-- close enough, let's say they're equal
end


Pi: for the eponymous constant

Theta: for representing angles

Lambda: for representing anonymous functions or closures

• You'll also see certain multiples of π, like 2π, π/2, π/4, and e (Euler's constant). – jzx Nov 19 '13 at 8:12
• @Thomas: Sure, any program with mathematical basis will bring the theoretical notation into the code. Notice the sentence "Other common greek letters widely used in programming are". "some graphics code" hardly qualifies as "common" or "widely used". I never claimed these are the only greek letters that can be used, nor that those are the only meanings you can set for those greek letters. – Panda Pajama Nov 19 '13 at 8:34
• @yzx: Last time I checked, Euler's constant used the latin letter "e". I was not talking about mathematical constants that appear in code, but instead about greek letters commonly used in code. – Panda Pajama Nov 19 '13 at 8:35
• @PandaPajama I have deleted my comment as it is not welcome. – Thomas Nov 19 '13 at 8:43
• If your comrades are particularly hip, you might even see Tau (τ) instead of 2π. – Kaz Dragon Nov 19 '13 at 8:49

dt stands for delta time. It is used in the calculation of frame rate to insure the game runs at the same speed no matter what the frame rate is.

More information on framerate independence can be found here.

dt (delta time) is the time between each cycle/render frame (or any time stamp you desire) of your loop. With this delta time we can stretch certain values over time. Just like in the real world we measure certain physics properties over time.

Let's say we run our game 60 frames a second. If we want our our player to move 5 pixels per second we do

 5 * (1/60); //Assuming we have a stable delta time of 0.016
// (1/60) = 0.016 || 0.016 * 5 =  0.083 --> moved per frame || 60 frames * 0.083 = 5


or

 5 * (newTime - oldTime); //aka dt


The character moves 5 pixels over 60 frames. The longer your cycle takes the bigger delta time will get.

For every framerate (1/30, 1/25, etc) the result will be the same.