test if a ray intersects an aabb in 3d space
Clone or download
Latest commit d9e33ce Oct 25, 2015
Type Name Latest commit message Commit time
Failed to load latest commit information.
demo bit of cleanup in raytracer demo Oct 18, 2015
.gitignore add fast/ray aabb isect Feb 6, 2015
.npmignore add fast/ray aabb isect Feb 6, 2015
LICENSE.txt rename to ray-aabb and work up readme Jan 30, 2015
README.md doc clarity Oct 18, 2015
bench.js cleanup bench.js Oct 18, 2015
package.json 3.0.2 Oct 25, 2015
ray-aabb.js add caching for rays with repeated ro + rd Oct 24, 2015
test.js automated test generation for normals Oct 17, 2015



test if a ray intersects an aabb in 2d/3d space

Implemented via the techniques described in Fast Ray/Axis-Aligned Bounding Box Overlap Tests using Ray Slopes


npm install ray-aabb


var createRay = require('ray-aabb');

                   /      /|
(-1, 1, 0) ---->  +------+ |
                  |      | +
                  |      |/

var ray_origin = [-1, 1, 0];
var ray_direction = [1, 0, 0];
var ray = createRay(ray_origin, ray_direction);

var box = [
  [0, 0, 0],
  [2, 2, 2]

// outputs: true

// avoid allocating new memory by reusing rays
ray.update(ray_origin, [-1, 0, 0]);

// outputs: false

var normal = [0, 0, 0];
var d = ray.intersects(box, normal);
// outputs: 1

// outputs: [ -1, 0, 0 ]

api surface

all vectors specified are arrays in the format: [x, y, z] with z being optional for 2d vectors

createRay(ray_origin, ray_direction)


  • ray_origin - a vector defining the ray origin
  • ray_direction - a normalized vector defining the ray direction returns: a Ray instance

Ray#update(ray_origin, ray_direction)

Allows Ray instances to be reused by precomputing ray classification. The intention here is that you will be casting a ray against many aabbs

parameters: same as createRay

returns: this (e.g. ray.update(ro, rd).intersects(box)

Ray#intersects(aabb[, normal]) where aabb specifies the corners of the bounding box:

[[x1, y1, z1], [x2, y2, z2]]

and the optional normal argument is a 2d/3d vector (e.g., [0, 0]) that will be populated with the non-normalized normal of the corner/edge/face that the ray intersected with.


if normal is not passed

  • true if intersection detected
  • false if no intersection

if normal is passed:

  • false if no intersection or a number denoting how far along the ray the collision occurred. You can use this number to compute the point of intersection. See the demos for example usage.


node and evergreen browsers using browserify




npm run demo-2d

3d (software raytracer)


npm run demo-raytracer