168 lines
3.0 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, Edge, Box, RevoluteJoint, DistanceJoint, Circle, 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.width = 50;
testbed.height = 50;
testbed.start(world);
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, y: 0 }, { x: 40, y: 0 }),
density: 0,
});
2024-12-24 15:07:16 +03:30
world
.createBody({
type: "static",
position: { x: -1.5, y: 10 },
})
2024-12-24 19:58:07 +03:30
.createFixture({
shape: new Box(6, 0.25),
density: 0,
});
2024-12-18 14:18:45 +03:30
const columnShape = new Box(0.1, 1);
2023-08-14 13:59:23 -07:00
for (let i = 0; i < 10; ++i) {
2024-12-24 15:07:16 +03:30
const ball = world.createBody({
type: "dynamic",
position: { x: -6 + 1 * i, y: 11.25 },
});
2024-12-24 19:58:07 +03:30
ball.createFixture({
shape: columnShape,
density: 20,
friction: 0.1,
});
}
2024-12-24 15:07:16 +03:30
world
.createBody({
type: "static",
position: { x: 1, y: 6 },
})
2024-12-24 19:58:07 +03:30
.createFixture({
shape: new Box(7, 0.25, { x: 0, y: 0 }, 0.3),
density: 0,
});
2024-12-24 15:07:16 +03:30
const b2 = world.createBody({
type: "static",
position: { x: -7, y: 4 },
});
2024-12-24 19:58:07 +03:30
b2.createFixture({
shape: new Box(0.25, 1.5),
density: 0,
});
2024-12-24 15:07:16 +03:30
const b3 = world.createBody({
type: "dynamic",
position: { x: -0.9, y: 1 },
angle: -0.15,
});
2024-12-24 19:58:07 +03:30
b3.createFixture({
shape: new Box(6, 0.125),
density: 10,
});
2024-12-18 14:18:45 +03:30
const jd = {
2023-08-14 13:59:23 -07:00
collideConnected: true,
};
2024-12-23 18:24:21 +03:30
world.createJoint(new RevoluteJoint(jd, ground, b3, { x: -2, y: 1 }));
2024-12-24 15:07:16 +03:30
const b4 = world.createBody({
type: "dynamic",
position: { x: -10, y: 15 },
});
2024-12-24 19:58:07 +03:30
b4.createFixture({
shape: new Box(0.25, 0.25),
density: 10,
});
2024-12-23 18:24:21 +03:30
world.createJoint(new RevoluteJoint(jd, b2, b4, { x: -7, y: 15 }));
2024-12-24 15:07:16 +03:30
const b5 = world.createBody({
type: "dynamic",
position: { x: 6.5, y: 3 },
});
2023-08-14 13:59:23 -07:00
{
2024-12-18 14:18:45 +03:30
const fd = {
2023-08-14 13:59:23 -07:00
density: 10,
friction: 0.1,
};
2024-12-24 19:58:07 +03:30
b5.createFixture({
shape: new Box(1, 0.1, { x: 0, y: -0.9 }, 0),
...fd,
});
b5.createFixture({
shape: new Box(0.1, 1, { x: -0.9, y: 0 }, 0),
...fd,
});
b5.createFixture({
shape: new Box(0.1, 1, { x: 0.9, y: 0 }, 0),
...fd,
});
2023-08-14 13:59:23 -07:00
}
2024-12-23 18:24:21 +03:30
world.createJoint(new RevoluteJoint(jd, ground, b5, { x: 6, y: 2 }));
2024-12-24 15:07:16 +03:30
const b6 = world.createBody({
type: "dynamic",
position: { x: 6.5, y: 4.1 },
});
2024-12-24 19:58:07 +03:30
b6.createFixture({
shape: new Box(1, 0.1),
density: 30,
});
2024-12-23 18:24:21 +03:30
world.createJoint(new RevoluteJoint(jd, b5, b6, { x: 7.5, y: 4 }));
2024-12-24 15:07:16 +03:30
const b7 = world.createBody({
type: "dynamic",
position: { x: 7.4, y: 1 },
});
2024-12-24 19:58:07 +03:30
b7.createFixture({
shape: new Box(0.1, 1),
density: 10,
});
world.createJoint(
new DistanceJoint({
bodyA: b3,
2024-12-23 18:24:21 +03:30
localAnchorA: { x: 6, y: 0 },
bodyB: b7,
2024-12-23 18:24:21 +03:30
localAnchorB: { x: 0, y: -1 },
}),
);
2023-08-14 13:59:23 -07:00
{
2024-12-18 14:18:45 +03:30
const radius = 0.2;
const circleShape = new Circle(radius);
2023-08-14 13:59:23 -07:00
for (let i = 0; i < 4; ++i) {
2024-12-24 15:07:16 +03:30
const body = world.createBody({
type: "dynamic",
position: {
x: 5.9 + 2 * radius * i,
y: 2.4,
},
});
2024-12-24 19:58:07 +03:30
body.createFixture({
shape: circleShape,
density: 10,
});
2023-08-14 13:59:23 -07:00
}
}