# Delta time movement calculation

This topic is 1074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I would need help;
I have game object which isn't moving in correct speed calculated from deltaTime bellow. It should reach half of the screen in one second, on any device.

Devices tested:

• tablet A7600-F - deltaTime = 0.03355443; (res. 720x1280)
• Xperia Z3 Compact - deltaTime = 0.13421772; (res. 720x1280)

Calculation:
calc = (screenWidth /2)* deltaTime. //I have app in landscape mode
speed = Math.round(calc);

- - - - -
Got:
speed on tablet - 21px per update;
speed on xperia - 86px per update;

You can see these results are very different (actualy it should be something about 8px - 20px). Game object is then extreme fast on Xperia (on tablet it's just a bit faster - speed is more than half of the screen per second).

What am I missing?

##### Share on other sites

Why are you rounding? Are the rounded values only used for display or are they fed back into the calculations?

##### Share on other sites

I'm using round to round it only for draw (since object can't pass 0.5 pixel). Then, next calculation is added to drawed object.X - that means to the rounded value.

##### Share on other sites

What you are doing is fine but what you are working out isn't speed. The speed is (width/2)/time (where time is 1 second) in other words 640. You then account for the delta time by multiplying and you get the same values as you have posted 21 and 86. The problem is you then consider those values 'speed' which they kinda are but a very odd form of speed, they are more accurately 'How far do I need to move the object this frame' - a distance rather than a speed.

It really depends on what you are doing after this step which is the important bit that we need to see. You should be doing something like:

calc = (screenWidth /2)* deltaTime.
speed = Math.round(calc);
object.x += speed; // except it's not really a speed it's a distance)

where x is the position.

?I don't know exactly how the object you have is setup (maybe it does take a speed which then gets updated via physics) but you could do something like this:

speed = (screenWidth/2); // this is in pixels/second and can be calculated elsewhere and stored
distance= speed*deltaTime; // This is how much you wish to add on to the object's position this frame
object.x += distance;

You can still round to the nearest pixel if you need to.

Edited by Nanoha

##### Share on other sites

The actual delta time should not impact the object speed at all. If the delta is higher, the game loop should be running slower and you should just get less updates per second. Check your framerate, it looks like it doesn't match your delta.

Edited by Ryder052

##### Share on other sites

If your rendering API requires it, rounding positions to draw is fine, but if those rounded values end up as part of the calculation that means that you are throwing some information away every frame, and that information will differ between machines, so you won't get a fixed result.

##### Share on other sites

Yes exactly this I'm doing. After calculation I have just object.X += speed.

- - -

speed = (screenWidth/2); // this is in pixels/second and can be calculated elsewhere and stored
distance= speed*deltaTime; // This is how much you wish to add on to the object's position this frame
object.x += displacement;

Sorry for misunderstanding, what do you mean with that displacement?

Note: I'm calculating this before player can play. Once he plays (or I allow him to), I stop any calculations and "speed" remains constant. So this result is just indicative (but I'm ok with that - that's reason why I'm doing this - to get average same speed, exact isn't necessary). Reason why this speed remains constant is, that I can't manipulate with it ingame (it would break my objects on scene).

##### Share on other sites

Note: I'm calculating this before player can play. Once he plays (or I allow him to), I stop any calculations and "speed" remains constant.

I don't understand. Delta time is not a constant, it is based on elapsed time? Perceived speed can appear constant, even if the actual distanced moved each frame varies to account for the elapsed time.

It is't clear what you mean by "break my objects on scene", but decoupling rendering from your game physics and fixing the time step should solve such problems.

##### Share on other sites
I don't understand. Delta time is not a constant, it is based on elapsed time?

That's my problem. If I calculate it in game when player plays, my object's positions getting messed up (but I don't know why). Only thing I'm doing to them is moving their position x += speed (or distance as Nanoha said), on each update. I don't know why it fails.

Edited by johnyX

##### Share on other sites

I'll try to put here key things of whole app.

Each update I'm updating game objects X position and then (in same update) drawing them on canvas. Position of objects are done as object.x += speed;

Speed is calculated in Mainthread, while running. Using

calc = (screenWidth /2)* deltaTime.
speed = Math.round(calc)

should I get distance (speed), that object have to travel for 1 second - every device updates in 1 second (but it evidently doesn't always).

Then,

- if I have distance calculated before game starts (I mean player play) and I don't change it, the game is ok (but distance sometimes not, as you can see on Xperia - 86px per update is extreme large.

- if I have distance calculated also durring game, it's breaking my objects on scene, making some moving faster than others.

Edited by johnyX

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 24
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633695
• Total Posts
3013373
×