2016-03-23 22:59:54 -04: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
|
2016-03-23 22:59:54 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// A basic slider crank created for GDC tutorial: Understanding Constraints
|
2023-07-31 21:47:06 -07:00
|
|
|
|
2024-12-23 18:24:21 +03:30
|
|
|
import { World, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck";
|
2023-07-31 21:47:06 -07:00
|
|
|
|
2024-12-24 15:07:16 +03:30
|
|
|
const world = new World({
|
|
|
|
|
gravity: { x: 0, y: -10 },
|
|
|
|
|
});
|
2023-07-31 21:47:06 -07:00
|
|
|
|
2023-08-20 03:24:33 -07:00
|
|
|
const testbed = Testbed.mount();
|
2023-08-14 13:59:23 -07:00
|
|
|
testbed.y = -15;
|
2023-07-31 21:47:06 -07:00
|
|
|
testbed.start(world);
|
|
|
|
|
|
2024-12-24 15:07:16 +03:30
|
|
|
const ground = world.createBody({
|
|
|
|
|
type: "static",
|
|
|
|
|
position: { x: 0.0, y: 17.0 },
|
|
|
|
|
});
|
2023-07-31 21:47:06 -07:00
|
|
|
|
|
|
|
|
// Define crank.
|
2024-12-24 15:07:16 +03:30
|
|
|
const crank = world.createBody({
|
|
|
|
|
type: "dynamic",
|
|
|
|
|
position: { x: -8.0, y: 20.0 },
|
|
|
|
|
});
|
2024-12-24 19:58:07 +03:30
|
|
|
crank.createFixture({
|
|
|
|
|
shape: new Box(4.0, 1.0),
|
|
|
|
|
density: 2.0,
|
|
|
|
|
});
|
2024-12-23 18:24:21 +03:30
|
|
|
world.createJoint(new RevoluteJoint({}, ground, crank, { x: -12.0, y: 20.0 }));
|
2023-07-31 21:47:06 -07:00
|
|
|
|
|
|
|
|
// Define connecting rod
|
2024-12-24 15:07:16 +03:30
|
|
|
const rod = world.createBody({
|
|
|
|
|
type: "dynamic",
|
|
|
|
|
position: { x: 4.0, y: 20.0 },
|
|
|
|
|
});
|
2024-12-24 19:58:07 +03:30
|
|
|
rod.createFixture({
|
|
|
|
|
shape: new Box(8.0, 1.0),
|
|
|
|
|
density: 2.0,
|
|
|
|
|
});
|
2024-12-23 18:24:21 +03:30
|
|
|
world.createJoint(new RevoluteJoint({}, crank, rod, { x: -4.0, y: 20.0 }));
|
2023-07-31 21:47:06 -07:00
|
|
|
|
|
|
|
|
// Define piston
|
2024-12-24 15:07:16 +03:30
|
|
|
const piston = world.createBody({
|
|
|
|
|
type: "dynamic",
|
2024-12-23 18:24:21 +03:30
|
|
|
position: { x: 12.0, y: 20.0 },
|
2024-12-24 15:07:16 +03:30
|
|
|
fixedRotation: true,
|
2016-03-23 22:59:54 -04:00
|
|
|
});
|
2024-12-24 19:58:07 +03:30
|
|
|
piston.createFixture({
|
|
|
|
|
shape: new Box(3.0, 3.0),
|
|
|
|
|
density: 2.0,
|
|
|
|
|
});
|
2024-12-23 18:24:21 +03:30
|
|
|
world.createJoint(new RevoluteJoint({}, rod, piston, { x: 12.0, y: 20.0 }));
|
|
|
|
|
world.createJoint(new PrismaticJoint({}, ground, piston, { x: 12.0, y: 17.0 }, { x: 1.0, y: 0.0 }));
|