123 lines
2.4 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, Circle, Box, Polygon, RevoluteJoint, 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.start(world);
testbed.info("Z: Limits, X: Motor");
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 }),
filterCategoryBits: 2,
filterMaskBits: 0xffff,
filterGroupIndex: 0,
2024-12-24 19:58:07 +03:30
});
2024-12-24 19:58:07 +03:30
const w = 100.0;
2024-12-24 15:07:16 +03:30
const rotator = world.createBody({
type: "dynamic",
position: { x: -10.0, y: 20.0 },
2024-12-24 19:58:07 +03:30
angularVelocity: w,
linearVelocity: { x: -8.0 * w, y: 0.0 },
});
rotator.createFixture({
shape: new Circle(0.5),
density: 5.0,
2024-12-24 15:07:16 +03:30
});
2024-12-18 14:18:45 +03:30
const joint = world.createJoint(
new RevoluteJoint(
{
motorSpeed: 1.0 * Math.PI,
maxMotorTorque: 10000.0,
enableMotor: true,
lowerAngle: -0.25 * Math.PI,
upperAngle: 0.5 * Math.PI,
enableLimit: false,
collideConnected: true,
},
ground,
rotator,
2024-12-23 18:24:21 +03:30
{ x: -10.0, y: 12.0 },
),
);
2024-12-24 15:07:16 +03:30
const ball = world.createBody({
type: "dynamic",
position: { x: 5.0, y: 30.0 },
});
2024-12-24 19:58:07 +03:30
ball.createFixture({
shape: new Circle(3.0),
density: 5.0,
// filterMaskBits: 1,
});
2024-12-18 14:18:45 +03:30
const platform = world.createBody({
type: "dynamic",
2024-12-24 15:07:16 +03:30
position: { x: 20.0, y: 10.0 },
bullet: true,
});
2024-12-24 19:58:07 +03:30
platform.createFixture({
shape: new Box(10.0, 0.2, { x: -10.0, y: 0.0 }, 0.0),
density: 2.0,
});
world.createJoint(
new RevoluteJoint(
{
lowerAngle: -0.25 * Math.PI,
upperAngle: 0.0 * Math.PI,
enableLimit: true,
},
ground,
platform,
2024-12-23 18:24:21 +03:30
{ x: 20.0, y: 10.0 },
),
);
// Tests mass computation of a small object far from the origin
2024-12-24 15:07:16 +03:30
const triangle = world.createBody({
type: "dynamic",
});
triangle.createFixture(
2024-12-23 18:24:21 +03:30
new Polygon([
{ x: 17.63, y: 36.31 },
{ x: 17.52, y: 36.69 },
{ x: 17.19, y: 36.36 },
]),
1,
); // assertion hits inside here
testbed.keydown = function (code, char) {
switch (char) {
case "Z":
2024-12-18 14:18:45 +03:30
joint?.enableLimit(!joint.isLimitEnabled());
break;
case "X":
2024-12-18 14:18:45 +03:30
joint?.enableMotor(!joint.isMotorEnabled());
break;
}
};
testbed.step = function () {
// if (stepCount++ == 360) {
2024-12-23 18:24:21 +03:30
// ball.setTransform(({ x: 0.0, y: 0.5 }), 0.0);
// }
2024-12-18 14:18:45 +03:30
testbed.status("Motor Torque", joint?.getMotorTorque(testbed.hz));
// testbed.status('Motor Force', joint.getMaxForce());
};