Ray intersection with spatial.. closest hit

I am trying to send out an ray and retrive the geometry. I had some problems with this since the ray would hit my skybox and return that as the first result.

Is there a way to retrive the triangle closest to the ray origin in terms of world coords, or do I have to walk through all of them and calculate distance?


My code

            Ray ray = new Ray(projectTile.getLocalTranslation(), direction);
            PickResults results = new BoundingPickResults();

            int[] indexBuffer = new int[3];
            int hits = results.getNumber();
            if(results.getNumber() > 0) {
                for(int i = 0; i < results.getNumber(); i++) {
                      TriMesh mesh = (TriMesh)results.getPickData(i).getTargetMesh();
                  //get model coord for hit
                  Vector3f coord = new Vector3f(
                  // translate coord to world
                  Matrix3f rotMatrix = projectTile.getLocalRotation().toRotationMatrix();
                  Vector3f worldCoord = rotMatrix.mult(coord,null);

Well, as for the skybox, you just shouldn't put it on the same Node as the rest of the object you're trying to pick.

Yeap I got that much but I might have several spatials I could hit on the way

Ok, I'm going to add a couple features:

  1. Spatial will have a flag (isCollidable), if this is set to false, it will not be used in collision calculations.
  2. PickResults will have information regarding distance between the origin of the pick ray and the bounding picked.

    I'll give out the details on how to use it when it's complete.

Ah thats cool… I was on the way todo a ray check on each spatial  in scene and for each hit do a length and then pick the closest one…

Spatial.isCollidable() and Spatial.setIsCollidable(boolean) is in CVS.

This will allow you to control individual Spatials, so you can say skyBox.setIsCollidable(false) and it will no longer show up in your results.

Working on distance information now.

Ok, everything is in.

You can setCheckDistance(true) in PickResults, which will do the following:

PickData will contain a distance variable

the hit list will now be sorted near to far.

TestPick now shows this.

Sounds very useful to me.

Sob…two weeks ago I had to implement it…

I used distance between the origin of the ray and the intersected volumes and set the closest volume as visible…

New features will be useful!!