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