[Solved]Importing .j3o model to a Geometry

Hello,

my problem is the next :

I have a models compose of one mesh and one material,

I want to load a new instance of it when I pressed the key “A”, but when I try to cast it to an Geometry I have a :

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



If I try to cast it to a Node all work, so, I would know if it have the possibility to convert the Node (or the spatial) to a Geometry (or several Geometry)

my actual code :

[java]CollisionResults cr = new CollisionResults();

Vector2f click2d = inputManager.getCursorPosition();

Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0).clone();

Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1).subtractLocal(click3d.normalizeLocal());

Ray ray = new Ray(click3d, dir);

try

{

map.collideWith(ray, cr);

inserCoordinate = cr.getClosestCollision().getContactPoint();



Geometry toAdd = (Geometry)assetManager.loadModel(“Models/hmls/hmlsOneMesh.j3o”);



toAdd.setLocalScale(0.10f);



toAdd.setLocalTranslation(new Vector3f(inserCoordinate.x, inserCoordinate.y + 1.05f, inserCoordinate.y));



CharacterStateControl toAddCharacterControl = new CharacterStateControl();



toAdd.addControl(toAddCharacterControl);



toAddCharacterControl.setAttack(1);

toAddCharacterControl.setSpeed(10);

toAddCharacterControl.setHeight(1);

toAddCharacterControl.setLife(10);



numberOfModel ++;

character.attachChild(toAdd);

}

catch(Exception e)

{e.printStackTrace();}[/java]



Thank you



See solution bellow

You use java instanceof to test if a downcast is possible. However, in this case it probably isn’t that. Open your model in the SceneComposer and you will see that it is most probably a branch of nodes. So you will have to search getChildren downwards until you find the geometry.

Hope that helps.



You can look here for more info: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

1 Like

assetmanager.loadModel(String name) returns an object of type Spatial.

The class Node and Geometry extends from the abstract class Spatial.

Obviously the dynamic type of your object is of class node, thats why you can only cast to Node.



So any reason why not to use Spatial toAdd instead of Geometry toAdd.

1 Like

@all : thank you for have tryed to solve my problem



@baalze : all my control are based for work with geometry, and I want to use some specific method which work only with geometry



@jmaasing : ok I try that when I come back on my home