Geometry cannot be cast to Node

(Java and LWJGL expert here, but I only started using JME yesterday)


I have successfully followed the collision tutorial. I now have a heightmap-generated world, with a player which can walk along and collide with.

However, when I attempt to add collision to a loaded OBJ model, I get the following error:

java.lang.ClassCastException: com.jme3.scene.Geometry cannot be cast to com.jme3.scene.Node

The error occurs when using this code:

CollisionShape planeShape =
        CollisionShapeFactory.createMeshShape((Node) plane); <- Error happens here
RigidBodyControl planeLand = new RigidBodyControl(planeShape, 0);

Obviously, plane is not instanceof node. But why? When I add terrain collision, I use the line CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) terrain); without any issues. Both terrain and plane are of type Spatial.

To load the plane variable, I use the following code:

Spatial plane = assetManager.loadModel("Models/PlaneModel.obj");

And I am successfully able to render the plane.

I haven’t been able to solve this after about an hour of looking (and testing), so any help would be appreciated.

Thank you

plane is a Geometry and not a Node. The plane model must be simple enough to be easily represented as a single Geometry.

If you could explain more about what is confusing about that then we could help further. Else I’m not sure what the issue is.

Maybe this helps:

I guess your plane needs to be contained in a node. Then, you can feed that node to the createMeshShape method. Haven’t checked though.

No, because the method actually takes a Spatial and doesn’t require the casting at all.

…but I was trying to give OP the benefit of the doubt since he says he’s a Java expert.

Thanks, that solves the issue.

In the collision tutorial, they specifically cast a Spatial to a Node, hence the confusion.

I don’t know if you have the power to do such things, but it would be a good idea to remove the unnecessary cast from the tutorial so to not confuse future newcomers.

Anyone has the power… including yourself.