planck-deterministic/example/HeavyOnLightTwo.ts

67 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2016-03-23 22:59:54 -04:00
/*
2024-11-23 16:32:23 +03:30
* Copyright (c) Erin Catto
* Licensed under the MIT license
2016-03-23 22:59:54 -04:00
*/
2024-12-23 18:24:21 +03:30
import { World, Body, Circle, Edge, Testbed } from "planck";
2016-03-23 22:59:54 -04:00
2024-12-24 15:07:16 +03:30
const world = new World({
gravity: { x: 0, y: -10 },
});
2016-03-23 22:59:54 -04:00
2023-08-20 03:24:33 -07:00
const testbed = Testbed.mount();
testbed.info("X: Add/Remove heavy circle");
testbed.start(world);
2016-03-23 22:59:54 -04:00
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,
});
2016-03-23 22:59:54 -04:00
2024-12-24 15:07:16 +03:30
const light1 = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 2.5 },
});
2024-12-24 19:58:07 +03:30
light1.createFixture({
shape: new Circle(0.5),
density: 10.0,
});
2016-03-23 22:59:54 -04:00
2024-12-24 15:07:16 +03:30
const light2 = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 3.5 },
});
2024-12-24 19:58:07 +03:30
light2.createFixture({
shape: new Circle(0.5),
density: 10.0,
});
2016-03-23 22:59:54 -04:00
2024-12-18 14:18:45 +03:30
let heavy: Body | null = null;
2017-01-25 00:35:14 -05:00
function toggleHeavy() {
if (heavy) {
world.destroyBody(heavy);
heavy = null;
} else {
2024-12-24 15:07:16 +03:30
heavy = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 9.0 },
});
2024-12-24 19:58:07 +03:30
heavy.createFixture({
shape: new Circle(5.0),
density: 10.0,
});
}
}
2017-01-25 00:35:14 -05:00
testbed.keydown = function (code, char) {
switch (char) {
case "X":
toggleHeavy();
break;
}
};