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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// This is used to test sensor shapes.
|
|
|
|
|
|
2024-12-15 13:30:32 +03:30
|
|
|
import { World, Vec2, Edge, 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();
|
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
|
|
|
let breakVelocity: Vec2;
|
|
|
|
|
let breakAngularVelocity: number;
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-08-14 13:59:23 -07:00
|
|
|
let broke = false;
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-07-31 21:47:06 -07:00
|
|
|
// 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 }),
|
|
|
|
|
density: 0.0,
|
|
|
|
|
});
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-07-31 21:47:06 -07:00
|
|
|
// Breakable dynamic body
|
2024-12-24 15:07:16 +03:30
|
|
|
const body1 = world.createBody({
|
|
|
|
|
type: "dynamic",
|
|
|
|
|
position: { x: 0.0, y: 40.0 },
|
|
|
|
|
angle: 0.25 * Math.PI,
|
|
|
|
|
});
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2024-12-23 18:24:21 +03:30
|
|
|
const shape1 = new Box(0.5, 0.5, { x: -0.5, y: 0.0 }, 0.0);
|
2024-12-24 19:58:07 +03:30
|
|
|
const piece1 = body1.createFixture({
|
|
|
|
|
shape: shape1,
|
|
|
|
|
density: 1.0,
|
|
|
|
|
});
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2024-12-23 18:24:21 +03:30
|
|
|
const shape2 = new Box(0.5, 0.5, { x: 0.5, y: 0.0 }, 0.0);
|
2024-12-24 19:58:07 +03:30
|
|
|
let piece2 = body1.createFixture({
|
|
|
|
|
shape: shape2,
|
|
|
|
|
density: 1.0,
|
|
|
|
|
});
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2024-12-15 13:30:32 +03:30
|
|
|
world.on("post-solve", function (contact, impulse) {
|
2023-07-31 21:47:06 -07:00
|
|
|
if (broke) {
|
|
|
|
|
// The body already broke.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-07-31 21:47:06 -07:00
|
|
|
// Should the body break?
|
2024-12-18 14:18:45 +03:30
|
|
|
const count = contact.getManifold().pointCount;
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-08-14 13:59:23 -07:00
|
|
|
let maxImpulse = 0.0;
|
|
|
|
|
for (let i = 0; i < count; ++i) {
|
2023-07-31 21:47:06 -07:00
|
|
|
maxImpulse = Math.max(maxImpulse, impulse.normalImpulses[i]);
|
|
|
|
|
}
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-07-31 21:47:06 -07:00
|
|
|
if (maxImpulse > 40.0) {
|
2025-06-05 22:53:16 +03:30
|
|
|
setTimeout(() => {
|
|
|
|
|
broke = true;
|
|
|
|
|
breakIt();
|
|
|
|
|
});
|
2023-07-31 21:47:06 -07:00
|
|
|
}
|
|
|
|
|
});
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2024-12-24 17:41:58 +03:30
|
|
|
function breakIt() {
|
2023-07-31 21:47:06 -07:00
|
|
|
// Create two bodies from one.
|
2024-12-18 14:18:45 +03:30
|
|
|
const center = body1.getWorldCenter();
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-07-31 21:47:06 -07:00
|
|
|
body1.destroyFixture(piece2);
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2024-12-24 15:07:16 +03:30
|
|
|
const body2 = world.createBody({
|
|
|
|
|
type: "dynamic",
|
|
|
|
|
position: body1.getPosition(),
|
|
|
|
|
angle: body1.getAngle(),
|
|
|
|
|
});
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2024-12-24 19:58:07 +03:30
|
|
|
piece2 = body2.createFixture({
|
|
|
|
|
shape: shape2,
|
|
|
|
|
density: 1.0,
|
|
|
|
|
});
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-07-31 21:47:06 -07:00
|
|
|
// Compute consistent velocities for new bodies based on
|
|
|
|
|
// cached velocity.
|
2024-12-18 14:18:45 +03:30
|
|
|
const center1 = body1.getWorldCenter();
|
|
|
|
|
const center2 = body2.getWorldCenter();
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2024-12-18 14:18:45 +03:30
|
|
|
const velocity1 = Vec2.add(
|
2024-12-15 13:30:32 +03:30
|
|
|
breakVelocity,
|
|
|
|
|
Vec2.cross(breakAngularVelocity, Vec2.sub(center1, center)),
|
|
|
|
|
);
|
2024-12-18 14:18:45 +03:30
|
|
|
const velocity2 = Vec2.add(
|
2024-12-15 13:30:32 +03:30
|
|
|
breakVelocity,
|
|
|
|
|
Vec2.cross(breakAngularVelocity, Vec2.sub(center2, center)),
|
|
|
|
|
);
|
2017-03-11 02:55:31 -05:00
|
|
|
|
2023-07-31 21:47:06 -07:00
|
|
|
body1.setAngularVelocity(breakAngularVelocity);
|
|
|
|
|
body1.setLinearVelocity(velocity1);
|
|
|
|
|
|
|
|
|
|
body2.setAngularVelocity(-breakAngularVelocity);
|
|
|
|
|
body2.setLinearVelocity(velocity2);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-15 13:30:32 +03:30
|
|
|
testbed.step = function () {
|
2023-07-31 21:47:06 -07:00
|
|
|
// Cache velocities to improve movement on breakage.
|
|
|
|
|
if (!broke) {
|
|
|
|
|
breakVelocity = body1.getLinearVelocity();
|
|
|
|
|
breakAngularVelocity = body1.getAngularVelocity();
|
|
|
|
|
}
|
|
|
|
|
};
|