Skip to content

Commit

Permalink
jMonkeyEngine#2284 Double check the collision really has happened in …
Browse files Browse the repository at this point in the history
…world space (jMonkeyEngine#2285)

(otherwise glancing blows can end up "happening" but at infinite distance due to numerical precision)
  • Loading branch information
richardTingle authored Aug 17, 2024
1 parent bc346ed commit e2e9a94
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions jme3-core/src/main/java/com/jme3/collision/bih/BIHNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,20 @@ public final int intersectWhere(Ray r,
t = t_world;
}

Vector3f contactNormal = Triangle.computeTriangleNormal(v1, v2, v3, null);
Vector3f contactPoint = new Vector3f(d).multLocal(t).addLocal(o);
float worldSpaceDist = o.distance(contactPoint);

CollisionResult cr = new CollisionResult(contactPoint, worldSpaceDist);
cr.setContactNormal(contactNormal);
cr.setTriangleIndex(tree.getTriangleIndex(i));
results.addCollision(cr);
cols++;
// this second isInfinite test is unlikely to fail but due to numeric precision it might
// be the case that in local coordinates it just hits and in world coordinates it just misses
// this filters those cases out (treating them as misses).
if (!Float.isInfinite(t)){
Vector3f contactNormal = Triangle.computeTriangleNormal(v1, v2, v3, null);
Vector3f contactPoint = new Vector3f(d).multLocal(t).addLocal(o);
float worldSpaceDist = o.distance(contactPoint);

CollisionResult cr = new CollisionResult(contactPoint, worldSpaceDist);
cr.setContactNormal(contactNormal);
cr.setTriangleIndex(tree.getTriangleIndex(i));
results.addCollision(cr);
cols++;
}
}
}
}
Expand Down

0 comments on commit e2e9a94

Please sign in to comment.