Page 1 of 1

Unexplainable Framedrop [explainable now]

Posted: Mon Oct 08, 2018 6:08 pm
by Nitec57
Hi,

I use BEPUphysics v1 for my FPS-styled game and have a performance issue.

What happens:
I let drop a spaceship on my terrain (2048x2048 vertices). It has a dynamic box-entity. I initialize it with a mass, because I want it to get dropped from the sky. But I get an incredibly framedrop for several seconds at the moment, when the spaceship stops to move. When I move against the spaceship and push it with the character, the FPS drops again (0 - 10 fps until I stop running against the ship). The curios thing about this is, that when I scale my ship down, the framedrops do not occur anymore.

Why does the size of my entity affects the performance so much? Has it too many contact with the terrain-faces?
Here some code how I generate my terrain and the ship entity:

Terrain initialization:

Code: Select all

BEPUphysics.BroadPhaseEntries.Terrain terrainBepu = new Terrain(terrain.HeightData, new AffineTransform(new Vector3B(0, 0, 0)));
Spaceship initialization:

Code: Select all

spaceShipEntity = new Box(new BEPUutilities.Vector3(camera.position.X, 400, camera.position.Z + 100), boxCoords.X, boxCoords.Y, boxCoords.Z, 1);
I add both to the following space:

Code: Select all

            ParallelLooper looper = new ParallelLooper();
            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                looper.AddThread();
            }
            space = new Space(looper);
            space.ForceUpdater.Gravity = new BEPUutilities.Vector3(0, -9.81f, 0);
            Services.AddService(typeof(Space), space);
            BepuConfigurationHelper.ApplySuperSpeedySettings(space);
I update the space without parameters:

Code: Select all

  space.Update();

Has anyone an idea? Thanks for your help.


EDIT:
If I use the basic cube model (10x size) from blender it works fine. But in both cases I use just a box around my model? So the model should be irrelevant.

Re: Unexplainable Framedrop

Posted: Mon Oct 08, 2018 7:15 pm
by Norbo
It's hard to say without seeing a repro directly, but I would assume it's related to testing a huge number of triangles. For example, if the terrain is completely flat and has triangles 1 unit wide, dropping a 400x400 box on top of it would cause at least 400 * 400 * 2 = 320000 box-triangle tests. In v1, that takes about 50-60ms on a 3770k.

Might also want to check if you're running in debug mode or if any other systems are interfering with performance. If it appears to be something other than just testing hundreds of thousands of triangles, I'd recommend trying to reproduce it in the BEPUphysicsDemos so I can take a look directly.

Re: Unexplainable Framedrop

Posted: Tue Oct 09, 2018 8:23 am
by Nitec57
Thank you for your fast reply. I have tested it again with the big sized (30x30! not 400x400 :D ) cube-model yesterday night and had also performance issues. So it is independent from the model. (not like I claimed in my first post)

The demos run very smoothly on my pc (ryzen 1700) and they are far more complicated than dropping a big cube on my terrain.. When I am at home, I will change the size of the cube-entities in the demo.

Maybe it is worth to say that the cube drops on my terrain without any performance issues. The framerate drops only when I walk against the cube and push it around the map. In the case of my spaceship model the framerate drops even when it stops to move (maybe there is an issue when state changes happen?)
I will make a video at home and upload it on YouTube, maybe it is easier to understand for you. It's hard to explain such things when not being a native english speaker :oops:

One question: what would happen if a dynamic entity would go "inside" an another entity? For example if I would drop the cube so fast, that it gets inside my terrain to half. Would this cause a performance issue for the engine to "push" the box to outside again?

Re: Unexplainable Framedrop

Posted: Tue Oct 09, 2018 10:24 pm
by Norbo
what would happen if a dynamic entity would go "inside" an another entity? For example if I would drop the cube so fast, that it gets inside my terrain to half. Would this cause a performance issue for the engine to "push" the box to outside again?
The most significant change would be the tendency for more triangles to be tested during deeper collisions. Secondarily, deep penetration in v1 is handled by a slightly more expensive collision detection routine, but the greater number of tests would be more concerning.

Re: Unexplainable Framedrop

Posted: Wed Oct 10, 2018 3:14 pm
by Nitec57
I cant reproduce the issue in the demo. I have changed the size of the boxes in the "terrain demo" and it runs perfectly smooth.
I captured a video what you can watch here https://www.youtube.com/watch?v=v-pPRoG ... e=youtu.be
On the top-left you can see the current FPS. Maybe you have a guess what could possibly drop the FPS?

Re: Unexplainable Framedrop

Posted: Wed Oct 10, 2018 5:15 pm
by Nitec57
It would be amazing if you could take a look at my code! https://github.com/Sertan57/BepuPhysicsTest

Re: Unexplainable Framedrop

Posted: Wed Oct 10, 2018 10:26 pm
by Norbo
I didn't actually get it running (environmental setup issues), but it looks like it's still just a matter of tons of triangles being tested. In the test, the box is 100 units across and the terrain uses the default scaling of 1, so given the flat terrain, the box is going to be tested against at least 100 * 100 * 2 = 20000 triangles every frame. That's never going to fast, but it'll be really bad in debug mode or on slower processors.

Also, the reason that it tends to be much faster when not in flat contact is that collision detection takes a bunch of shortcuts. If it can exit the computation early using a cheap test, it will do so. Once the big box is in flat contact, though, those shortcuts can't be used anymore and the cost goes up a ton.

It slows down when the box is pushed because the character's movement wakes the box up. Sleeping objects don't require much computation, so it's very fast when idle.

I suspect the reason you didn't see it in the BEPUphysicsDemos is that 1) the terrain is not as dense relative to the size of the objects, and 2) the terrains are not completely flat. The density means there's less triangles to collide with, and the different heights mean that many triangles end up getting skipped completely.

I'd recommend using a less dense mesh, or using a non-heightmap mesh which includes only as much detail as is necessary to represent the shape. In other words, if you know there are large completely flat areas that will be tested against big objects, representing them with a handful of larger triangles will save a ton of computation.

Re: Unexplainable Framedrop

Posted: Thu Oct 11, 2018 3:33 am
by Nitec57
ahhhh.. I did not notice that the terrain in the demo uses a 8x8 spacing :shock:
I have wasted 3 days by searching why my game does not run as smooth as the demo :lol:

Thank you for your help and time Norbo! And for this awesome engine, I love it!