ElapsedTime Movement?

Started by
3 comments, last by Merlin Roger Avery 11 years, 6 months ago
Many years of programming and I never actually used a proper elapsed time functionality, which promises to run and move game objects in pixels per second rather than pixels per frames.

My question is where do you actually add elapsed time to? Is it every single object that you move? (which sounds like something you will eventually miss) or is it only on the main loop?

My example...?

[Engine] -> Game(delta) -> Player(delta) -> moveRight(delta) - > x += speed*delta;
[Engine] -> Game(delta) -> Enemy(delta) -> jump(delta) - > y -= jumpValue*delta;
[Engine] -> Game(delta) -> BackgroundManager(delta) -> moveBackgrounds(delta) - > for each(bg in bgList) bg.x -= speed*delta;

Am I thinking correctly or..?
Any interesting tutorials someone can link me to?

Start by doing what is necessary; then do what is possible; and suddenly you are doing the impossible.

Advertisement
Working with time instead of frames will give you a more synchronized game. it's also a must when you plan to go online.
Why? Assume you have 50 processes running on your computer and your enemy has 25. Your enemies game-cycle will be called twice as fast. So he will have the advantage.

You add the time to any calculation for movement, shooting, camera, etc.

m_Velocity = m_Velocity * deltaTime;
m_Position = m_Velocity; // you don't need to put a delta time here because it's using the velocity who has a delta time calculated in it.

// This is the camera you want to move:
m_Camera.moveLeft( int delta );
// Inside the method of the camera:
m_CameraPosition.x = m_CameraPosition * deltaTime;
// This means you need a method for the camera: void Tick( int deltaTime ) and store the deltaTime !!


Edit:
If you are not sure to put a delta time with a line of code you need to think on an online game.
Will the code without deltaTime have a possibility to lose synchronization? If yes, add the deltaTime.

I hope this was helpful.


~EngineProgrammer
Just do the same thing you normally do, however this time multiply the velocity by the elapsed time.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

I just schedule an event to go off every 80 milliseconds, and when it does, update the game logic. Whether or not that event is next in the event queue, I check for input and render the screen. Of course this is not the approach I'd use for an online twitch-based game, but I don't plan on ever writing one.
If you're handling all the entity updates in batch, you only need one delta for all of them. Create your delta and pass it through your updates. Depending on how you go about calculating your delta it can be a subtly costly operation that has little gain. It also mean all objects are synced to the same 'point' in time.

I do all these things in floatational numbers. So time becomes normalized to a second.

This topic is closed to new replies.

Advertisement