Giter Club home page Giter Club logo

voxel-aabb-sweep's Introduction

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

voxel-aabb-sweep's People

Contributors

fenomas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

terrac

voxel-aabb-sweep's Issues

Collisions Not Working For Negative Positions

I was just giving this a few tests and noticed that negative coordinates can cause the collision to stop working correctly. A slightly modified example should help:

var sweep = require('voxel-aabb-sweep')
var getVoxel = function(x,y,z) { return y == 0 && x == 0 && z == 0 }
var box = { base: [-50,10,-50], max: [-20,20,-20], translate: function() {} }
var vector = [ 0, -10, 0 ]
var dist = sweep( getVoxel, box, vector, function() {
    return true
})

console.log(dist);
// this returns 20 when it should only return 9

Question about using this vs a naive axis-by-axis sweep

Hi, when making the changes for fenomas/voxel-physics-engine#4 I found myself wondering the practical effect of using this instead of a naive axis-by-axis sweep. Did you implement this as a theoretical idea or did you find in practice that the axis-by-axis sweep felt "off"/wonky when moving?

I ask as I need to (re-)implement preventing a player from falling off the edge of blocks and while the exact logic will be different, it seems to me similar tradeoffs apply (complexity vs "feel")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.