Physics lag and fix

Hello eveyrbody.

I found a “bug” and a way to fix it. But first, let’s explain the context.

I am creating a “robotcraft”-clone as i think that it’s not very hard to do and i want to provide a free version of it.

So far, everything works fine : i have an editor, the network part is ok etc.

(editor: )

Ok, now the bug. I noticed that when two vehicles collide, the server lags HARD (it takes 100% of a cpu and the game goes in slowmo).
Vehicles are like this:

I created the collision shape with this:

[java]CollisionShape cs = CollisionShapeFactory.createDynamicMeshShape(the_vehicle);[/java]
(well, not exactly like this, but you got the idea).

So, why is there a lag ? Because this method does NOT return a hull collision shape : it returns a compoundcollisionshape made of hulls.

And, for a reason i don’t know, when a vehicle with wheels hit an other vehicle with wheels and bot use a compoundcollisionshape, it lags. Without the wheels, it’ ok however.

So, this is the fix:

public static CollisionShape createHullShape(Node n)
Mesh m = getMerged(n);
return new HullCollisionShape(m);

private static Mesh getMerged(Node n)
final Collection<Geometry> parts = new ArrayList<>();
Mesh outMesh = new Mesh();

n.depthFirstTraversal(new SceneGraphVisitor() {

  public void visit(Spatial spatial)
    if ( ! (spatial instanceof Geometry) )
    parts.add((Geometry) spatial);

GeometryBatchFactory.mergeGeometries(parts, outMesh);

return outMesh;


No magic here : i create a single mesh from geometries, then a single hollcollisionshape from this mesh.
it’s not a magic fix that will solve the problem in every situation, but maybe it could be a good idea to add this in the collisionshapefactory in a futur version, as an other method to create a collisionshape from a node.

Also, an other bug i found (but didn’t solve it): when a vehicle hit a planecollisionshape (an invisible wall) its position becomes (NaN, NaN, NaN). I think it’s because a wheel is only a ray, so it goes from “not in the wall” to “in the wall” and from there the raycast can’t compute the distance from the “ground”. It doesn’t happen in real life cause in real like wheels are not rays.

sorry i messed up with tags and i can’t edit my first post.

the first image is :
and the second is :