[SOLVED] Collision Shape Performance

Hello Guys,
I’m trying to create a simple car racing game with about 50 concurrent moving cars on the track. The cars are low-poly with RigidBodyControl (needed for collision detection)
Problem is that performance drops dramatically with this amount of cars even with less then that.
Here is the collision shape code:

modelShape = CollisionShapeFactory.createDynamicMeshShape(parentNode);
RigidBodyControl modelCtl = new RigidBodyControl(modelShape, mass); 

We are talking about kinematic objects
This is how the car collision shape looks like:

What should I do to improve performance? I’m thinking maybe simplify the collision shape…

Thanks!

1 Like

Yeah.

…but it should be an easy thing to test to see if that’s it. Just use a box shape to test. If things go fast then you know you need a better collision shape (compound shape or whatever).

If things are still slow then you can look at something else.

…because I’m also wondering if there is some overhead for all of those kinematic objects instead of regular rigid bodies.

1 Like

I’m not sure I understand - I thought that having the RigidBodyControl with mass==1 & setKinematic(true) is the “regular” state with best performance while still enabling collision detection isn’t it?
Otherwise, what do you mean by regular rigid bodies?
Also looking at the documentation (Physics: Gravity, Collisions, Forces :: jMonkeyEngine Docs) it seems like the right option for these kind of cars… WDYT?

I don’t know how kinematic bodies are implemented in bullet exactly but they are a special beast because you are essentially warping them everywhere all the time. So it’s worth testing to see if that messes up optimizations.

It’s telling that kinematic objects can have more complicated collision shapes than regular “physics affects them” rigid bodies. I can imagine a type of implementation where warping a rigid body around invalidates all caches or whatever.

I’m not saying it is the case but it could be the case.

By regular rigid bodies I mean rigid bodies that are affected by physics and not just warped around manually everywhere. So pushed around with forces instead of just setting the position every frame.

1 Like

I just tested that and it seems that regular rigid bodies yields even worse (much worse) performance , at least in my case

I’ll try the simplify collision shape path and update

1 Like

I guess I don’t know why it wasn’t a 20 second test to switch out modelShape for a box shape.

…that would have been the first thing I tried.

1 Like

I thought that the original shape was not that complex so I skipped that test. Anyway, now I’m trying this

 modelShape = CollisionShapeFactory.createBoxShape(parentNode);

and it works much faster

1 Like

One thing I don’t understand - why the collision shape gets an offset down from the geometry (see pic below)


The boxes position of the wheels looks fine but the top surrounding Box has an offset down.
What am I doing wrong?
Here is the code:

modelShape = CollisionShapeFactory.createBoxShape(parentNode);
                    
RigidBodyControl modelCtl = new RigidBodyControl(modelShape, mass);
modelCtl.setKinematic(true);
parentNode.addControl(modelCtl);
bulletAppState.getPhysicsSpace().add(modelCtl);
1 Like

What’s the local transform of parentNode?

Never mind—I think there’s a bug in createBoxShape()

1 Like

so there’s also a bug in creating a single box shape:

modelShape = new BoxCollisionShape(((BoundingBox)parentNode.getWorldBound()).getExtent(new Vector3f()));

I guess they are related.

1 Like

Now after switching to a simpler box shape for the race cars, the scene handles 100 concurrent cars with good performance

2 Likes

Isn’t is strange to use the world bound, though?

1 Like

Copy-paste from the documentation… :slight_smile:

2 Likes

You can also manually offset a collision shape by adding child shapes to a compound shape using addChildShape( CollisionShape shape, Vector3f location ).

I think that is also how the vehicle demo does it to lower the center of mass or something…

Also to add to the performance discussion:

It’s always faster to construct compound shapes out of simple shapes like boxes and spheres vs. using triangle meshes because the intersections are much easier to calculate.

A simple example would be two balls colliding. If you use simple sphere shapes the Bullet physics engine can assume these to be perfect spheres and just compare the radians of the balls. Whereas with mesh shapes it has to process many triangle intersections, which is relatively complex.

2 Likes

The car models have many meshes and vertices, which explains why their hull shapes are so complex and costly to simulate.

If you’re satisfied with box shapes, that’s great. If not, there are ways to produce shapes of intermediate complexity, such as createVhacdShape() and createMergedHullShape().

I’ll try to fix createBoxShape() ASAP.

4 Likes

I’ve committed a fix for the various createBoxShape() issues. The fix will be in the next Minie release—very soon, I hope!

I’m going to tag this topic as “Solved”.

4 Likes

Thanks a lot Stephen!

1 Like

If anyone wants to try the fix, it’s in Minie-3.1.0-test4. Production work, however, should wait for the stable Minie-3.1.0 release, coming soon.

3 Likes