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

voxel-aabb-sweep

v0.5.0

Published

Sweep an AABB through voxels or a tileset and find collisions

Downloads

255

Readme

voxel-aabb-sweep

Sweep an AABB along a vector and find where it collides with a set of voxels.

There are other libraries that do this naively, by sweeping the AABB along each axis in turn, but this is inaccurate for larger movements, and it gives anisotropic results (i.e. it prefers to collide in some axes over others).

In contrast this library essentially raycasts along the the AABB's leading corner, and each time the ray crosses a voxel boundary, it checks for collisions across the AABB's leading face in that axis. This gives correct results even across long movements, with reasonably solid performance.

The raycasting algorithm is from fast-voxel-raycast.

Installation

npm install voxel-aabb-sweep

Usage

var sweep = require('voxel-aabb-sweep')
var callback = function (dist, axis, dir, vec) { /* .. */ }
var distance = sweep(getVoxels, box, vector, callback, noTranslate, epsilon)
  • distance - the total scalar distance the AABB moved during the sweep
  • getVoxel - a function(x,y,z) that returns a truthy value for voxels that collide the AABB
  • box - an object shaped like an aabb-3d
  • vector - vector along which the AABB is to move. E.g. [5, 10, -3]
  • callback - A function that will get called when a collision occurs.
  • noTranslate - (default false) If true, the AABB will not be translated to its new position.
  • epsilon - (default 1e-10) Rounding factor by which an AABB must cross a voxel boundary to count

The collision callback:

  • dist - the scalar distance moved so far in the sweep
  • axis - the axis in which a collision occured
  • dir - movement direction (1 or -1) along the collision axis
  • vec - the vector distance remaining to be moved (can and probably should be updated by the callback)
  • return value: if you return true the sweep will end at the collision; otherwise it will continue along vec

Once the sweep ends, box will be moved to its final position via its translate() method.

Example

var sweep = require('voxel-aabb-sweep')
var getVoxel = function(x,y,z) { return (y > 5) }
var box = { base: [0,0,0], max: [1,1,1], translate: function() {} }
var vector = [ 5, 10, -4 ]
var dist = sweep( getVoxel, box, vector, function() {
    return true
})
// dist: 5.937171043518958

How to use this library

To use this like a volumetric raycast, and simply find where the first collision occurs, you'll want to return true in the callback (like the example above).

To use this as you might in a physics engine, where collisions mean the AABB stops moving in the obstructed direction, but continues along the other axes, you'll probably want to zero out the obstructed component of the vec parameter (which signfies how far the AABB has left to sweep) and let the sweep continue:

var dist = sweep( getVoxel, box, vector, function(dist, axis, dir, vec) {
    vec[axis] = 0
    return false
})

You could also do something more complicated, like bouncing back along the obstructed axis, etc.

Hacking

# clone this repo
cd voxel-aabb-sweep
npm install     # get dev dependencies
npm test        # run tests

License

© 2016 Andy Hall, MIT license