This one, you seem to have another problem there. Do you have the latest Java? I remember there was a super-strange issue they solved with a special float number, can’t remember exactly.
$ uname -a
Linux lifebook 3.10-2-amd64 #1 SMP Debian 3.10.5-1 (2013-08-07) x86_64 GNU/Linux
$ java -version
java version “1.7.0_21”
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-5)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
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.Main.simpleInitApp(Main.java:59)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:722)
sorry I guess I should have posted this first but I thought my code was at fault!
okay its a little less than obvious but for anyone else who’s confused by it here’s the answer
short answer first
[java] Geometry bGeom = (Geometry) ((Node) part).getChild(“BridgeGeom1”);
GImpactCollisionShape collisionShape = new GImpactCollisionShape(bGeom.getMesh());[/java]
if you have only the j3o object or you’re not too sure of what data has what name - for example BridgeGeom1 is actually BridgeGeom in blender… then you can walk through the nodes and identify them like this
[java] SceneGraphVisitor visitor = new SceneGraphVisitor() { @Override
public void visit(Spatial spatial) {
System.out.println(" found " + spatial.getName() + " instance of " + spatial.getClass());
}
};
part.depthFirstTraversal(visitor);[/java]
for a simple object you might get some output like this:
found BridgeGeom1 instance of class com.jme3.scene.Geometry
found BridgeNode instance of class com.jme3.scene.Node
found Models/bridgePart.blend instance of class com.jme3.scene.Node
I still think that casting a Spatial to a Geometry should cause a compile error… (like Node to Geometry does)
Node and Geometry are both sub classes of Spatial (as the tutorial I posted explains). A Spatial can be a Geometry or Node. A Node can never be a Geometry and a Geometry can never be a Node, this is why you can tell this at compile time. Casting means that you tell the compiler that you know what kind of sub class it will be so it will fail at runtime if you were wrong. Maybe you should up your java skills a bit before continuing because this kind of comprehension issue will haunt you more the deeper you get into game development.
Edit: btw, you can open the model in the jME SDK to see the structure and actual names of the model.
@codifies said:
this is why you can tell this at compile time.
nah
Spatial cast to Geometry does compile, it just doesn’t run - which is what caused my confusion
Yes.
@normen said:
A Spatial can be a Geometry or Node. [This is why you can't tell at compile time]
A Node can never be a Geometry and a Geometry can never be a Node, this is why you can tell this at compile time.
When you get a tutorial link, please read the tutorial instead of just looking for the direct answer to whatever you think your question is. If you think the earth is a disk and ask why people don’t fall off the edge more often and I show you a globe that still doesn’t answer the question why they don’t fall of the disk, right?
To try and put this in perspective here is an analogy:
Let’s say you want to go to university to become a published writer or study great literature. If a “finished game” were an “advanced literature degree” then this object casting stuff is the equivalent of learning to make words with letters. So far down into the basics. Super-beginner-object-oriented programming stuff.
It is worthwhile to try and run through some Java tutorials with some real meat to teach you object oriented programming.
When you sent the link I thought you were answering the question I asked, not seeing anything about Mesh data I completely missed the point you were making about the Spatial, even missing for a while that it was abstract…
I don’t see why you just couldn’t have a optional Mesh property for the Node and have done, you can either use the Node as pure hierarchy or use it for both hierarchy and geometry - but you are SO gonna disagree with that…
@codifies said:
When you sent the link I thought you were answering the question I asked, not seeing anything about Mesh data I completely missed the point you were making about the Spatial, even missing for a while that it was abstract...
I don’t see why you just couldn’t have a optional Mesh property for the Node and have done, you can either use the Node as pure hierarchy or use it for both hierarchy and geometry - but you are SO gonna disagree with that…
You mean like how most methods in the known universe will return Collection or List instead of SomeSpecificListImplementation? (References: see JDK, Guava, 99.9% of other projects. ;))
This design is so common among graphs that there is even a pattern for it (visitor pattern if you care to look it up). So I think your design suggestions are a bit misguided. Forcing all Spatial subclasses to return Mesh is patently ridiculous.