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

distributedlife-sat

v1.4.0

Published

Library for performing 2D collision detection

Downloads

25

Readme

distributedlife-sat

This project is a rewrite of Jim Riecken's SAT.js library. I rewrote it on a flight back to see family in coding style that suites me. I also trimmed out stuff I don't think I need. It's not compatible due to API changes but all the tests still pass.

About

SAT.js is a simple JavaScript library for performing collision detection (and projection-based collision response) of simple 2D shapes. It uses the Separating Axis Theorem (hence the name)

It supports detecting collisions between:

  • Circles (using Voronoi Regions.)
  • Convex Polygons (and simple Axis-Aligned Boxes, which are of course, convex polygons.)

It also supports checking whether a point is inside a circle or polygon.

When a collision occurs your callback gets invoked.

It's released under the MIT license.

To use it in node.js, you can run npm install distributedlife-sat and then use it with var SAT = require('distributedlife-sat');

Functions

I took a more functional approach than the original. All you have is functions and data. Functions return new versions of data, so through away your old stuff.

The original SAT.js library has a bunch of performance stuff around limiting reallocs, etc. I have stripped all that out as using this for high-performance stuff isn't an objective.

Collisions

I collapsed the original set of functions that run different algorithms depending on whether you have one or two circles, polygons or points into a single function. This works out what structure you have and calls the appropriate algorithm.

  • if you have a radius, you're a circle
  • if you have an x and a y, you're a vector
  • otherwise, you're a polygon
function callMeOnCollision () {
  console.log('ouch');
}

var test = require('distributedlife-sat').collisions.test;
test(a, b, callMeOnCollision)

At present we call your callback on collision and you get no further information about who was doing, what direction they were heading, etc.

Shapes

Inside is a helper file with a bunch of shapes. These make it easier for you to create objects for colliding. These objects exist in your physics models. I make no promises about how easy it may be to render them. I like to keep my physics model and my rendering model separate.

Most shapes take a point that is their anchor in world space. For the circle, this will be the center. For the rectangles you have the option of a centre oriented c object or a top-left tl oriented one.

var shapes = require('distributedlife-sat').shapes;

var origin = {x: 0, y: 0};
var width = 25;
var height = 25;

circle(origin, 25);
tlSquare(origin, width);
tlBox(origin, width, height);
tlRectangle(origin, width, height);
cSquare(origin, width);
cBox(origin, width, height);
cRectangle(origin, width, height);
polygon(points, [{x: 0, y: 0}, {x: 100, y: 100}, {x: 0, y: 100}]);
triangle(origin, width);

The box and rectangles are synonyms.

Useful Vector Functions

These functions return new vectors and without modifying the supplied vectors.

  • sub (v1, v2) - subtract v2 from v1 and return the new vector
  • add (v1, v2) - add v2 to v1 and return the new vector
  • dot (v1, v2) - return the dot product of v1 and v2
  • len2 (v) - return dot product of the vector and itself
  • len (v) - return the magnitude of the vector
  • normalise (v) - return the normal of the vector
  • scale (v, x, y) - scale v1 by x and y. If y is not supplied it scales v.x and v.y by the x value.
  • perp (v) - return the vector perpendicular to the supplied vector
  • rotate (v, angle) - return the vector that is a rotate of angle on v
  • reverse (v) - return the reverse of the vector
  • project (v1, v2) - project v1 on v2.
  • reflect (v, axis) - reflect the vector along the supplied axis

Useful Polygon Functions

All of these functions return new polygons and do not modify the supplied polygons.

  • rotate (polygon, angle) - rotate polygon by angle
  • translate (polygon, vector) - translate polygon by vector
  • setPoints (polygon, points) - set the points of the polygon
  • setOffset (polygon, offset) - change the offset of the polygon from it's world origin
  • setAngle (polygon, angle) - set the angle of the polygon
  • getAABB (polygon) - return the Axis-Aligned Bounding Box for the polygon

Useful Circle Functions

  • getAABB (circle) - return the Axis Aligned Bounding Box for the supplied circle.

Tests

To run the tests from your console:

mocha

To install mocha you will need to have run npm install after cloning the repo.