Get mesh from spacial

    Geometry geom2 = (Geometry) part;
    GImpactCollisionShape collisionShape = new GImpactCollisionShape(geom2.getMesh());

although the cast from spatial to geom compiles it causes a runtime error ( :o runtime error in java :o) )

whats the recommended way to get actual mesh data from a spatial holding a jo3 object

TiA

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!

Erm… I thought you get an actual JVM crash instead of a nice exception that should actually tell you what to do… :roll:

You loaded a Node, cast it to Node and use that to find the actual child Geometry you want.

sorry? I don’t understand your reply

I want to get a Geometry from a Spatial, how do I do that?

A Spatial won’t cast to a Geometry?

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

Nah, its definitely nothing to do with the jdk/jre version, I see exactly the same error in Windows…

Casting a Spatial into a Geometry aint the way to go !

interestingly if I cast to a Node first I do get a compile error (inconvertible types), which I suspect you should do with Spatial -> Geometry

Geometry geom2 = (Geometry)((Node) part);

@normen said: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

sorry missed that post last time I was here, just didn’t display - most odd! (maybe we posted at the same time?)

alas nothing in that url tells you how to access the Geometry from a Spatial…

As I said, the Spatial you have there is a Node, the Geometry you want is one of its children.

Determine if spatial is a node, if so get all childs go to beginning of sentence for each. instanceof is your friend here

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.

1 Like

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

@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?

re: Java learning, I agree with Normen.

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 think the engine passing abstract classes to the user is interesting design and is certainly likely to confuse
http://www.engr.mun.ca/~theo/Courses/sd/5895-downloads/sd-principles-3.ppt.pdf

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 think the engine passing abstract classes to the user is interesting design and is certainly likely to confuse

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.

Forcing all Spatial subclasses to return Mesh is patently ridiculous.

which is why I didn’t suggest exactly that