113 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

/*
2024-11-23 16:32:23 +03:30
* Copyright (c) Erin Catto
* Licensed under the MIT license
*/
// This tests bullet collision and provides an example of a gameplay scenario.
// This also uses a loop shape.
2024-12-23 18:24:21 +03:30
import { World, Circle, Box, Chain, 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);
// Ground body
2024-12-24 15:07:16 +03:30
const ground = world.createBody({
type: "static",
});
ground.createFixture(
new Chain(
[
2024-12-23 18:24:21 +03:30
{ x: 0.0, y: -2.0 },
{ x: 8.0, y: 6.0 },
{ x: 8.0, y: 20.0 },
{ x: -8.0, y: 20.0 },
{ x: -8.0, y: 6.0 },
],
true,
),
0.0,
);
// Flippers
2024-12-23 18:24:21 +03:30
const pLeft = { x: -2.0, y: 0.0 };
const pRight = { x: 2.0, y: 0.0 };
2024-12-24 15:07:16 +03:30
const leftFlipper = world.createBody({
type: "dynamic",
position: { x: -2.0, y: 0.0 },
});
const rightFlipper = world.createBody({
type: "dynamic",
position: { x: 2.0, y: 0.0 },
});
2024-12-24 19:58:07 +03:30
leftFlipper.createFixture({
shape: new Box(1.75, 0.1),
density: 1.0,
});
rightFlipper.createFixture({
shape: new Box(1.75, 0.1),
density: 1.0,
});
2024-12-18 14:18:45 +03:30
const jd = {
2023-08-14 13:59:23 -07:00
enableMotor: true,
maxMotorTorque: 1000.0,
enableLimit: true,
motorSpeed: 0.0,
};
2024-12-18 14:18:45 +03:30
const leftJoint = new RevoluteJoint(
{
...jd,
lowerAngle: (-30.0 * Math.PI) / 180.0,
upperAngle: (5.0 * Math.PI) / 180.0,
},
ground,
leftFlipper,
leftFlipper.getPosition(),
);
world.createJoint(leftJoint);
2024-12-18 14:18:45 +03:30
const rightJoint = new RevoluteJoint(
{
...jd,
lowerAngle: (-5.0 * Math.PI) / 180.0,
upperAngle: (30.0 * Math.PI) / 180.0,
},
ground,
rightFlipper,
rightFlipper.getPosition(),
);
world.createJoint(rightJoint);
// Circle character
2024-12-18 14:18:45 +03:30
const ball = world.createBody({
type: "dynamic",
2024-12-24 15:07:16 +03:30
position: { x: 1.0, y: 15.0 },
bullet: true,
});
2024-12-24 19:58:07 +03:30
ball.createFixture({
shape: new Circle(0.2),
density: 1.0,
});
testbed.step = function () {
if (testbed.activeKeys.right) {
rightJoint.setMotorSpeed(-20.0);
} else {
rightJoint.setMotorSpeed(10.0);
}
if (testbed.activeKeys.left) {
leftJoint.setMotorSpeed(20.0);
} else {
leftJoint.setMotorSpeed(-10.0);
}
2023-08-14 13:59:23 -07:00
};