Skip to content
Browse files

Merge pull request #68 from asalga/bug66

Added AABB-AABB intersection test along with a unit test.
  • Loading branch information...
2 parents ca78aa1 + 906c7ce commit abab3fa0c5814b72a3f6ecf28aac1c4791cf236c @cjcliffe committed Aug 2, 2012
Showing with 97 additions and 0 deletions.
  1. +35 −0 source/CubicVR.Math.js
  2. +62 −0 tests/unit/test_aabb.html
View
35 source/CubicVR.Math.js
@@ -850,6 +850,41 @@ CubicVR.RegisterModule("Math",function (base) {
var y = aabb[0][1] < aabb[1][1] ? aabb[1][1] - aabb[0][1] : aabb[0][1] - aabb[1][1];
var z = aabb[0][2] < aabb[1][2] ? aabb[1][2] - aabb[0][2] : aabb[0][2] - aabb[1][2];
return [x,y,z];
+ },
+ /**
+ returns
+ 0 if AABBs do not intersect
+ 1 if box1 is inside box2
+ 2 if box2 is inside box1
+ 3 if AABBs are disjoint
+ */
+ intersectsAABB: function (box1, box2) {
+ // box1 is inside box2.
+ if( box1[0][0] >= box2[0][0] && box1[1][0] <= box2[1][0] &&
+ box1[0][1] >= box2[0][1] && box1[1][1] <= box2[1][1] &&
+ box1[0][2] >= box2[0][2] && box1[1][2] <= box2[1][2]){
+ return 1;
+ }
+ // box2 is inside box1.
+ if( box2[0][0] >= box1[0][0] && box2[1][0] <= box1[1][0] &&
+ box2[0][1] >= box1[0][1] && box2[1][1] <= box1[1][1] &&
+ box2[0][2] >= box1[0][2] && box2[1][2] <= box1[1][2]){
+ return 2;
+ }
+
+ // Disjoint
+ if( box1[0][0] > box2[1][0] || box1[1][0] < box2[0][0] ){
+ return 0;
+ }
+ if( box1[0][1] > box2[1][1] || box1[1][1] < box2[0][1] ){
+ return 0;
+ }
+ if( box1[0][2] > box2[1][2] || box1[1][2] < box2[0][2] ){
+ return 0;
+ }
+
+ // Otherwise AABB's intersect.
+ return 3;
}
};
View
62 tests/unit/test_aabb.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
+<html>
+
+ <head>
+ <title>
+ CubicVR.js: AABB Collision Test
+ </title>
+ <script src="../../CubicVR.js" type="text/javascript">
+ </script>
+ <script type='text/javascript'>
+ var test = 1,
+ failed = false;
+
+ function checkEqual(a, b){
+ if(a !== b && !failed){
+ alert('Test #' + test + ' failed.');
+ failed = true;
+ }
+ test++;
+ }
+
+ function runTests(gl, canvas) {
+ var res;
+
+ // box1 inside box2
+ res = CubicVR.aabb.intersectsAABB([[-1, -1, -1],[1, 1, 1]],
+ [[-2, -2, -2],[2, 2, 2]]);
+ checkEqual(res, 1);
+
+ // box2 inside box1
+ res = CubicVR.aabb.intersectsAABB([[-2, -2, -2],[2, 2, 2]],
+ [[-1, -1, -1],[1, 1, 1]]);
+ checkEqual(res, 2);
+
+ // disjoint
+ res = CubicVR.aabb.intersectsAABB([[ 1, 1, 1],[ 2, 2, 2]],
+ [[-2, -2, -2],[-1, -1, -1]]);
+ checkEqual(res, 0);
+
+ // disjoint
+ res = CubicVR.aabb.intersectsAABB([[ 1, 1, 1],[ 2, 2, 2]],
+ [[-2, -2, -2],[-1, -1, -1]]);
+ checkEqual(res, 0);
+
+ // disjoint
+ res = CubicVR.aabb.intersectsAABB([[ 1, 1, 1],[2, 2, 2]],
+ [[ 1, 3, 1],[2, 4, 2]]);
+ checkEqual(res, 0);
+
+ // intersection
+ res = CubicVR.aabb.intersectsAABB([[-1, -1, -1],[1, 1, 1]],
+ [[ 1, 1, 1],[2, 2, 2]]);
+ checkEqual(res, 3);
+
+ alert('All tests passed');
+ }
+ </script>
+ </head>
+
+ <body onLoad="CubicVR.start('auto',runTests);"></body>
+
+</html>

0 comments on commit abab3fa

Please sign in to comment.
Something went wrong with that request. Please try again.