Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,21 @@
}

return new_rs;
}
},

// Arvo's algorithm
// Arvo, James, A Simple Method for Box-Sphere Intersection Testing, p. 335-339, code: p. 730-732, BoxSphere.c.
// solid-solid case
// `sphere` should be Array [ x, y, z, radius ]
intersectWithSphere: function( sphere ) {
var dmin = 0;
for ( var i = 0; i < this._numDimensions; i++ ) {
if ( sphere[i] < this.min[i] ) dmin += Math.pow( ( sphere[i] - this.min[i] ), 2 );
if ( sphere[i] > this.max[i] ) dmin += Math.pow( ( sphere[i] - this.max[i] ), 2 );
}
return dmin <= Math.pow( sphere[ this._numDimensions ], 2 )
}
};

return AABB;
}));
}));
18 changes: 17 additions & 1 deletion test/aabb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ vows.describe('AABB').addBatch({
var t = new AABB([-6, -3], [6, 2])
assert.isFalse (aabb.contained(t));
},
'circle intersects': function( aabb ) {
var c = [ 0, 0, 1 ];
assert.isTrue( aabb.intersectWithSphere( c ) );
},
'circle does not intersect': function( aabb ) {
var c = [ 10, 10, 1 ];
assert.isFalse( aabb.intersectWithSphere( c ) );
}
},
'Testing 3D AABB': {
topic: function(){ return new AABB([-5, -2, -5], [5, 3, 5]); },
Expand Down Expand Up @@ -104,6 +112,14 @@ vows.describe('AABB').addBatch({
{a: 0, b: 0}
];
assert.isFalse (aabb.intersectWithSegment(s));
}
},
'sphere intersects': function( aabb ) {
var s = [ 0, 0, 0, 1 ];
assert.isTrue( aabb.intersectWithSphere( s ) );
},
'sphere does not intersect': function( aabb ) {
var s = [ 10, 10, 10, 1 ];
assert.isFalse( aabb.intersectWithSphere( s ) );
}
}
}).export(module);