Casting a spatial to node

I’m trying to create a solid floor using a geometry box, 30x30. In the tutorial I am using, the author used a loaded model for the scene, but since I am using a jme primitive I need to cast it to a spatial.

    sceneModel = (Spatial)floorGeom;
    CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node)sceneModel);
    RigidBodyControl landscape = new RigidBodyControl(sceneShape,  0);
    sceneModel.addControl(landscape);
    rootNode.attachChild(sceneModel);

The first line is the problem line. There really isn’t a reason I’m not creating a model instead for the floor, using a primitive just seems easier at the moment. I get a cannot cast geometry to spacial exception thrown when I run the program, so I am wondering what I could be doing differently?

Edit: I read the error wrong and it is actually the second line which throws the error, cannot cast geomerty to node. So this means that sceneModel is not being casted as a spatial per the first line?

A Geometry is not a Node… but both are Spatials.

The first line is unnecessary and basically meaningless because if you already had a Geometry then you could do everything you could do to a Spatial already. This is basic inheritance. (Strongly recommend learning more Java before diving into 3D game development.)

I’ve never used CollisionShapeFactory before but 2 seconds of javadoc clicking shows that it takes a Spatial so that cast is unnecessary also.

When we recommend that people learn Java first it isn’t because we are trying to discourage. Quite the opposite. You are way more likely to get discouraged if you get tripped up on these super-basic issues every time you try to duct tape some cut-pasted code together. If coding a game were climbing a mountain then these sorts of issues are like tying your shoes level of complexity.

1 Like