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

@deltasnare/physics2d-collision

v1.0.0

Published

Polygon-to-polygon collision calculator for Deltasnare's physics engine

Downloads

1

Readme

Polygon-to-polygon collision calculator for Deltasnare's physics engine

Usage

const collision = require('@deltasnare/physics2d-collision')
const { mat2d } = require('gl-matrix')

// create some collider models
const target = collision.create([[0, 5], [-2, 0], [0, -5], [2, 0]])
const near = collision.create([[1, 3], [1, -3], [4, -3], [4, 3]])
const far = collision.create([[3, 3], [3, -3], [6, -3], [6, 3]])

// transform models
const transformed = collision.transform(target, mat2d.fromTranslation([], [-1, -5]))

// test for collisions
const nearCollision = collision.check(target, near) // { depth: 1, normal: [1, 0] }
const farCollision = collision.check(target, far) // null

The collision module exports three functions:

collision.create(vertices)

Creates a collider model. The argument is an array of vertices in vec2 format ([x, y]). The vertices must define a convex mesh in counter-clockwise order. Failing to comply with this assumption may lead to undefiend behavior and/or the physics engine blowing up. For the structure of the returned collider model, see below.

collision.transform(model, transform)

Transforms a collider model with a 2D matrix, returning the resulting model. The model argument must be a collider model created by collision.create() or compatible with it, and transform is a mat2d, a 3x2 2D transformation matrix.

collision.check(first, second)

Checks if two models collide. Both arguments must be collider models created by collision.create() or compatible with it. Returns null if the objects do not collide. If they do, it returns a collision in the following format:

{ depth: 1, normal: [1, 0] }

Where

  • depth is the penetration depth (how far the objects overlap)
  • normal is the collision normal, the direction of the penetration. This vector is normalized.

Collider Models

Collider models are the basic models that can be collided with this module. Here is an example:

{ vertices: [ [ 0, 5 ], [ -2, 0 ], [ 0, -5 ], [ 2, 0 ] ],
  normals:
   [ [ -0.9284766908852593, 0.3713906763541037 ],
     [ -0.9284766908852593, -0.3713906763541037 ],
     [ 0.9284766908852593, -0.3713906763541037 ],
     [ 0.9284766908852593, 0.3713906763541037 ] ],
  boundingBox: { min: [ -2, -5 ], max: [ 2, 5 ] } }

Collider models are objects, with the following layout:

  • vertices is an array of vec2 which define a convex polygon in counter-clockwise order
  • normals is an array of vec2 where each member normals[i] corresponds to the outer normal of the edge between vertices[i] and vertices[i + 1]
  • boundingBox defines an axis-aligned bounding box of the mesh using an object, where:
    • min is a vec2, containing the lowest coordinates of the box in each axis, and
    • max is a vec2, containing the highest coordinates of the box in each axis