bugfix: TrianglePick.getIntersectionPoint() (1 post)

  • Profile picture of ttrocha ttrocha32p said 1 year, 9 months ago:

    TrianglePick.getIntersectionPoint() is not always returning the nearest intersection. Actually getIntersectionPoint is a bit unspecifiy as there might be more than one points in one mesh that are hit. Nevertheless to make the thing more predictable I changed the method to always return the nearest intersectionpoint.

    as before, to get a result you need enable checkDistance for TrianglePickResult

    Here is the patch:

    Index: src/com/jme/intersection/TrianglePickData.java
    ===================================================================
    --- src/com/jme/intersection/TrianglePickData.java   (revision 5616)
    +++ src/com/jme/intersection/TrianglePickData.java   (working copy)
    @@ -52,7 +52,8 @@
         private final Vector3f[] vertices = new Vector3f[]{new Vector3f(), new Vector3f(), new Vector3f()};
     
        private final Vector3f intersectionPoint = new Vector3f();
    -
    +   private final Vector3f nearestIntersectionPoint = new Vector3f();
    +   
         public TrianglePickData(Ray ray, TriMesh targetMesh,
              ArrayList targetTris, boolean checkDistance) {
           super(ray, targetMesh, targetTris, false);
    @@ -81,6 +82,7 @@
              distances[i] = triDistanceSq;
              if (triDistanceSq > 0 && triDistanceSq < distanceSq) {
                     distanceSq = triDistanceSq;
    +                nearestIntersectionPoint.set(intersectionPoint);
              }
           }
           
    @@ -128,8 +130,15 @@
           return Float.POSITIVE_INFINITY;
        }
     
    +   /**
    +    *
    +    * returns the nearest intersection point! This method only works with setCheckDistance(...)
    +    * enabled in TrianglePickResults
    +    *
    +    * @return Vector3f nearest intersection-point
    +    */
        public Vector3f getIntersectionPoint() {
    -      return intersectionPoint;
    +      return nearestIntersectionPoint;
        }