MS3D Model and Mouse Picking


    I'm writing a game (shooting training) where there are several targets randomly distributed in the scenario, and when a target gets hit by a shot, it it is destroyed (removefromParent()), from the scene. I also have some Spheres appearing randomly in the scenario too, to train for accuracy.

    I'm having a problem with the targets, because when I have many of them randomly distributed, sometimes they get in line. When getting in line, I shoot the first one in the line, but the last one gets hit (and removed from the scene), which is a false positive. That's weird, because when shooting the spheres, it is very precise.

    I'm not sure if this is related to the mouse picking technique I'm using, or something with the BoundingCapsule() for the Node Model. For the Mouse Picking, I'm using the following:

public void performAction(InputActionEvent evt)
        shotTime += evt.getTime();
        // Get the mouse input device from the jME mouse
        // Is button 0 down? Button 0 is left click

        if (MouseInput.get().isButtonDown(0) && shotTime >= 0.2f)
            shotTime = 0;
            Vector2f screenPos = new Vector2f();
            // Get the position that the mouse is pointing to
            screenPos.set(am.getHotSpotPosition().x, am.getHotSpotPosition().y);
            Vector3f worldCoords = display.getWorldCoordinates(screenPos, 0);
            Vector3f worldCoords2 = display.getWorldCoordinates(screenPos, 1);
            Ray ray = new Ray(worldCoords, worldCoords2.subtractLocal(worldCoords).normalizeLocal());
            PickResults results = new BoundingPickResults();
            scene.findPick(ray, results);

            if (results.getNumber() > 0)
                PickData pickData = results.getPickData(0);
                Spatial objetoEscolhido = results.getPickData(0).getTargetMesh().getParentGeom();
                // pickData.getTargetMesh().setRandomColors();

      "Nome do Objeto: "
                        + objetoEscolhido.getName());

A ray cast hits everything in its way, but the nearest object is not at index 0.

The problem is, that your just removing the first result you get, but thats not what you want.

You need to get all results, and then check which object is nearest to the player / camera.

see this recent topic, with a solution to your problem: