85 lines
1.6 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, Body, Edge, Box, Polygon, Circle, RevoluteJoint, Testbed } from "planck";
2024-12-24 15:07:16 +03:30
const world = new World({
gravity: { x: 0, y: -4 },
});
2023-08-20 03:24:33 -07:00
const testbed = Testbed.mount();
testbed.start(world);
2024-12-18 14:18:45 +03:30
const COUNT = 30;
2024-12-18 14:18:45 +03:30
let middle: 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,
});
2024-12-18 14:18:45 +03:30
const bridgeRect = new Box(0.5, 0.125);
2024-12-18 14:18:45 +03:30
const bridgeFD = {
density: 20.0,
friction: 0.2,
};
2023-08-14 13:59:23 -07:00
let prevBody = ground;
for (let i = 0; i < COUNT; ++i) {
2024-12-24 15:07:16 +03:30
const body = world.createBody({
type: "dynamic",
position: { x: -14.5 + 1.0 * i, y: 5.0 },
});
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: bridgeRect,
...bridgeFD,
});
2024-12-23 18:24:21 +03:30
const anchor = { x: -15.0 + 1.0 * i, y: 5.0 };
world.createJoint(new RevoluteJoint({}, prevBody, body, anchor));
if (i * 2 === COUNT) {
middle = body;
}
prevBody = body;
}
2024-12-23 18:24:21 +03:30
const anchor = { x: -15.0 + 1.0 * COUNT, y: 5.0 };
world.createJoint(new RevoluteJoint({}, prevBody, ground, anchor));
2023-08-14 13:59:23 -07:00
for (let i = 0; i < 2; ++i) {
2024-12-24 15:07:16 +03:30
const body = world.createBody({
type: "dynamic",
position: { x: -8.0 + 8.0 * i, y: 12.0 },
});
2024-12-23 18:24:21 +03:30
const vertices = [
{ x: -0.5, y: 0.0 },
{ x: 0.5, y: 0.0 },
{ x: 0.0, y: 1.5 },
];
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: new Polygon(vertices),
density: 1.0,
});
}
2024-12-18 14:18:45 +03:30
const shape = new Circle(0.5);
2023-08-14 13:59:23 -07:00
for (let i = 0; i < 3; ++i) {
2024-12-24 15:07:16 +03:30
const body = world.createBody({
type: "dynamic",
position: { x: -6.0 + 6.0 * i, y: 10.0 },
});
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: shape,
density: 1.0,
});
}