This demo demonstrate basic collision detection with Shaku.collision manager, using collision flags / mask (will only collide with matching colors).
Move the shape with your mouse to test collision, press 1-4 to replace the shape, and + / - to set size.
Click on shapes to randomize their flags.
The following shows a minimal code example for collision detection with collision flags.
// create collision world
let world = Shaku.collision.createWorld(128);
// add point to world with collision flags of 0001
let point = new Shaku.collision.PointShape(new Shaku.utils.Vector2(125, 65));
point.collisionFlags = 1;
world.addShape(point);
// add a rectangle to world with collision flags of 0010
let rect = new Shaku.collision.RectangleShape(new Shaku.utils.Rectangle(45, 75, 100, 50));
rect.collisionFlags = 1 << 1;
world.addShape(rect);
// add a circle to world with collision flags of 0100
let circle = new Shaku.collision.CircleShape(new Shaku.utils.Circle(new Shaku.utils.Vector2(300, 315), 150));
circle.collisionFlags = 1 << 2;
world.addShape(circle);
// add a line to world with collision flags of 1000
let lines = new Shaku.collision.LinesShape(new Shaku.utils.Line(new Shaku.utils.Vector2(100, 100), new Shaku.utils.Vector2(150, 150)));
lines.collisionFlags = 1 << 3;
world.addShape(lines);
// create a circle at mouse position with radius of 100. we'll use it to check collisions
let shapeToTest = new Shaku.collision.CircleShape(new Shaku.utils.Circle(Shaku.input.mousePosition, 100));
// test a single result, sorted by distance, only on shapes that have collision flag bit 0001
let collisionSingle = world.testCollision(shapeToTest, true, 1);
if (collisionSingle) {
console.log(collisionSingle.first, collisionSingle.second);
}
// test many results, not sorted, only on shapes that have collision flag bits 0001 or 0101
let collisions = world.testCollisionMany(shapeToTest, false, 5);
for (let i = 0; i < collisions.length; ++i) {
console.log(collisions[i].first, collisions[i].second);
}