Skip to content

Instantly share code, notes, and snippets.

@macalinao
Created December 28, 2011 05:47
Show Gist options
  • Save macalinao/1526603 to your computer and use it in GitHub Desktop.
Save macalinao/1526603 to your computer and use it in GitHub Desktop.
public static boolean checkCollision(BoundingBox a, Plane b) {
boolean pos = (a.min.dot(b.normal) > 0);
return pos != (a.max.dot(b.normal) > 0) //Planes that are axis-aligned. most cases
//X N N
|| pos != (new Vector3(a.max.getX(), a.min.getY(), a.min.getZ()).dot(b.normal) > 0)
//N X N
|| pos != (new Vector3(a.min.getX(), a.max.getY(), a.min.getZ()).dot(b.normal) > 0)
//N N X
|| pos != (new Vector3(a.min.getX(), a.min.getY(), a.max.getZ()).dot(b.normal) > 0)
//N X X
|| pos != (new Vector3(a.min.getX(), a.max.getY(), a.max.getZ()).dot(b.normal) > 0)
//X N X
|| pos != (new Vector3(a.max.getX(), a.min.getY(), a.max.getZ()).dot(b.normal) > 0)
//X X N
|| pos != (new Vector3(a.max.getX(), a.max.getY(), a.min.getZ()).dot(b.normal) > 0);
}
@Test
public void testCheckCollision_BoundingBox_Plane() {
System.out.println("checkCollision");
BoundingBox a = new BoundingBox(
new Vector3(-1, -1, -1),
new Vector3(1, 1, 1));
Plane b = new Plane(
new Vector3(0, 0, 0),
Vector3.Up);
boolean result = CollisionHelper.checkCollision(a, b);
assertTrue(result);
//Check slanted plane
b = new Plane(
new Vector3(0, 0, 0),
new Vector3(1, 1, 1).normalize());
result = CollisionHelper.checkCollision(a, b);
assertTrue(result);
//Check non intersecting
b = new Plane(
new Vector3(-2, -2, -2),
Vector3.Up);
result = CollisionHelper.checkCollision(a, b);
assertFalse(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment