Low fps with RigidBodyControl added to Terrain


Admittedly I’m a jme3 newbie (but at least well versed in Java). I’m creating a game which currently has 3 models loaded, water and terrain (mostly based on examples for now). With physics activated amongst the models, everything is working fine and fps is around 30 - 70. But when I add

    terrain.addControl(new RigidBodyControl(0));

to the terrain, the fps drops to 1 on a Lenovo W530, 2x4 cores with a Nvidia Qadro K2000M. The physics work fine though. I do not understand what I’m doing wrong. Are the models too complex or wrong ? Or is there a bug in the engine ?

The source code is available on GitHub - neuweiler/CarrierCommander: A java based open source re-make of a once fantastic game
(uncomment lines 323-324 in CarrierCommander.java to see the fps drop)

BTW: Thanks for this wonderful engine ! Setting up the base for this game was done in about 5 hours with the tutorials. Really stunning results with such little code ! I started with Java3D in 2007 and ended up freezing the project. jME3 really is a “game” changer :wink:

I’m not sure why you would want the terrain to be affected by physics. I have to admit that I have never used bullet physics, but I’m a physicist and it does not make much sense to me.
If you would like to apply some force on terrain, of course it would be the appropriate form of Newton’s law (actio = reactio). But usually terrain is much heavier than anything else, so you can just neglect the force that acts on terrain.
Therefore you should use terrain as an inertial reference frame, for all other physics, where objects move relative to some terrain.
Maybe you can clarify this.

One reason for the frame rate to drop is, what I think of, the shear amount of triangles, that needs to be checked for collision. If you can’t find the collision with other objects then physics just won’t work.

1 Like

If you want to make things bounce off the terrain it has to participate in the physics simulation. That’s accomplished by adding a rigidbody control based on the terrain mesh.

It is in fact infinitely heavy in the physics simulation :slight_smile: , that’s why the mass is set to 0 in the contructor.

One probable reason for the FPS-drop is that the terrain is large and can not quickly be culled in the physics simulation. So most (or even all)l objects must be checked against the terrain object. Depending on the amount of objects that can decrease performance.

Sorry, forgot to state the purpose of using physics on the terrain: The terrain doesn’t have to be affected, that’s why it has a mass of 0 (as specified in the tutorials). It should only affect other objects. The game will have amphibious vehicles that will drive on the terrain (like a tank or car). Also objects like airplanes or an aircraft carrier which can collide with the terrain (above and under water) and should get blocked by it, take damage or get destroyed. The terrain will consist of several islands with mountains, flat terrain and under-water terrain.

Ok, didn’t know that. (I like your project. :+1:)

So, you can try to reduce the amount of terrain for the physics simulation - somehow. Try it with just a small chunk of terrain, maybe 256 x 256 vertices and see if it makes the performance better.

1 Like

Shouldn’t it be

RigidBodyControl rbc = new RigidBodyControl(0);

Not sure if that’s the problem but at least that’s how I always add my terrain and I never had a problem with FPS.

And as the others pointed out, you might want to change the vertex count of the terrain if that doesn’t help.

1 Like

Thanks a lot for the thumbs up :smile:

I just tried it with 128x128 and performance is definitely better… as long as no object is close to the terrain, I get 60 fps but when the carrier comes close to the mountain, it drops to 7-13 fps which is still a tad too slow - considering it should also run on slower machines.
Darn, I had the idea of using an even bigger terrain (1024x1024) with many islands on it at the same time (see http://www.darkbee.com/custom/images/carriermap_th.gif) . But apparently, this is not going to work out. When switching from one island to the other I’ll have to replace the terrain. Or probably it’s the complexity of the carrier itself. I’ll try also with different models.

I tried it but it didn’t help unfortunately. Thanks anyway !
I replaced the carrier with a simpler object and fps was higher / acceptable with a 128x128 terrain. With 512 x 512 it was still too low. But with a different object I was able to see a difference in fps as soon as an object get close to the terrain.
I get about 200k vertices / triangles at the moment. Can you tell me how many you’ve got in your apps ?

A usual trick is to have the collision object a lot simpler than the actual rendered mesh. Using a capsule for example. Maybe that is good-enough for a ship - it depends on your use case.

1 Like

That did the trick, thanks a lot !
I replaced the model’s default collision shape with this:

    CompoundCollisionShape comp = new CompoundCollisionShape();
    comp.addChildShape(new BoxCollisionShape(new Vector3f(20, 13, 149)), new Vector3f(0f, -1f, -25f));
    comp.addChildShape(new BoxCollisionShape(new Vector3f(7, 19, 30)), new Vector3f(30f, 30f, 5f));
    RigidBodyControl control = new RigidBodyControl(comp, 100000000);

Probably needs some more tweaking but for now it’s ok and performance is unaffected: 140fps even with a 512x512 mesh. It’s a pleasure to watch the carrier whirl through the air (0 gravity) or sink to the ground and roll over with different friction parameters. :smile:

Thanks everybody for the very fast response and good support !

Now let’s apply some forces to keep it floating and upgright after a collision… that’s gonna be fun. I have the idea to apply a force from below and depending on the roll and pitch to move it accordingly aft/stern starboard/backboard away from center of gravity. If that doesn’t work, apply split forces in 3 or 4 places.

1 Like

There are some other projects that maybe is similar to what you are doing. You might find some inspiration from Pirate Hell (http://www.piratehell.net) by ceiphren and his work OceanMonkey Update

Also Enemy Ahead! might be similar: Enemy Ahead!

1 Like

Thanks a lot for the hints ! I’m looking forward to learn from these projects. They look really nice.
In the meantime I’ve come up with a solution to simulate a floating boat. Looks pretty realistic to me. I’ll post it in a different thread though.

1 Like