Slow motion with time steps problem

Hey guys!

Nice community over here aroun box2d. 🙂

I have tried to struggle on by using google, this forum and my own solutions to create a topdown view car game and now I faced problem that looks like too much for me.

I'm trying to apply slowmotion by changing my timeStep from 1 / 30 to 1 / 60, this I think is the right way to do slow motion in box2d. Em I right?

Then I keep updating my actor sprites positions every frame to keep the movement smooth, but I call my actor methods where all the box2d stuff happens every 2 frames, because I dropped my timestep to half. This is the part i'm not sure that em I doing it right. It's only how I figured out to do it... -> Call box2d related methods every second frame, because box2d functions twice slower than before.

Now here comes the weird issue.. When I drive in slow motion without steering, it runs perfectly the same way as in normal motion.. ok thats fine.. Now if I do turns, donuts etc. it doens't do it perfectly the same way with and w/o slow motion. It feels like theres a tiny power loss because of the slowmotion. The power loss is not large, but it's something I can't deal with.

Has any one some kind of explanation for this, could it be because of the way I call the car propelling methods on every second frame and if so how should I implement it then? 🙁

PS:
I move the car that has 4 wheels, by applying linear velocty to each wheel, so if it runs perfectly straight, I cant understand what makes the difference in turning.
Try to keep the timestep but change the timer it runs on to be called less often. Your simulation is changing because the math is being calculated twice as often - after a few seconds of calculations, the number errors are going to be large enough to make a noticeable difference.

In general, mucking with the timestep mid-simulation tends to produce unwanted results and inconsistent simulations. If you still have to keep it though, it may take some hacking to make it work - likely more trouble than it's worth.
patadox wrote:
Try to keep the timestep but change the timer it runs on to be called less often. Your simulation is changing because the math is being calculated twice as often - after a few seconds of calculations, the number errors are going to be large enough to make a noticeable difference.

In general, mucking with the timestep mid-simulation tends to produce unwanted results and inconsistent simulations. If you still have to keep it though, it may take some hacking to make it work - likely more trouble than it's worth.


Ok. Well it didin't look like it's changing at all when I don't do turns. I mean even one minute of driving and braking it didin't have a single pixel missmatch in anywhere. I run this test with my keystroke replay system and compared skid marks on the "road". They all matched perfectly 1/1 with not a single pixel error. I also tried to go with 1 / 120 time step, and same results. Thought, when I did any turning with the car the difference was even larger in smaller time step.

The problem should not be in my replay system either, because it can run over 10 minute sessions with no errors, and it does the same in slow motion when diving straight. I also tested that I set the wheel angle to max degree before applying throttle and the result is clearly different between slow and normal motion. Thats when I figured out, it has to be something with the physics and for sure nothing with the replays.

Well you have to be right with the fact that there occurs some mathematical differences when doing smaller (or larger) steps. but as I said, its weird the difference occurs straight away in such a large scale when I turn -> my replay goes to different roads after one turn.

Why I first even tried to change the time step was due to the smooth movement it provides when you keep updating display objects still with the max fps. If I just do the easy thing and drop fps, or call all the methods in slower framerate it doesnt look too good. The difference is huge between 30 and 15 fps. It looks more likely a fps drop (thats what it is 😆) than a slow motion. ^^

Thanks for the reply!
i didn't read the entire last post. but if you are unhappy with slower frame rate.. you could self calculate half steps between the box2d updates on the off frames. you may want to put your self about 3 frames behind to begin with so that you are doing half steps retroactively rather than predictively. just a thought.
sketchbookgames wrote:
i didn't read the entire last post. but if you are unhappy with slower frame rate.. you could self calculate half steps between the box2d updates on the off frames. you may want to put your self about 3 frames behind to begin with so that you are doing half steps retroactively rather than predictively. just a thought.


Hmm thats a good point that I was thinking about, but some more complex way. 😀 Didin't came up the idea to keep display objects animating X frames behind the simulation.

Got to give this one more thoughts when I have the time.


Thanks for the help, appreciate it. 😁
Given that your linear velocity (driving in a straight line) works fine, but your angular velocity (turning) breaks, it seems like the debugging won't be in Box2D, but in your calls to change the angular velocity of a body, and how they differ from linear.

When adjusting the angular velocity, do you have the time step interval length in your equation? That's the only thing I can think of that would produce this problem...

I still think you'll wind up with numerical errors though, especially the longer it runs. You may have to get your slo-motion working 'close enough', and then every couple of seconds nudge the game world back into place. Keyframes, essentially.
I think your mistake is that you are calling your functions every other frame. That is not always correct.

For example, if you are using ApplyForce, you are continually applying a force for the duration of the following frame. You should call it every frame, as otherwise you are not applying force for half of your frames, and it'll be half as effective.

In general, continuous things should be done every frame, regardless of framerate, and sudden things only once per event, regardless of framerate. See the documentation notes for the difference between ApplyForce (continuous) and ApplyImpulse (sudden).

Original thread: https://box2d.org/forum/viewtopic.php?t=4241