planck-deterministic/example/PolyCollision.ts

76 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

/*
2024-11-23 16:32:23 +03:30
* Copyright (c) Erin Catto
* Licensed under the MIT license
*/
2024-12-23 18:24:21 +03:30
import { World, Transform, Manifold, CollidePolygons, Box, Testbed } from "planck";
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.info("Use arrow keys to move and Z or X to rotate.");
testbed.start(world);
2024-12-18 14:18:45 +03:30
const polygonA = new Box(2, 4);
2024-12-23 18:24:21 +03:30
const transformA = new Transform({ x: 0.0, y: 0.0 }, 0.0);
2024-12-18 14:18:45 +03:30
const polygonB = new Box(5, 5);
2024-12-23 18:24:21 +03:30
const positionB = { x: 5, y: 4 };
2023-08-14 13:59:23 -07:00
let angleB = 1.9160721;
2024-12-18 14:18:45 +03:30
const transformB = new Transform(positionB, angleB);
testbed.step = function () {
2024-12-18 14:18:45 +03:30
const manifold = new Manifold();
new CollidePolygons(manifold, polygonA, transformA, polygonB, transformB);
2024-12-18 14:18:45 +03:30
const worldManifold = manifold.getWorldManifold(
null,
transformA,
polygonA.getRadius(),
transformB,
polygonB.getRadius(),
);
testbed.status("point count", manifold.pointCount);
2024-12-18 14:18:45 +03:30
const vA = polygonA.m_vertices.map((v) => Transform.mul(transformA, v));
testbed.drawPolygon(vA, testbed.color(0.9, 0.9, 0.9));
2024-12-18 14:18:45 +03:30
const vB = polygonB.m_vertices.map((v) => Transform.mul(transformB, v));
testbed.drawPolygon(vB, testbed.color(0.9, 0.9, 0.9));
2023-08-14 13:59:23 -07:00
for (let i = 0; i < manifold.pointCount; ++i) {
testbed.drawPoint(worldManifold.points[i], 4.0, testbed.color(0.9, 0.3, 0.3));
}
};
testbed.keydown = function () {
if (testbed.activeKeys["left"]) {
positionB.x -= 0.2;
}
if (testbed.activeKeys["right"]) {
positionB.x += 0.2;
}
if (testbed.activeKeys["down"]) {
positionB.y -= 0.2;
}
if (testbed.activeKeys["up"]) {
positionB.y += 0.2;
}
if (testbed.activeKeys["Z"]) {
angleB += 0.2;
}
if (testbed.activeKeys["X"]) {
angleB -= 0.2;
}
transformB.set(positionB, angleB);
};