Fixed Timestep

This is the official forum for Learning Cocos2D: A Hands-On Guide to Building iPhone and iPad Games with Cocos2D, Box2D, and Chipmunk by Rod Strougo and Ray Wenderlich, published by Addison-Wesley.

Fixed Timestep

Postby BeeZee » Wed Mar 14, 2012 7:42 pm

Hi Ray,

You mention in your book that there's a better way of handling the timestep available online at:

http://gafferongames.com/game-physics/f ... -timestep/

After poring over the link for a while, I can't figure out how to actually integrate it with Space Viking in place of the existing update method. Would you be able to show us how? It would be a fantastic learning opportunity. Thanks as always for your time!
BeeZee
Hacker
 
Posts: 11
Joined: Thu Mar 08, 2012 8:27 pm
Has thanked: 0 time
Been thanked: 1 time

Re: Fixed Timestep

Postby larry » Sun Mar 18, 2012 12:43 am

@BeeZee,
Have you compared the "before" and "after" of the Update routine? As they mention a lot of the strange behavior was fixed.

As I understand the routine, it's a matter of how you slice up time. How many intervals per fraction of time? The article would be updating the code from 12.25. But, I was pretty satisfied with 12.25 as I worked through the book.
larry
Uber Haxx0r
 
Posts: 380
Joined: Sun Oct 09, 2011 6:11 am
Has thanked: 3 times
Been thanked: 43 times

Re: Fixed Timestep

Postby n_dubbs » Mon Mar 19, 2012 1:45 pm

@larry is correct by specifying the time in which your update method is called, it will fix a lot of issues. The article just highlights the need to tweak how often your update method is called.
User avatar
n_dubbs
Team Member
iOS Tutorial Team Member
 
Posts: 893
Joined: Fri Feb 18, 2011 7:17 pm
Location: Pittsburgh, PA
Has thanked: 0 time
Been thanked: 36 times

Re: Fixed Timestep

Postby BeeZee » Tue Mar 20, 2012 5:28 pm

Thanks for the responses! I'm still very confused on how to implement a solution like what the linked article is proposing, though. The fixed time step provided in the book is below:

Code: Select all
static double UPDATE_INTERVAL = 1.0f/60.0f;
static double MAX_CYCLES_PER_FRAME = 5;
static double timeAccumulator = 0;

timeAccumulator += dt;
if (timeAccumulator > (MAX_CYCLES_PER_FRAME * UPDATE_INTERVAL)) {
    timeAccumulator = UPDATE_INTERVAL;
}
int32 velocityIterations = 3;
int32 positionIterations = 2;
while (timeAccumulator >= UPDATE_INTERVAL) {
    timeAccumulator -= UPDATE_INTERVAL;
    world->Step(UPDATE_INTERVAL,
        velocityIterations, positionIterations);
}


This seems easy enough to use. Right afterwards, however, it suggests:

There is one further optimization you can do to your physics loop: interpolate the results to remove the effects of that tiny extra slice of time in each frame. For more information on how to do this, check out http://gafferongames.com/game-physics/f ... -timestep/ or search the Cocos2D forums for a good discussion.


The linked article seems like a really interesting thing to explore, but its implementation is a lot more theoretical and is pretty far above the level of understanding I'm at after reading the book. There seem to be a lot of these discussions online, but all the ones I've been finding apply to different languages. Since the book makes mention of it, I think it would be awesome to dive in further and try to get it working with Cocos2D on iOS.
BeeZee
Hacker
 
Posts: 11
Joined: Thu Mar 08, 2012 8:27 pm
Has thanked: 0 time
Been thanked: 1 time

Re: Fixed Timestep

Postby BeeZee » Tue Mar 20, 2012 11:20 pm

I found a really good implementation of this here, if anyone's interested:

http://www.cocos2d-iphone.org/forum/topic/8922

It wasn't too hard to get this running in place of the existing code. I'd be happy to help if anyone needs assistance figuring out how to use it.
BeeZee
Hacker
 
Posts: 11
Joined: Thu Mar 08, 2012 8:27 pm
Has thanked: 0 time
Been thanked: 1 time

Re: Fixed Timestep

Postby n_dubbs » Mon Mar 26, 2012 12:36 pm

@BeeZee thanks for sharing! :D
User avatar
n_dubbs
Team Member
iOS Tutorial Team Member
 
Posts: 893
Joined: Fri Feb 18, 2011 7:17 pm
Location: Pittsburgh, PA
Has thanked: 0 time
Been thanked: 36 times


Return to Learning Cocos2D Book Official Forum

Who is online

Users browsing this forum: No registered users and 2 guests