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…
…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.
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.
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.
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().