Can not cast spatial to geometry

Hi guys I have been using this code to make a simple ship:


package mygame;

import com.jme3.asset.AssetManager;

import com.jme3.input.InputManager;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.material.Material;

import com.jme3.scene.Node;

import com.jme3.scene.Spatial;

public class Player {

public Spatial b;

Main game;

AssetManager assetManager;

InputManager inputManager;

Node rootNode;

Player(Main aThis){

game = aThis;

rootNode = game.getRootNode();

assetManager = game.getAssetManager();

inputManager = game.getInputManager();

b = (Spatial) assetManager.loadModel("Models/Garbage Pod.j3o");


Material mat_lit = new Material( assetManager, "Common/MatDefs/Light/Lighting.j3md");

mat_lit.setTexture("m_DiffuseMap", assetManager.loadTexture("Textures/GPod.tga"));

mat_lit.setFloat("m_Shininess", 10f); // [0,128]



initKeys(); // load my custom keybinding


/** Custom Keybinding: Map named actions to inputs. /

private void initKeys() {

* You can map one or several inputs to one named mapping. /

inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_LEFT));

inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_RIGHT));

inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_UP));

// left click!

* Add the named mappings to the action listeners. /

inputManager.addListener(analogListener, new String[]{"Left", "Right", "Up"});


* Use this listener for continuous events /

private AnalogListener analogListener = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

if (name.equals("Right")) {

b.rotate(0, -5
tpf ,0);


if (name.equals("Left")) {

b.rotate(0, 5*tpf,0);


if (name.equals("Up")) {





It has been working for a while now. I dont remember making ANY changes to this code but all of the sudden i am getting this error:

Jan 23, 2011 3:21:13 PM handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.ClassCastException: com.jme3.scene.Node cannot be cast to com.jme3.scene.Geometry
at mygame.Player.(
at mygame.Main.simpleInitApp(
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(

I know what the error means but I have no clue what could be causing it, Since this code has worked for me before. Does anyone see any errors? I sure couldnt after looking over it so many times :S.

I think, that for one reason or other, this line:

[java]assetManager.loadModel("Models/Garbage Pod.j3o")[/java]

now returns a node, rather than a spatial. What used to be a spatial has become a child of a node.

See if it helps to cast it to Node instead. If you want to dig into it, do a print out of the children of that node and see what it brings.

The stack trace you posted doesn’t match the code!

The crash is at “mygame.Player.(”

But line 46 just has a comment:

[java] /** You can map one or several inputs to one named mapping. */[/java]

In any case, you shouldn’t cast models you get with loadModel() to geometry.

Since loadModel() returns a Spatial object, just use that as is

Oh yes, thats another thing i forgot to mention. It points to line 46 even if i take out half the code… even if line 46 is blank. It will error there.

ANND, changing the spatial object to a Node does not make a difference. Ive tried it already. This is why i’m so darn confused :(.

I think you forgot to recompile the code :stuck_out_tongue:

It’s still using the old one obviously

Figured it out, just had to build & clean then it worked. Thanks!