Skip to content

Instantly share code, notes, and snippets.

@lukashaertel
Created August 3, 2015 14:39
Show Gist options
  • Save lukashaertel/053c784bee9e80a830ce to your computer and use it in GitHub Desktop.
Save lukashaertel/053c784bee9e80a830ce to your computer and use it in GitHub Desktop.
@Override
public Optional<Vector3> pick(Properties properties, Matrix4 world, Ray ray) {
// Translate to local space
Ray localRay = ray.cpy().mul(world.cpy().inv());
// Result vector and dot product
Vector3 result = null;
float resultDot = Float.POSITIVE_INFINITY;
// Intersect with all planes
Vector3 potential = new Vector3();
next: for (Plane a : HEX_SIDES)
if (Intersector.intersectRayPlane(localRay, a, potential)) {
// Check if point is in front of another plane, then it is not a
// valid intersection
for (Plane b : HEX_SIDES)
if (a != b && PlaneSide.Front.equals(b.testPoint(potential)))
continue next;
//
float dot = Tools.rayDot(localRay, potential);
if (dot < resultDot) {
result = potential.cpy();
resultDot = dot;
}
}
// If there has been no result, return absent
if (result == null)
return Optional.absent();
return Optional.of(result.mul(world));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment