Skip to content

Commit

Permalink
ray vs AABB check
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Jun 30, 2015
1 parent 6dc0f3b commit e26f48e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/collision/AABB.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,44 @@ AABB.prototype.toWorldFrame = function(frame, target){

return target.setFromPoints(corners);
};

/**
* Check if the AABB is hit by a ray.
* @param {Ray} ray
* @return {number}
*/
AABB.prototype.overlapsRay = function(ray){
var t = 0;

// ray.direction is unit direction vector of ray
var dirFracX = 1 / ray._direction.x;
var dirFracY = 1 / ray._direction.y;
var dirFracZ = 1 / ray._direction.z;

// this.lowerBound is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
var t1 = (this.lowerBound.x - ray.from.x) * dirFracX;
var t2 = (this.upperBound.x - ray.from.x) * dirFracX;
var t3 = (this.lowerBound.y - ray.from.y) * dirFracY;
var t4 = (this.upperBound.y - ray.from.y) * dirFracY;
var t5 = (this.lowerBound.z - ray.from.z) * dirFracZ;
var t6 = (this.upperBound.z - ray.from.z) * dirFracZ;

// var tmin = Math.max(Math.max(Math.min(t1, t2), Math.min(t3, t4)));
// var tmax = Math.min(Math.min(Math.max(t1, t2), Math.max(t3, t4)));
var tmin = Math.max(Math.max(Math.min(t1, t2), Math.min(t3, t4)), Math.min(t5, t6));
var tmax = Math.min(Math.min(Math.max(t1, t2), Math.max(t3, t4)), Math.max(t5, t6));

// if tmax < 0, ray (line) is intersecting AABB, but whole AABB is behing us
if (tmax < 0){
//t = tmax;
return false;
}

// if tmin > tmax, ray doesn't intersect AABB
if (tmin > tmax){
//t = tmax;
return false;
}

return true;
};

0 comments on commit e26f48e

Please sign in to comment.