Skip to content

Commit

Permalink
-- Fixed an issue with AABBs creating with a 'vec' argument having
Browse files Browse the repository at this point in the history
negative values. Added 'touches' to test if two boxes touch but do not overlap.
Unit tests were added to reflect these changes.

Signed off by Mark Farrell <markfarr2011@gmail.com>
  • Loading branch information
markfarrell committed Dec 22, 2013
1 parent 800c92f commit f752625
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
24 changes: 21 additions & 3 deletions index.js
Expand Up @@ -3,17 +3,20 @@ module.exports = AABB
var vec3 = require('gl-matrix').vec3

function AABB(pos, vec) {

if(!(this instanceof AABB)) {
return new AABB(pos, vec)
}

this.base = pos
var pos2 = vec3.create()
vec3.add(pos2, pos, vec)

this.base = vec3.min(vec3.create(), pos, pos2)
this.vec = vec
this.max = vec3.max(vec3.create(), pos, pos2)

this.mag = vec3.length(this.vec)

this.max = vec3.create()
vec3.add(this.max, this.base, this.vec)
}

var cons = AABB
Expand Down Expand Up @@ -83,6 +86,17 @@ proto.intersects = function(aabb) {
return true
}

proto.touches = function(aabb) {

var intersection = this.union(aabb);

return (intersection !== null) &&
((intersection.width() == 0) ||
(intersection.height() == 0) ||
(intersection.depth() == 0))

}

proto.union = function(aabb) {
if(!this.intersects(aabb)) return null

Expand All @@ -95,3 +109,7 @@ proto.union = function(aabb) {

return new AABB([base_x, base_y, base_z], [max_x - base_x, max_y - base_y, max_z - base_z])
}




26 changes: 26 additions & 0 deletions test.js
Expand Up @@ -91,5 +91,31 @@ test('intersects works', function(t) {
b1 = aabb([12, 12, 12], [4, 4, 4])
t.equals(b0.intersects(b1), true, 'should intersect (b0 contains b1)')

b1 = aabb([-5, -5, -5], [20, 20, 20])
t.equals(b0.intersects(b1), true, 'should intersects (b0 contains b1)')

b1 = aabb([-5, -5, -5], [10, 10, 10])
t.equals(b0.intersects(b1), false, 'should not intersect (b0 does not contain b1)')

t.end()
})

test('touches works', function(t) {
var b0 = aabb([10,10,10], [10, 10, 10])
, b1 = aabb([0, 0, 0], [10, 10, 10])

t.equals(b0.touches(b1), true, 'should touch')

b1 = aabb([10, 0, 0], [10, 10, 10])
t.equals(b0.touches(b1), true, 'should touch')

b1 = aabb([0, 10, 0], [10, 10, 10])
t.equals(b0.touches(b1), true, 'should touch')

b1 = aabb([0, 0, 10], [10, 10, 10])
t.equals(b0.touches(b1), true, 'should touch')

b1 = aabb([-10, -10, -10], [10, 10, 10])
t.equals(b0.touches(b1), false, 'should not touch')

})

0 comments on commit f752625

Please sign in to comment.