planck-deterministic/example/CollisionFiltering.ts

176 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

/*
2024-11-23 16:32:23 +03:30
* Copyright (c) Erin Catto
* Licensed under the MIT license
*/
// This is a test of collision filtering.
// There is a triangle, a box, and a circle.
// There are 6 shapes. 3 large and 3 small.
// The 3 small ones always collide.
// The 3 large ones never collide.
// The boxes don't collide with triangles (except if both are small).
2024-12-23 18:24:21 +03:30
import { World, Edge, Polygon, Box, Circle, PrismaticJoint, Testbed } from "planck";
2024-12-18 14:18:45 +03:30
const SMALL_GROUP = 1;
const LARGE_GROUP = -1;
2024-12-18 14:18:45 +03:30
const TRIANGLE_CATEGORY = 0x0002;
const BOX_Category = 0x0004;
const CIRCLE_CATEGORY = 0x0008;
2024-12-18 14:18:45 +03:30
const TRIANGLE_MASK = 0xffff;
const BOX_MASK = 0xffff ^ TRIANGLE_CATEGORY;
const CIRCLE_MAX = 0xffff;
2024-12-24 15:07:16 +03:30
const world = new World({
gravity: { x: 0, y: -10 },
});
2023-08-20 03:24:33 -07:00
const testbed = Testbed.mount();
testbed.start(world);
// Ground body
2024-12-24 15:07:16 +03:30
const ground = world.createBody({
type: "static",
});
2024-12-24 19:58:07 +03:30
ground.createFixture({
shape: new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }),
2024-12-18 14:18:45 +03:30
friction: 0.3,
});
// Small triangle
2023-08-14 13:59:23 -07:00
const smallTriangle = {
density: 1.0,
filterCategoryBits: TRIANGLE_CATEGORY,
filterMaskBits: TRIANGLE_MASK,
filterGroupIndex: SMALL_GROUP,
};
2024-12-18 14:18:45 +03:30
const body1 = world.createBody({
type: "dynamic",
2024-12-23 18:24:21 +03:30
position: { x: -5.0, y: 2.0 },
});
body1.createFixture(
2024-12-23 18:24:21 +03:30
new Polygon([
{ x: -1.0, y: 0.0 },
{ x: 1.0, y: 0.0 },
{ x: 0.0, y: 2.0 },
]),
smallTriangle,
);
2024-12-24 15:07:16 +03:30
// Large triangle
2023-08-14 13:59:23 -07:00
const largeTriangle = {
density: 1.0,
filterCategoryBits: TRIANGLE_CATEGORY,
filterMaskBits: TRIANGLE_MASK,
filterGroupIndex: LARGE_GROUP,
};
2024-12-18 14:18:45 +03:30
const body2 = world.createBody({
type: "dynamic",
2024-12-23 18:24:21 +03:30
position: { x: -5.0, y: 6.0 },
fixedRotation: true, // look at me!
});
body2.createFixture(
2024-12-23 18:24:21 +03:30
new Polygon([
{ x: -2.0, y: 0.0 },
{ x: 2.0, y: 0.0 },
{ x: 0.0, y: 4.0 },
]),
largeTriangle,
);
2024-12-24 15:07:16 +03:30
const body = world.createBody({
type: "dynamic",
position: { x: -5.0, y: 10.0 },
});
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: new Box(0.5, 1.0),
density: 1.0,
});
world.createJoint(
2024-12-18 14:18:45 +03:30
new PrismaticJoint({
bodyA: body2,
bodyB: body,
enableLimit: true,
2024-12-23 18:24:21 +03:30
localAnchorA: { x: 0.0, y: 4.0 },
localAnchorB: { x: 0, y: 0 },
localAxisA: { x: 0.0, y: 1.0 },
2024-12-18 14:18:45 +03:30
lowerTranslation: -1.0,
upperTranslation: 1.0,
}),
);
// Small box
2023-08-14 13:59:23 -07:00
const smallBox = {
density: 1.0,
restitution: 0.1,
2023-08-14 13:59:23 -07:00
filterCategoryBits: BOX_Category,
filterMaskBits: BOX_MASK,
filterGroupIndex: SMALL_GROUP,
};
2024-12-24 15:07:16 +03:30
const body3 = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 2.0 },
});
2024-12-24 19:58:07 +03:30
body3.createFixture({
shape: new Box(1.0, 0.5),
...smallBox,
});
2024-12-24 15:07:16 +03:30
// Large box
2023-08-14 13:59:23 -07:00
const largeBox = {
density: 1.0,
restitution: 0.1,
2023-08-14 13:59:23 -07:00
filterCategoryBits: BOX_Category,
filterMaskBits: BOX_MASK,
filterGroupIndex: LARGE_GROUP,
};
2024-12-24 15:07:16 +03:30
const body4 = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 6.0 },
});
2024-12-24 19:58:07 +03:30
body4.createFixture({
shape: new Box(2.0, 1.0),
...largeBox,
});
// Small circle
2023-08-14 13:59:23 -07:00
const smallCircle = {
density: 1.0,
2023-08-14 13:59:23 -07:00
filterCategoryBits: CIRCLE_CATEGORY,
filterMaskBits: CIRCLE_MAX,
filterGroupIndex: SMALL_GROUP,
};
2024-12-24 15:07:16 +03:30
const body5 = world.createBody({
type: "dynamic",
position: { x: 5.0, y: 2.0 },
});
2024-12-24 19:58:07 +03:30
body5.createFixture({
shape: new Circle(1.0),
...smallCircle,
});
// Large circle
2023-08-14 13:59:23 -07:00
const largeCircle = {
density: 1.0,
2023-08-14 13:59:23 -07:00
filterCategoryBits: CIRCLE_CATEGORY,
filterMaskBits: CIRCLE_MAX,
filterGroupIndex: LARGE_GROUP,
};
2024-12-24 15:07:16 +03:30
const body6 = world.createBody({
type: "dynamic",
position: { x: 5.0, y: 6.0 },
});
2024-12-24 19:58:07 +03:30
body6.createFixture({
shape: new Circle(2.0),
...largeCircle,
});