npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ray-aabb

v3.0.2

Published

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

Downloads

49

Readme

ray-aabb

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

install

npm install ray-aabb

use

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]
];

console.log(ray.intersects(box));
// outputs: true

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

console.log(ray.intersects(box));
// outputs: false

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

console.log(normal);
// 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)

parameters:

  • 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.

returns

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.

platforms

node and evergreen browsers using browserify

Demos

2d

random-ray

npm run demo-2d

3d (software raytracer)

random-ray

npm run demo-raytracer

license

MIT