Page 1 of 1

Huge lag when using the Physics System

Posted: Sat Jun 23, 2012 5:28 am
by Arthur Gibraltar
Hi, I'm a newbie on programming and creating games, started testing everything a few days ago :oops: . I'm using C# and XNA, and implemented the BePu physics (It was very difficult to discover how to and I didin't finish reading all the demos yet, but it's very organized and this physics system is helping me a lot, either to understand how to create a game and to learn more about programming, 'cause I don't know too much yet). It was very difficult to create this forum account because the questions were kind of complicated and I took many wrong guesses....
Anyway, it was everything OK until now: I'm creating a new game inside (with implemented) BePu physics (".dll" files) and I need to create many and many BePu Box objects in Kinematic state, for creating the map, that I want it to be editable inside game. So the problem is that, after creating just a few box (about 60), the FPS gets a very low rate. I don't know how much is the rate because I didin't create any FPS meter but I get a really huge lag when controlling my testing cube-character through the map. If I disable the physics, everything gets back to normal. And I saw some topics here: <http://bepu.nfshost.com/forum/viewtopic.php?f=4&t=1704> and <http://bepu.nfshost.com/forum/viewtopic.php?f=4&t=1614>, but I tried to use those orientations and I got no result. I don't know if that was because I don't know how to apply them correctly or if it's not the case. The thing is that I don't know too much about changing the time step (is there any code example?), but I did apply the "SuperSpeed" configuration but without successful results. I tried to create "space.TimeStepSettings.TimeStepDuration += 1 / 320" still with lag and "space.TimeStepSettings.TimeStepDuration += 1 / int.MaxValue - 1" with my objects teletransporting and disappearing :shock: , and I programmed this setting at every game's "Update(GameTime gameTime)" call. I don't remember now, but i tried that StepDuration value with one that I got my physics stucked, not working. I guess it was "+= 2". How should I really configure that correctly? :?:
So, is there any suggestion for what I could do? :?: I was creating the game until today and I'll pause a little until find for this problem a solution, otherwise my game won't run if only with 60 boxes it already slows down. Thanks for any reply. :mrgreen:

Cordially, Arthur. :D

PS: there are just kinematic objects now, and my testing cube-character is the only one that is dynamic.

Re: Huge lag when using the Physics System

Posted: Sat Jun 23, 2012 3:38 pm
by Norbo
Space.TimeStepSettings.TimeStepDuration is how many time units pass in a single time step. It should be a small value. The Space.TimeStepSettings.TimeStepDuration defaults to 1/60f. Don't change it at that value unless there's a good reason to change it.
The thing is that I don't know too much about changing the time step (is there any code example?)
It's a settable property:

Code: Select all

Space.TimeStepSettings.TimeStepDuration = value;
I tried to create "space.TimeStepSettings.TimeStepDuration += 1 / 320" still with lag and "space.TimeStepSettings.TimeStepDuration += 1 / int.MaxValue - 1" with my objects teletransporting and disappearing
The value defaults to 1/60f, so adding 1/320f to it won't really do much. The resulting value is around 1/50.5f.

Adding "1 / int.MaxValue - 1" results in a value of -59/60f. Negative values are invalid; the engine cannot simulate backwards like that.
and I programmed this setting at every game's "Update(GameTime gameTime)" call. I don't remember now, but i tried that StepDuration value with one that I got my physics stucked, not working. I guess it was "+= 2". How should I really configure that correctly?
The TimeStepDuration should not be continually changed. It can be set once and left alone. Attempting to add 2 every frame will result in rapidly increasing time step durations. A time step duration of 2 is way too long for any simulation, let alone 4, 6, 8, ...

But again, unless you know there is a reason to adjust it, don't change the time step duration away from its default of 1/60f.
So, is there any suggestion for what I could do? I was creating the game until today and I'll pause a little until find for this problem a solution, otherwise my game won't run if only with 60 boxes it already slows down. Thanks for any reply.
60 kinematic objects is an extremely small simulation, even for a low-end phone. It should not be slowing down. There is likely something else going on. If you can replicate the simulation and slowdown in a BEPUphysicsDemos demo, I could give you more specific recommendations.

But here's some suggestions:
1) If Space.Update(dt) is called to advance the game simulation, keep in mind that it can do multiple time steps in one frame. This is called internal time stepping; it helps make the physics simulation independent of frame rate. Space.Update() without a parameter only performs one time step.

A common problem is Space.Update(dt) being called with elapsed milliseconds as the parameter. Almost always, the intended value is elapsed seconds, which is a thousand times smaller. By passing in elapsed milliseconds rather than elapsed seconds, the simulation will attempt to simulate more time steps. There's a configurable per-frame time step maximum that stops it from doing the full requested thousand updates per frame, but it will still make the simulation take a lot of time.

It is unlikely that this fully explains the slowdown unless you are testing on a very low end device (1ghz single core phone or worse).

2) Some NaN's or infinities may be sneaking into the simulation. Depending on the form of corruption, these will not necessarily crash everything, but they will still massively slow down the simulation. Make sure all the inputs to the engine are valid.

3) Grab a profiler or implement your own timing code and measure where the time is being spent.

Re: Huge lag when using the Physics System

Posted: Sat Jun 23, 2012 5:08 pm
by Arthur Gibraltar
About the Time Step Duration and Settings, thank you! I understando a little better now. I'm starting to come out of darkness into programming.
So I'll try your suggestions (thanks a lot for them), but like I'm newbie on programming, it may take several time to apply them. And I'm gonna reread all my programming and find parts that could generate the slowdown within your suggestions too (like the NaN or the infinities).

And I'm testing on a normal PC (2.80 GHz, I guess), debugging on Visual Studio C# (the application takes 25% of my CPU) :shock: .

Just a doubt appeared about:
Norbo wrote:If you can replicate the simulation and slowdown in a BEPUphysicsDemos demo, I could give you more specific recommendations.
Then I should copy a Demo that most looks like my program and recreate the problem? I'll try it too.

As soon as I make the modifications and create the tests, I'll post the results here, but it may take much time, 'cause I'm still learning :P . Thank you, thank you very much for the patience!! :)

Re: Huge lag when using the Physics System

Posted: Sat Jun 23, 2012 5:10 pm
by Norbo
Then I should copy a Demo that most looks like my program and recreate the problem? I'll try it too.
Yup. You can just modify an existing one so that you can run it without having to fiddle with the DemosGame listing of demo types if you'd like.