Basically, I have this Spatial, whose name is “Opp”. However, when the ray touches it, the name output from the following code:
[java]System.out.println("Hit: " + results.getCollision(i).getGeometry().getName());[/java]
is submesh0. What must I do to make the output correct?
Name it when you instantiate it?
[java]
Sphere something = new Sphere(60, 60, radius);
geom = new Geometry("Something", something);
geom.setName("MyName");
[/java]
Oh. I wasn’t ‘converting’ it to a Geom. Thanks.
Wait. I’m using a Spatial, not a Sphere. ‘something’ can’t be a Spatial, if I understand well.
[java]
Spatial spa = new Spatial("MyName");
[/java]
Spatial is abstract, cannot be instantiated.
Come on… Check the methods!
[java]
spatial.setName("MyName");
[/java]
Already done that. I thought it was obvious. I said its name is “Opp”. However, when I hit it, the name it displays is submesh0.
Let’s go back from the start.
The CollisionResults will give you a list of geometries with which the ray will collide.
If you get a weird result then you’re doing something wrong. I use getName() for my collision detection and the code reports what I’ve entered. Use the debugger and highlight “results.getCollision(i).getGeometry()” and see what that gives you.
My guess is, it’s something you’re not expecting and is not named.
Maybe ur setting it on the node, and not the geometry?
No, I’m setting it on the Spatial:
[java]marker4 = (assetManager.loadModel(“Models/The Ark3/The Ark3.j3o”));
marker4.setMaterial(assetManager.loadMaterial(“Materials/Ark2.j3m”));
marker4.setName(“Opp”); [/java]
Could the problem be because I am using a Spatial and not a Geometry? With Geometry, it returns the name. This only happens when I hit an imported model.
PS: [java]System.out.println("Hit: " + results.getCollision(i).getGeometry().getName());[/java]
This is the code I use to get the name.
We are saying that you might be getting something you’re not expecting from the CollisionResults. Something that isn’t named.
I remember when I did my collision detection I was getting a weird collision. It turned out it was the orbit that I was drawing, not a star or a planet… I found out what it was by using the debugger and exploring the object’s properties.
No luck. Whenever I hit the geometry (boxes and spheres) the names display well. When I hit the Spatial, it displays ‘submesh0’. I also added a watch results.getCollision(i).getGeometry() but it displayed the same thing.
To set a name on a spatial you can also use spatial.setName(“Name”);
The way I do it, because of the multiple of object I have in my solar system scene, is that I put a userData on each of those objects I want to use for collision.
That works perfectly for me.
[java]
CollisionResults cr = new CollisionResults();
sceneNode.collideWith(ray, cr);
if (cr.size() > 0) {
for (Iterator i = cr.iterator(); i.hasNext() {
CollisionResult col = (CollisionResult) i.next();
String className = col.getGeometry().getUserData(“Class”);
if (className != null) {
if (className.equals(“Planet”) || className.equals(“Star”)) {
System.out.println("Closest collision : " + col.getGeometry().getName());
setPickedObject(col.getGeometry());
int id = (int) col.getGeometry().getUserData(“ID”);
pickObject(col.getGeometry().getWorldTranslation(), findObject(className, id));
gotOne = true;
}
}
if (gotOne) {
break;
}
}
}
[/java]
When you get collision results they are for Geometry objects. When you loaded the model it was probably a Node that contained a bunch of Geometry objects.
This is a pretty common pitfall.
I’m sort of surprised that no one has suggested just checking the hit geometry’s parent (and potentially that spatial’s parent and so on) since this question comes up every other week or so.
Try this code and see what you see:
[java]
for( Spatial s = results.getCollision(i).getGeometry(); s != null; s = s.getParent() {
System.out.println( “name:” + s.getName() );
}
[/java]
Yeah. Nesting can be brutal.
I honestly prefer my way because it suits me fine and I’m sure I won’t hit “garbage”. There a lot less iteration too. But that’s just my way of doing things. Memory footprint is a bit higher but I can live with that.