Performance problem with Mobile and static meshes
Performance problem with Mobile and static meshes
Hi,
I seem to be seeing a massive performance drop to basically a freeze when I have both a mobile mesh and static mesh or convex hull mesh in the scene. This only happens when at least one mobile mesh is in my game level with one non-mobile mesh. If for example I make all the meshes which would usually be static, mobile ones. So all the meshes in the seen are mobile. There is no massive drop in performance, which leads me to think this is a bug as that surely behave worse. Is this a bug or am I missing something?
I seem to be seeing a massive performance drop to basically a freeze when I have both a mobile mesh and static mesh or convex hull mesh in the scene. This only happens when at least one mobile mesh is in my game level with one non-mobile mesh. If for example I make all the meshes which would usually be static, mobile ones. So all the meshes in the seen are mobile. There is no massive drop in performance, which leads me to think this is a bug as that surely behave worse. Is this a bug or am I missing something?
Re: Performance problem with Mobile and static meshes
-Are any of the mobile meshes in question dynamic, and more generally, are any of the objects capable of generating contacts with each other?
-If they are colliding, how complicated are the involved objects? How many contacts are being generated?
-Are there any NaN's happening somewhere? Propagating infinities and NaN's can kill performance if they don't crash things first. Exactly where these would come from is unknown, but they usually start external to the engine with velocity setting or something along those lines.
-Is internal time stepping being used (passing in an elapsed time to the Space.Update method)? This can cause big spikes in frame time if individual updates take consistently longer to compute than the time step duration. The Space.TimeStepSettings.TimeStepCountPerFrameMaximum specifies how many time steps can be taken in a single frame; it defaults to 3 in v0.16.0 and 10 in prior versions.
There's nothing particularly special about the static-mobile collision case compared to the mobile-mobile collision case. Differences in performance would likely be related to external factors like collisions being computed versus not being computed.
As always, a stripped down runnable reproduction case would help diagnose the issue if other approaches fail.
-If they are colliding, how complicated are the involved objects? How many contacts are being generated?
-Are there any NaN's happening somewhere? Propagating infinities and NaN's can kill performance if they don't crash things first. Exactly where these would come from is unknown, but they usually start external to the engine with velocity setting or something along those lines.
-Is internal time stepping being used (passing in an elapsed time to the Space.Update method)? This can cause big spikes in frame time if individual updates take consistently longer to compute than the time step duration. The Space.TimeStepSettings.TimeStepCountPerFrameMaximum specifies how many time steps can be taken in a single frame; it defaults to 3 in v0.16.0 and 10 in prior versions.
There's nothing particularly special about the static-mobile collision case compared to the mobile-mobile collision case. Differences in performance would likely be related to external factors like collisions being computed versus not being computed.
As always, a stripped down runnable reproduction case would help diagnose the issue if other approaches fail.
Re: Performance problem with Mobile and static meshes
I've done a bit more testing. It seems to get stuck updating the mobile mesh. However for testing purpose with changing the scenery to mobile meshes themselves(instead of static meshes and convex hulls) it seems to run fine.
- The mobile meshes are dynamic. They are able to generate contacts with static meshes. However they are not colliding yet because as soon as static/convex hulls objects are added to the scene. The entire game basically gets stuck on the mobile meshes update method.
- The object are not hugely complicated. Triangle wise they are both roughly under 1k triangles.
- As far as I can tell no.
- I am passing the elapsed time. I shall try changing the timestep variables.
I shall have more of a fiddle and if I can't fix it in my game. I'll try and make a reproducible version in a small project.
- The mobile meshes are dynamic. They are able to generate contacts with static meshes. However they are not colliding yet because as soon as static/convex hulls objects are added to the scene. The entire game basically gets stuck on the mobile meshes update method.
- The object are not hugely complicated. Triangle wise they are both roughly under 1k triangles.
- As far as I can tell no.
- I am passing the elapsed time. I shall try changing the timestep variables.
I shall have more of a fiddle and if I can't fix it in my game. I'll try and make a reproducible version in a small project.
Re: Performance problem with Mobile and static meshes
What is type of the kinematic object being added? Is it another mobile mesh? Is created in collision with the other objects, and if so, how complex is the collision (number of contacts)? If it's not another mesh, is it a convex hull? If so, how complex and how large is the convex hull?
If there are not any collisions happening at the time of the slowdown, that would be exceptionally strange.
If there are not any collisions happening at the time of the slowdown, that would be exceptionally strange.
Which update method, specifically?The entire game basically gets stuck on the mobile meshes update method.
Re: Performance problem with Mobile and static meshes
Edited my previous post a bit. As through testing I found a lot of my assumptions wrong. It might help to explain my scene a little bit so you can get a better idea of what's going on. Basically I have a very basic pirate ship model and some floating islands which are also fairly basic shapes. All of these are not affected by gravity so they can float. The ship is a dynamic Mobile Mesh and the island should be Static Meshes ideally as they will never move or be dynamic in anyway. However as soon as I add these as Static Meshes or Convex hulls the game basically gets stuck processing something inside the Update method for the Mobile Mesh internally within BEPU. When I add the Static Meshes(now down to button pressing for debugging) it basically freezes before they can even collide and cause contacts. Hopefully that's a bit clearer. Simply changing these islands to Mobile Meshes makes for no performance issue at all. In fact I can spawn 50 without a drop in frame rate. Which is very strange. Really how they should behave as Static Meshes. Most of the meshes involved are not very big in terms of bounding size. Maybe at max 100 units.
With a bit of testing it doesn't seem to matter if they are kinematic or dynamic(the islands) while convex hulls. They still cause the performance issue.
Edit: Very simple static meshes and convex hulls do not seem to cause this issue. As I have just be testing with a cube.
With a bit of testing it doesn't seem to matter if they are kinematic or dynamic(the islands) while convex hulls. They still cause the performance issue.
Edit: Very simple static meshes and convex hulls do not seem to cause this issue. As I have just be testing with a cube.
Re: Performance problem with Mobile and static meshes
I can't seem to reproduce the issue. Three static meshes partially overlapping, each with 20,000 triangles, along with a variety of boxes and multiple hundred-plus triangle dynamic MobileMeshes all in collision, performs normally. One of my theories, mesh-mesh continuous collision detection, also couldn't produce significant slowdowns (though when pushed it is relatively expensive for complicated models).
PositionUpdateModes, mobile mesh solidity settings, dynamic vs kinematic, debug vs release, and single vs multithreaded don't seem to affect anything in unexpected ways. I'll probably need a reproduction case to diagnose it.
PositionUpdateModes, mobile mesh solidity settings, dynamic vs kinematic, debug vs release, and single vs multithreaded don't seem to affect anything in unexpected ways. I'll probably need a reproduction case to diagnose it.
Re: Performance problem with Mobile and static meshes
I started creating a demo to recreate the bug. But before I even started adding additional static/convex meshes it seems to freeze with just one mobile mesh in the game. The project's linked below as it was too big to attach(5mb). I am not sure if I have set something up wrong. But I can't find a reason for it freezing right off the bat in my own code. The project is using the latest development built since yesterday. I'm not sure if this is the same bug, non related or just a mistake in pulling some of the classes from my game over. But it seems very similar just under a different circumstance. The mesh should rotate by itself if there's no freeze/performance drop.
http://www.mediafire.com/?ck28kvq8t50a9z3
Side Note: I don't think its the mesh as it works in my game as a mobile mesh.
http://www.mediafire.com/?ck28kvq8t50a9z3
Side Note: I don't think its the mesh as it works in my game as a mobile mesh.
Re: Performance problem with Mobile and static meshes
It appears that the bug in the reproduction case occurs when the object is dynamic and alone in the simulation. There was a bug in the rewritten dynamic hierarchy that would cause the object to collide with itself, obviously causing some significant slowdowns. The most recent development version should eliminate the problem (http://bepuphysics.codeplex.com/SourceC ... evelopment).
The original problem, however, is not addressed by this update- this specific bug only occurred when there was a single object in the broad phase that had collision-permitting collision rules, and nothing else.
The original problem, however, is not addressed by this update- this specific bug only occurred when there was a single object in the broad phase that had collision-permitting collision rules, and nothing else.
Re: Performance problem with Mobile and static meshes
Ah right, I downloaded the latest development version and it doesn't seem to fix the single object issue.
Re: Performance problem with Mobile and static meshes
Is it the correct changeset (ded2511222f1)? I copied it into the BEPU Bug project and it works as expected.
Re: Performance problem with Mobile and static meshes
Readded the reference and it seemed to fix it. I must of just referenced the wrong version.
Re: Performance problem with Mobile and static meshes
Right, so in the below linked test project. I have added one convex hull entity. It is in the middle of the screen and the mobile mesh is about 50 units to the right. With the convex hull entity in the scene the performance problem happens. Sorry for the messy classes as they are just taken from my game.
http://www.mediafire.com/?zk0g3pqxtc3biv9
http://www.mediafire.com/?zk0g3pqxtc3biv9
Re: Performance problem with Mobile and static meshes
It appears that the convex hull is 1400 units wide and so collides with every single triangle in the mesh. Additionally, convex hull collision test expense scales linearly with the vertex count in the convex hull. That hull has 168 vertices, so it's pretty heavy. Combine a heavy per-triangle test with 3968 triangle collisions and it takes a while 
The MeshEntity.GeneratePhysicsMesh overload does nothing with the transform it creates, so the vertices used to create the convex hull are larger than intended.
By the way, if you encounter 'normal' performance problems later, I would recommend checking to see if the cause is the mesh detail. In general, the simpler a collision mesh is (without making individual triangles so large that they cause numerical issues), the faster it will be.

The MeshEntity.GeneratePhysicsMesh overload does nothing with the transform it creates, so the vertices used to create the convex hull are larger than intended.
By the way, if you encounter 'normal' performance problems later, I would recommend checking to see if the cause is the mesh detail. In general, the simpler a collision mesh is (without making individual triangles so large that they cause numerical issues), the faster it will be.
Re: Performance problem with Mobile and static meshes
Great thanks a lot for the help. Seems like I forgot to apply the transform! The mesh is actually just for testing and is not intended to be used in game.
Edit: Just tried adding transforms and in particular for static meshes it doesn't seem to be working.
var mesh = new StaticMesh(vertices, indices);
mesh.WorldTransform = new AffineTransform(Scale, Quaternion.Identity, Position);
Should scale and position the StaticMesh to the correct place right?
Edit #2: Fixed!
Thanks a lot for your help.
Edit: Just tried adding transforms and in particular for static meshes it doesn't seem to be working.
var mesh = new StaticMesh(vertices, indices);
mesh.WorldTransform = new AffineTransform(Scale, Quaternion.Identity, Position);
Should scale and position the StaticMesh to the correct place right?
Edit #2: Fixed!
Thanks a lot for your help.
Re: Performance problem with Mobile and static meshes
By the way, giving the transform to the constructor will be a bit faster than using the property after the fact. Setting the property later requires some reprocessing on the acceleration structure.