How do I play the animation of Spatials that are returned from CollisionResults?

        CollisionResults results = new CollisionResults();
        ray.setOrigin(cam.getLocation());
        ray.setDirection(cam.getDirection());
        rootNode.collideWith(ray, results);
        if (results.size() > 0) {
            Spatial target = (Spatial)(results.getClosestCollision().getGeometry());

                if (target.getName().equals("door")) {
                    System.out.println(target.getControl(AnimControl.class));//.getChannel(0).setAnim("open");
                    System.out.println(door.getControl(AnimControl.class));
                }
            
        }

The code above shows how the target spatial, which is a spatial spatial named “door”, is being returned by the collision results.
Doing this should return an exact copy of the target Spatial right? So by that definition, the target, which is now referencing the door Spatial, should allow me to get its animation control. The weird thing is,
System.out.println(target.getControl(AnimControl.class));
prints out null, while printing out the AnimControl of the original Spatial prints out the actual AnimControl.
System.out.println(door.getControl(AnimControl.class));

I do know that there is another way to go about doing this; That it is also possible to just make the animation channel for every door in my game a global variable, and then do something like, door4channel.setAnim("open) but I think this way is a more convenient and simpler way to do it.

So would anyone know how to get the AnimControl from a CollisionResult? Thanks.

Hint 1: CollisionResults only contain geometries.

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

Hint 3: http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#getParent()

1 Like

Ok but then how does is make sense that
System.out.println(target.getControl(AnimControl.class));
prints out null, but
System.out.println(door.getControl(AnimControl.class));
prints out an object?
Both should be referring to the door spatial. The only difference between the target spatial and the door spatial is that the target spatial is a Geometry cast into a Spatial

It’s a geometry, not a spatial. Generally the door spatial you’re looking for can be grabbed using target.getParent(). This is because the geometry itself is a child of the spatial and doesn’t have the AnimControl.class you’re trying to grab. The geometry is usually named the same as the spatial, so that’s why your code does what it does currently, and you can confirm this by opening the model in the SDK and checking the names.

1 Like

Oh thanks! That worked. I didn’t know Spatials had Geometry objects, since Geometry is a child of Spatial.

@Calcium3D said: Oh thanks! That worked. I didn't know Spatials had Geometry objects, since Geometry is a child of Spatial.

Geometry is a SUBCLASS of Spatial. So is Node. Spatial is not a concrete class so nothing is actually “just a spatial”. Your scene is made of Nodes and those Nodes have Geometry. Collisions ONLY happen on Geometry.

At the risk of repeating myself, I refer you to this tutorial again: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

1 Like

Yes I saw that a couple weeks ago, I just meant to say subclass in stead of child. I was sleepy and used the wrong word