• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
OmarShehata

Seperating game logic from game loop?

7 posts in this topic

Strange title, but I didn't know how else to word it.

Basically what I want to do is to have my game events time based instead of frame based. This is to ensure that, if I want something to move across the screen in 2 seconds, whether the game plays at 1000 fps or at 10 fps, it takes 10 seconds.

I've been thinking about this but I'm not too sure how I would structure my game and whatnot. I'm not asking about implementing this in a specific language, but just how I would go about creating my game like this?

Thanks!
0

Share this post


Link to post
Share on other sites
Typically the easiest (but not best) way to handle this is to compute the interval between frames and assign all your values in a fixed time unit (seconds or milliseconds usually). This requires getting an accurate system timer (QueryPerformanceCounter on Windows), storing the time between frames, and multiplying all your values by the elapsed time in order to get the correct numbers.
1

Share this post


Link to post
Share on other sites
a fairly simple method to get a fixed timestep is:
const float timeStep = 0.016667; //60 logic updates per second)
float currentTime = lastFrameTime = getTimeInSecondsAccuratlyFromTheOs();
[code]
while(!done) {
currentTime = getTimeAccuratlyFromTheOs();
while(currentTime > lastFrameTime + timeStep) {
update(timeStep);
lastFrameTime+=timeStep;
}
}
[/code]
this gives you a stable simulation(the results should be the same regardless of the actual framerate) and also allows you to change the size of the timestep if needed (you shouldn't change it at runtime though).

the update method should then update the gamestate based on the timestep you pass to it, i.e to move a unit you do position+=velocity*timeStep.

To get an even better result you can store 2 gamestates, one current and one old and have the update method read from the old state and write to the new state (and on the following update switch them around), this lets you interpolate between the states when rendering and also makes paralellization easier (multiple components/entities/whateveryou'reusing can read from the old state at the same time and write to their own part of the new state without any synchronization)
1

Share this post


Link to post
Share on other sites
This is a nice article related to your question, in case you haven't read it yet:
http://dewitters.koonsolo.com/gameloop.html
1

Share this post


Link to post
Share on other sites
[quote name='enunes' timestamp='1340675173' post='4952860']
This is a nice article related to your question, in case you haven't read it yet:
[url="http://dewitters.koonsolo.com/gameloop.html"]http://dewitters.koo...m/gameloop.html[/url]
[/quote]

Many thanks for all the replies! This article's explanation is perfect!
0

Share this post


Link to post
Share on other sites
[quote name='OmarShehata' timestamp='1340696753' post='4952935']
[quote name='enunes' timestamp='1340675173' post='4952860']
This is a nice article related to your question, in case you haven't read it yet:
[url="http://dewitters.koonsolo.com/gameloop.html"]http://dewitters.koo...m/gameloop.html[/url]
[/quote]

Many thanks for all the replies! This article's explanation is perfect!
[/quote]

Okay, follow up question:

So the whole rendering as fast as possible but keeping the actual game loop at constant speed seemed like a brilliant idea to me. It would go smoother on faster PC's and choppier, but still fast paced, on slow PC's, right?

There's just a teency problem with that.

On a slow PC, the game won't be able to render as fast, so since it's trying to render as fast as possible, the time taken for each frame fluctuates, even if ever so slightly, which causes the update in the game/physics step to look jumpy. So if one frame takes a few milliseconds longer, the character moves more than in the other frames and this is very noticeable.

How do other people deal with this problem?

I thought of giving the player the option of either choosing the FPS they want to run the game at, so higher FPS means the game itself will run slow, but smooth if they're computer can't handle it, but a slower FPS would mean the correct game speed but choppier movements. And also have an "auto fps" which simply goes down to the lowest fps. (So it doesn't go up and down depending on computer speed, just goes down to minimize fluctuations)

Comments/ideas/tips?
0

Share this post


Link to post
Share on other sites
Here are all the answers!

[url="http://gafferongames.com/game-physics/fix-your-timestep/"]http://gafferongames.com/game-physics/fix-your-timestep/[/url]
1

Share this post


Link to post
Share on other sites
[quote name='OmarShehata' timestamp='1341470724' post='4955865']
On a slow PC, the game won't be able to render as fast, so since it's trying to render as fast as possible, the time taken for each frame fluctuates, even if ever so slightly, which causes the update in the game/physics step to look jumpy. So if one frame takes a few milliseconds longer, the character moves more than in the other frames and this is very noticeable.

How do other people deal with this problem?
[/quote]

i mentioned this in my post earlier, store two (or three can be a good idea for multithreaded engines) copies of the game state, the old state and the "current" state , then interpolate between them based on the exact time when you render (Thus you will let rendering lag slightly behind instead). (so if logic frame 2 is at 605.02 and logic frame 3 is at 605.04 and you have frame1(from 605.0 and frame2 stored), the loop comes around, you grab the current time and it says 605.035 now rather than rendering frame 2 (The last logic frame) we interpolate between frame1 and frame2 (we're 75% of the way to the next logic frame (timestep 0.02 and we've done 0.015, 0.015/0.02 = 0.75)
Thus, for linear movement we just take the positions(as vectors) in frame 1 and scale them by 0.75 and add the positions from frame2(scaled by 0.25) and voila, we get smooth movement. (For rotations its probably easiest to slerp quaternions (you could just get a library for that).
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0