Skip to content

Commit

Permalink
#2284 Double check the collision really has happened in world space
Browse files Browse the repository at this point in the history
(otherwise glancing blows can end up "happening" but at infinite distance due to numerical precision)
  • Loading branch information
richardTingle committed Jun 14, 2024
1 parent b7787a7 commit 56e7003
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 56e7003

Please sign in to comment.