A 2D collision detection library for modern HTML5 games by Richard Marks. Licensed under MIT. Copyright 2016, Richard Marks
A small ES6 API for detecting 2D collisions between objects.
- Rectangle vs Rectangle
- Circle vs Circle
- Rectangle vs Circle
- Alpha (pixel perfect)
- Alpha vs Rectangle
- Alpha vs Circle
- Alpha vs Alpha
Install into your project with
npm install c2d --save-dev
Create colliders and test for collisions between two of them. Could not be any easier.
import {api as c2d} from 'c2d';
// create colliders
// displayObject should be an EaselJS DisplayObject instance
const boxCollider = c2d.createBoxCollider({displayObject, width: 128, height: 128});
const circleCollider = c2d.createCircleCollider({displayObject, radius: 64});
const alphaCollider = c2d.createAlphaCollider({displayObject});
// test collision between two objects
let objectsCollided = c2d.collided(colliderA, colliderB);
if (objectsCollided) {
console.log(`There was a collision`);
}
There are more functions available in Collision2D for advanced users. If you want to use the full API, simply change your import to the following
import {fullAPI as c2d} from 'c2d';
You now have access to everything in Collision2D.
Version 2.2 - last updated Apr 27, 2016