I have seen around that it is better to have a "fixed time step" for the physics simulations, and in every example i have seen (at least here) they are not doing that. Is there any example around of how to change to a fixed time step?
Thanks!
Fixed time step
(9 posts) (3 voices)-
Posted 2 years ago #
-
like chipmunk spaceStep? sure... you just give it a constant.
Like for your spaceStep you could do something like this:
[self schedule:@selector(chipmunkStep:) interval:1.0/20.0]; .......... - (void)chipmunkStep:(ccTime)delta { cpSpaceStep(space, delta); }OR just do this (what I do)
#define kTimeStep 1.0/18.0 .............. [self schedule:@selector(chipmunkStep:)]; .............. - (void)chipmunkStep:(ccTime)delta { cpSpaceStep(space, kTimeStep); }The first method is probably better, as the space step will make up for lost time (FPS drops and such......)
I hope this is kinda right, the code above really helped me improve FPS random drops and performence.
~ Natanavra.
Posted 2 years ago # -
I'm somewhat confused by the fixed timestep matter too.
I'm doing a box2d game, and using this code:
int32 velocityIterations = 8; int32 positionIterations = 1; world->Step(dt, velocityIterations, positionIterations);which, judging by the comments in the example, is not a fixed timestep.
some people mention using an accumulator?running the game at 60fps and 30fps produces different results in the forces influencing the world.
I'm confused :(Posted 2 years ago # -
That's why i asked this, since in the box2d example there is this link: http://gafferongames.com/game-physics/fix-your-timestep/
but it looks way more complex that what natanavra posted above.I have been using with chipmunk the default step funcion (the one that comes in the examples) and one where i only do cpSpaceStep(space, 0.02f); and the result seems the same... (Why 0.02f? because it's the value that makes my game move as fast as if i left the initial configuration, pretty arbitrary i think...).
Posted 2 years ago # -
Accumulator usage is not advised... works like ths basically:
#define cpFloat delta 0.01 - (void)steppr:(ccTime)dt { accumulator += delta; while(accumulator > delta) { accumulator -= delta; cpSpaceStep(space, delta); } }You may get in accuracies with the simulation if using this...
Another option, (many say it's a good way) is this:- (void)steppr:(ccTime) delta { int steps = 1; cpFloat dt = 1.0/240.0/steps; for(int i = 0 ; i < steps ; i++) cpSpaceStep(space, dt); }Although I'm not sure why it should work better than the example I posted in the previous post...
Also, note that capping your game to 30 FPS is highly advised (tested myself) it's so much better than 60 FPS, no random slow downs no nothing, just a rock solid 30 FPS - it's amazing!Anything I posted is from my understanding, it is possible that I'm wrong (although it works for me) so don't flame me for being stupid with these posts (if at all)
~ Natanavra.
Posted 2 years ago # -
I saw a discussion like a month ago about capping the fps to 30, can you explain here the steps for doing that? since it wasn't clear to me how to do that back there.
If i am not mistaken i have to reduce director's animation interval to 1/30...
if i use your method:#define kTimeStep 1.0/18.0
..............
[self schedule:@selector(chipmunkStep:)];
..............
- (void)chipmunkStep:(ccTime)delta {
cpSpaceStep(space, kTimeStep);
}should kTimeStep remain the same anyway?
also, do i have to change anything else?
Posted 2 years ago # -
There is something i don't understand that i have been playing with...
Basically there are 3 variables that change the pace that the game runs:- The max framerate, given by the animation interval of the director. (Right now i have it a 1/30)
- The interval of the scheduled method that continually updates the animation
- In the cpSpaceStep(space,st); the st passed...Now... i know i should have the directors animation interval at 1/30 for sure.
If i play with the step interval i have different outcomes, passing no interval makes a choppy animation (the objects move in an horrible fashion, like with jumps)
if i set it to a high interval also the same result, know at 15-60 it gets better, but not as good as when i had 60 fps.If i pass different values to the cpSpaceStep function the objects just move like slower or faster.
My doubt is how to achieve a good relation between these to make a smooth fast animation...And why running the step function at a larger interval (1/15) makes the animation smoother than calling it more often like (1/120)
Posted 2 years ago # -
@pabloruiz55, sorry for the late answer, I just didn't notice the topic.
To answer you questions, I'll change what I said.
Cap your Director, don't cap schedules and don't use constants in your cpSpaceStep.
Sorry to be a bit confusing;[[Director sharedDirector] setAnimationInterval: 1.0/30.0];When schedule chipmunk step, don't cap.
[self schedule:@selector(stepper:)];And lastly, use your delta time (this will cancel chopiness):
- (void)stepper:(ccTime)delta { cpSpaceStep(space, delta); }Update me if it works better for you.
I currently don't have my mac at hand, but if this won't work for you - I'll copy code from my project and re-explain it.I hope this will help you, and I'm sorry for the confusion.
~ Natanavra.Posted 2 years ago # -
Thanks, no problem!
I tried it but i see almost the same results, one difference is that with what you posted above, if there is an huge slowdown (right now i have one if openfeint initializes while playing) the simulation keeps running, after the slowdown the player would be at another place... instead by using a fixed value, after the slowdown it is as if the game had paused for a little momment which is better for my game...As for the chopyness it remains, but maybe i am looking too much... at 30 fps i shouldn't expec the same smoothness i had at 60...
Posted 2 years ago #
Reply
You must log in to post.