Ray collision with Spatials

Hello! So, another problem just popped up for me … :explode: It’s probably easy to solve, but I’ve been searching for an answer and trying out my own stuff but I think I need some help! So, I was wondering, I use a ray collision to find intersections when you click… as you’d do…

     CollisionResults results = new CollisionResults();
     Ray ray = new Ray(cam.getLocation(), cam.getDirection());
     rootNode.collideWith(ray, results);
     String nameHit = results.getClosestCollision().getName();

This works perfectly for geometries that are just added through code, but not for Spatials using imported assets… so, how would I make it recognise spatials as well? They all have a RigidBodyControl, maybe that helps? Thanks! :smiley:

what doesn’t work? It will give you back the geometry that collided, maybe you want the parent or something?

You can also do a PhysicsRayCast, which will use the collision shape instead.

@wezrule said: what doesn't work? It will give you back the geometry that collided, maybe you want the parent or something?

You can also do a PhysicsRayCast, which will use the collision shape instead.

Ah, I should have been more specific. The name of the geometry is simply “cylinder1” despite the fact that I set the name of the spatial to “beer can”… how do I set the name of whatever the ray collides with?

I think he means he just gets a geometry back instead of his expected spatial. Use .getParent() - you may need to traverse up several times depending on your model structure.

@jayfella said: I think he means he just gets a geometry back instead of his expected spatial. Use .getParent() - you may need to traverse several parents depending on your model structure.

Yes, that may well be the problem. How would I go back the needed numbers of parent levels?

We’ll you would traditionally check if the node is an instance of spatial and then cast it back to a spatial. So…

[java]Node mySpatial = geo.getParent();
While (!(myspatial instanceof Spatial))
{
mySpatial = mySpatial.getParent();
}

Spatial sp = (Spatial)mySpatial;[/java]

@jayfella said: We'll you would traditionally check if the node is an instance of spatial and then cast it back to a spatial. So...

[java]Node mySpatial = geo.getParent();
While (!(myspatial instanceof Spatial))
{
mySpatial = mySpatial.getParent();
}

Spatial sp = (Spatial)mySpatial;[/java]

So when I try that, I simply get that the name is “Cylinder”, despite the fact that I changed the name to “beerCan” … any ideas?

[java]
targetMove = results.getClosestCollision().getGeometry();
Node mySpatial = targetMove.getParent();
while (!(mySpatial instanceof Spatial))
{
mySpatial = mySpatial.getParent();
}

Spatial sp = (Spatial) mySpatial;
System.out.println(sp.getName()); // Prints out “Cylinder”
[/java]

Well everything derives from the Node class, so it’s difficult to distinguish between objects like that. It was a bad example tbh. An alternative approach would be to check the name instead of the type:

[java]
Node mySpatial = targetMove.getParent();
while (!(mySpatia.getName().equals(“beerCan”))
{
mySpatial = mySpatial.getParent();
}

Spatial sp = (Spatial) mySpatial;
System.out.println(sp.getName());
[/java]

you have that backwards jayfelle,. everything derives from Spatial, not Node. Node is a subclass of spatial.

but yes the easiest way to do is is to traverse upwards and check for the name of the node youre looking for.

another way to do it is to look for userdata or controls attached, for instance if you wanted to play a “hit” animation on a character, then traverse up until you find AnimControl. it just depends on what kind of data youre searching for.