diff --git a/example/8-Ball.ts b/example/8-Ball.ts index 604d5807..cd5f8348 100644 --- a/example/8-Ball.ts +++ b/example/8-Ball.ts @@ -163,7 +163,9 @@ class BilliardPhysics { rails.push(topLeftRail.map((v) => ({ x: -v.x, y: -v.y }))); for (let i = 0; i < rails.length; i++) { - const body = this.world.createBody(); + const body = this.world.createBody({ + type: "static", + }); const shape = new Polygon(rails[i]); const fixture = body.createFixture(shape, { friction: 0.1, @@ -200,6 +202,7 @@ class BilliardPhysics { for (let i = 0; i < pockets.length; i++) { const body = this.world.createBody({ + type: "static", position: pockets[i], }); const shape = new Circle(POCKET_RADIUS); diff --git a/example/AddPair.ts b/example/AddPair.ts index e4bdebd5..7e81ad78 100644 --- a/example/AddPair.ts +++ b/example/AddPair.ts @@ -5,7 +5,7 @@ import { World, Circle, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: 0 }); +const world = new World(); const testbed = Testbed.mount(); testbed.y = 0; diff --git a/example/ApplyForce.ts b/example/ApplyForce.ts index c8b1467d..9dd78ea0 100644 --- a/example/ApplyForce.ts +++ b/example/ApplyForce.ts @@ -11,7 +11,10 @@ const testbed = Testbed.mount(); testbed.y = -20; testbed.start(world); -const ground = world.createBody({ x: 0.0, y: 20.0 }); +const ground = world.createBody({ + type: "static", + position: { x: 0.0, y: 20.0 }, +}); const wallFD = { density: 0.0, @@ -72,7 +75,10 @@ const boxFD = { }; for (let i = 0; i < 10; ++i) { - const box = world.createDynamicBody({ x: 0.0, y: 5.0 + 1.54 * i }); + const box = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 5.0 + 1.54 * i }, + }); box.createFixture(new Box(0.5, 0.5), boxFD); diff --git a/example/BasicSliderCrank.ts b/example/BasicSliderCrank.ts index 9347bdc5..b1afcdd1 100644 --- a/example/BasicSliderCrank.ts +++ b/example/BasicSliderCrank.ts @@ -7,28 +7,40 @@ import { World, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.y = -15; testbed.start(world); -const ground = world.createBody({ x: 0.0, y: 17.0 }); +const ground = world.createBody({ + type: "static", + position: { x: 0.0, y: 17.0 }, +}); // Define crank. -const crank = world.createDynamicBody({ x: -8.0, y: 20.0 }); +const crank = world.createBody({ + type: "dynamic", + position: { x: -8.0, y: 20.0 }, +}); crank.createFixture(new Box(4.0, 1.0), 2.0); world.createJoint(new RevoluteJoint({}, ground, crank, { x: -12.0, y: 20.0 })); // Define connecting rod -const rod = world.createDynamicBody({ x: 4.0, y: 20.0 }); +const rod = world.createBody({ + type: "dynamic", + position: { x: 4.0, y: 20.0 }, +}); rod.createFixture(new Box(8.0, 1.0), 2.0); world.createJoint(new RevoluteJoint({}, crank, rod, { x: -4.0, y: 20.0 })); // Define piston -const piston = world.createDynamicBody({ - fixedRotation: true, +const piston = world.createBody({ + type: "dynamic", position: { x: 12.0, y: 20.0 }, + fixedRotation: true, }); piston.createFixture(new Box(3.0, 3.0), 2.0); world.createJoint(new RevoluteJoint({}, rod, piston, { x: 12.0, y: 20.0 })); diff --git a/example/BodyTypes.ts b/example/BodyTypes.ts index a8fe8fe4..04527e2f 100644 --- a/example/BodyTypes.ts +++ b/example/BodyTypes.ts @@ -5,7 +5,9 @@ import { World, Edge, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.info("Z: Dynamic, X: Static, C: Kinematic"); @@ -13,15 +15,23 @@ testbed.start(world); const SPEED = 3.0; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 })); // Define attachment -const attachment = world.createDynamicBody({ x: 0.0, y: 3.0 }); +const attachment = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 3.0 }, +}); attachment.createFixture(new Box(0.5, 2.0), 2.0); // Define platform -const platform = world.createDynamicBody({ x: -4.0, y: 5.0 }); +const platform = world.createBody({ + type: "dynamic", + position: { x: -4.0, y: 5.0 }, +}); platform.createFixture(new Box(0.5, 4.0, { x: 4.0, y: 0.0 }, 0.5 * Math.PI), { friction: 0.6, @@ -57,7 +67,10 @@ world.createJoint( ); // Create a payload -const payload = world.createDynamicBody({ x: 0.0, y: 8.0 }); +const payload = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 8.0 }, +}); payload.createFixture(new Box(0.75, 0.75), { friction: 0.6, density: 2.0 }); testbed.keydown = function (code, char) { diff --git a/example/Boxes.ts b/example/Boxes.ts index 9acb097b..773ee444 100644 --- a/example/Boxes.ts +++ b/example/Boxes.ts @@ -1,23 +1,29 @@ import { World, Edge, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -const bar = world.createBody(); +const bar = world.createBody({ + type: "static", +}); bar.createFixture(new Edge({ x: -20, y: 5 }, { x: 20, y: 5 })); bar.setAngle(0.2); for (let i = -2; i <= 2; i++) { for (let j = -2; j <= 2; j++) { - const box = world.createBody().setDynamic(); - box.createFixture(new Box(0.5, 0.5)); - box.setPosition({ x: i * 1, y: -j * 1 + 20 }); + const box = world.createBody({ + type: "dynamic", + position: { x: i * 1, y: -j * 1 + 20 }, + }); box.setMassData({ mass: 1, center: { x: 0, y: 0 }, I: 1, }); + box.createFixture(new Box(0.5, 0.5)); } } diff --git a/example/Breakable.ts b/example/Breakable.ts index 2cfceb41..fcb4db32 100644 --- a/example/Breakable.ts +++ b/example/Breakable.ts @@ -7,7 +7,9 @@ import { World, Vec2, Edge, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -18,11 +20,17 @@ let breakAngularVelocity: number; let broke = false; // Ground body -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); // Breakable dynamic body -const body1 = world.createDynamicBody({ x: 0.0, y: 40.0 }, 0.25 * Math.PI); +const body1 = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 40.0 }, + angle: 0.25 * Math.PI, +}); const shape1 = new Box(0.5, 0.5, { x: -0.5, y: 0.0 }, 0.0); const piece1 = body1.createFixture(shape1, 1.0); @@ -58,7 +66,11 @@ function Break() { body1.destroyFixture(piece2); - const body2 = world.createDynamicBody(body1.getPosition(), body1.getAngle()); + const body2 = world.createBody({ + type: "dynamic", + position: body1.getPosition(), + angle: body1.getAngle(), + }); piece2 = body2.createFixture(shape2, 1.0); diff --git a/example/Breakout.ts b/example/Breakout.ts index f5b13504..7c8b987f 100644 --- a/example/Breakout.ts +++ b/example/Breakout.ts @@ -215,27 +215,45 @@ class BreakoutPhysics { createBoardPhysics() { { - const wall = this.world.createBody({ x: +9, y: -0.5 }); + const wall = this.world.createBody({ + type: "static", + position: { x: +9, y: -0.5 }, + }); wall.createFixture(new EdgeShape({ x: 0, y: -12.5 }, { x: 0, y: +11.5 }), wallFix); } { - const wall = this.world.createBody({ x: -9, y: -0.5 }); + const wall = this.world.createBody({ + type: "static", + position: { x: -9, y: -0.5 }, + }); wall.createFixture(new EdgeShape({ x: 0, y: -12.5 }, { x: 0, y: +11.5 }), wallFix); } { - const wall = this.world.createBody({ x: 0, y: +12 }); + const wall = this.world.createBody({ + type: "static", + position: { x: 0, y: +12 }, + }); wall.createFixture(new EdgeShape({ x: -8, y: 0 }, { x: +8, y: 0 }), wallFix); } { - const wall = this.world.createBody({ x: 9, y: 12 }); + const wall = this.world.createBody({ + type: "static", + position: { x: 9, y: 12 }, + }); wall.createFixture(new EdgeShape({ x: -1, y: 0 }, { x: 0, y: -1 }), wallFix); } { - const wall = this.world.createBody({ x: -9, y: 12 }); + const wall = this.world.createBody({ + type: "static", + position: { x: -9, y: 12 }, + }); wall.createFixture(new EdgeShape({ x: 1, y: 0 }, { x: 0, y: -1 }), wallFix); } { - const wall = this.world.createBody({ x: 0, y: -13 }); + const wall = this.world.createBody({ + type: "static", + position: { x: 0, y: -13 }, + }); wall.createFixture(new EdgeShape({ x: -9, y: 0 }, { x: +9, y: 0 }), wallFix); wall.setUserData(new WallData()); @@ -273,7 +291,8 @@ class BreakoutPhysics { } addBallPhysics(data: BallData) { - const body = this.world.createDynamicBody({ + const body = this.world.createBody({ + type: "dynamic", bullet: true, angle: Math.random() * Math.PI * 2, }); @@ -301,7 +320,10 @@ class BreakoutPhysics { addBrickPhysics(data: BrickData) { const shape = data.subtype == "small" ? smallBrickShape : normalBrickShape; const pos = { x: (data.i - 3) * 2, y: 9 - data.j * 2 }; - const body = this.world.createBody(pos); + const body = this.world.createBody({ + type: "static", + position: pos, + }); body.createFixture(shape, brickFix); body.setUserData(data); @@ -321,7 +343,9 @@ class BreakoutPhysics { } addDropPhysics(drop: DropData) { - const body = this.world.createDynamicBody(); + const body = this.world.createBody({ + type: "dynamic", + }); if (drop.subtype == "+") { body.createFixture(new BoxShape(0.08, 0.32), dropFix); body.createFixture(new BoxShape(0.32, 0.08), dropFix); diff --git a/example/Bridge.ts b/example/Bridge.ts index c1b08ee3..449827f2 100644 --- a/example/Bridge.ts +++ b/example/Bridge.ts @@ -5,7 +5,9 @@ import { World, Body, Edge, Box, Polygon, Circle, RevoluteJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -4 }); +const world = new World({ + gravity: { x: 0, y: -4 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -14,7 +16,9 @@ const COUNT = 30; let middle: Body; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const bridgeRect = new Box(0.5, 0.125); @@ -26,7 +30,10 @@ const bridgeFD = { let prevBody = ground; for (let i = 0; i < COUNT; ++i) { - const body = world.createDynamicBody({ x: -14.5 + 1.0 * i, y: 5.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: -14.5 + 1.0 * i, y: 5.0 }, + }); body.createFixture(bridgeRect, bridgeFD); const anchor = { x: -15.0 + 1.0 * i, y: 5.0 }; @@ -42,7 +49,10 @@ const anchor = { x: -15.0 + 1.0 * COUNT, y: 5.0 }; world.createJoint(new RevoluteJoint({}, prevBody, ground, anchor)); for (let i = 0; i < 2; ++i) { - const body = world.createDynamicBody({ x: -8.0 + 8.0 * i, y: 12.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: -8.0 + 8.0 * i, y: 12.0 }, + }); const vertices = [ { x: -0.5, y: 0.0 }, @@ -54,6 +64,9 @@ for (let i = 0; i < 2; ++i) { const shape = new Circle(0.5); for (let i = 0; i < 3; ++i) { - const body = world.createDynamicBody({ x: -6.0 + 6.0 * i, y: 10.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: -6.0 + 6.0 * i, y: 10.0 }, + }); body.createFixture(shape, 1.0); } diff --git a/example/BulletTest.ts b/example/BulletTest.ts index b15c5c2e..e56fb380 100644 --- a/example/BulletTest.ts +++ b/example/BulletTest.ts @@ -5,16 +5,23 @@ import { World, Edge, Box, stats, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -10.0, y: 0.0 }, { x: 10.0, y: 0.0 }), 0.0); ground.createFixture(new Box(0.2, 1.0, { x: 0.5, y: 1.0 }, 0.0), 0.0); -const body = world.createDynamicBody({ x: 0.0, y: 4.0 }); +const body = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 4.0 }, +}); body.createFixture(new Box(2.0, 0.1), 1.0); // x = Math.random(-1.0, 1.0); diff --git a/example/Cantilever.ts b/example/Cantilever.ts index 659eae35..6e6ab5a1 100644 --- a/example/Cantilever.ts +++ b/example/Cantilever.ts @@ -10,19 +10,26 @@ import { World, Edge, Box, WeldJoint, Polygon, Circle, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); const COUNT = 8; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); { let prevBody = ground; for (let i = 0; i < COUNT; ++i) { - const body = world.createDynamicBody({ x: -14.5 + 1.0 * i, y: 5.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: -14.5 + 1.0 * i, y: 5.0 }, + }); body.createFixture(new Box(0.5, 0.125), 20.0); const anchor = { x: -15.0 + 1.0 * i, y: 5.0 }; @@ -34,7 +41,10 @@ ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); { let prevBody = ground; for (let i = 0; i < 3; ++i) { - const body = world.createDynamicBody({ x: -14.0 + 2.0 * i, y: 15.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: -14.0 + 2.0 * i, y: 15.0 }, + }); body.createFixture(new Box(1.0, 0.125), 20.0); const anchor = { x: -15.0 + 2.0 * i, y: 15.0 }; @@ -56,7 +66,10 @@ ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); { let prevBody = ground; for (let i = 0; i < COUNT; ++i) { - const body = world.createDynamicBody({ x: -4.5 + 1.0 * i, y: 5.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: -4.5 + 1.0 * i, y: 5.0 }, + }); body.createFixture(new Box(0.5, 0.125), 20.0); if (i > 0) { @@ -70,7 +83,10 @@ ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); { let prevBody = ground; for (let i = 0; i < COUNT; ++i) { - const body = world.createDynamicBody({ x: 5.5 + 1.0 * i, y: 10.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: 5.5 + 1.0 * i, y: 10.0 }, + }); body.createFixture(new Box(0.5, 0.125), 20.0); if (i > 0) { @@ -99,12 +115,18 @@ ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); { x: 0.0, y: 1.5 }, ]; - const body = world.createDynamicBody({ x: -8.0 + 8.0 * i, y: 12.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: -8.0 + 8.0 * i, y: 12.0 }, + }); body.createFixture(new Polygon(vertices), 1.0); } for (let i = 0; i < 2; ++i) { - const body = world.createDynamicBody({ x: -6.0 + 6.0 * i, y: 10.0 }); + const body = world.createBody({ + type: "dynamic", + position: { x: -6.0 + 6.0 * i, y: 10.0 }, + }); body.createFixture(new Circle(0.5), 1.0); } } diff --git a/example/Car.ts b/example/Car.ts index 8f805f77..479429e2 100644 --- a/example/Car.ts +++ b/example/Car.ts @@ -23,7 +23,9 @@ let HZ = 4.0; const ZETA = 0.7; const SPEED = 50.0; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); const groundFD = { density: 0.0, @@ -67,7 +69,10 @@ x += 40.0; ground.createFixture(new Edge({ x: x, y: 0.0 }, { x: x, y: 20.0 }), groundFD); // Teeter -const teeter = world.createDynamicBody({ x: 140.0, y: 1.0 }); +const teeter = world.createBody({ + type: "dynamic", + position: { x: 140.0, y: 1.0 }, +}); teeter.createFixture(new Box(10.0, 0.25), 1.0); world.createJoint( new RevoluteJoint( @@ -93,7 +98,10 @@ const bridgeFD = { let prevBody = ground; let i: number; for (i = 0; i < 20; ++i) { - const bridgeBlock = world.createDynamicBody({ x: 161.0 + 2.0 * i, y: -0.125 }); + const bridgeBlock = world.createBody({ + type: "dynamic", + position: { x: 161.0 + 2.0 * i, y: -0.125 }, + }); bridgeBlock.createFixture(new Box(1.0, 0.125), bridgeFD); world.createJoint( @@ -108,18 +116,46 @@ world.createJoint(new RevoluteJoint({}, prevBody, ground, { x: 160.0 + 2.0 * i, // Boxes const box = new Box(0.5, 0.5); -world.createDynamicBody({ x: 230.0, y: 0.5 }).createFixture(box, 0.5); - -world.createDynamicBody({ x: 230.0, y: 1.5 }).createFixture(box, 0.5); - -world.createDynamicBody({ x: 230.0, y: 2.5 }).createFixture(box, 0.5); - -world.createDynamicBody({ x: 230.0, y: 3.5 }).createFixture(box, 0.5); - -world.createDynamicBody({ x: 230.0, y: 4.5 }).createFixture(box, 0.5); +world + .createBody({ + type: "dynamic", + position: { x: 230.0, y: 0.5 }, + }) + .createFixture(box, 0.5); + +world + .createBody({ + type: "dynamic", + position: { x: 230.0, y: 1.5 }, + }) + .createFixture(box, 0.5); + +world + .createBody({ + type: "dynamic", + position: { x: 230.0, y: 2.5 }, + }) + .createFixture(box, 0.5); + +world + .createBody({ + type: "dynamic", + position: { x: 230.0, y: 3.5 }, + }) + .createFixture(box, 0.5); + +world + .createBody({ + type: "dynamic", + position: { x: 230.0, y: 4.5 }, + }) + .createFixture(box, 0.5); // Car -const car = world.createDynamicBody({ x: 0.0, y: 1.0 }); +const car = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 1.0 }, +}); car.createFixture( new Polygon([ { x: -1.5, y: -0.5 }, @@ -137,10 +173,16 @@ const wheelFD = { friction: 0.9, }; -const wheelBack = world.createDynamicBody({ x: -1.0, y: 0.35 }); +const wheelBack = world.createBody({ + type: "dynamic", + position: { x: -1.0, y: 0.35 }, +}); wheelBack.createFixture(new Circle(0.4), wheelFD); -const wheelFront = world.createDynamicBody({ x: 1.0, y: 0.4 }); +const wheelFront = world.createBody({ + type: "dynamic", + position: { x: 1.0, y: 0.4 }, +}); wheelFront.createFixture(new Circle(0.4), wheelFD); const springBack = world.createJoint( diff --git a/example/Chain.ts b/example/Chain.ts index 32105024..f3a4b41d 100644 --- a/example/Chain.ts +++ b/example/Chain.ts @@ -5,12 +5,16 @@ import { World, Edge, Box, RevoluteJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const shape = new Box(0.6, 0.125); @@ -18,7 +22,8 @@ const shape = new Box(0.6, 0.125); const y = 25.0; let prevBody = ground; for (let i = 0; i < 30; ++i) { - const body = world.createDynamicBody({ + const body = world.createBody({ + type: "dynamic", position: { x: 0.5 + i, y: y }, }); body.createFixture(shape, { diff --git a/example/CharacterCollision.ts b/example/CharacterCollision.ts index a3d0f836..ba8b494f 100644 --- a/example/CharacterCollision.ts +++ b/example/CharacterCollision.ts @@ -9,7 +9,9 @@ import { World, Vec2Value, Edge, Chain, Box, Polygon, Circle, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.info(` @@ -20,19 +22,27 @@ testbed.info(` testbed.start(world); // Ground body -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }), 0.0); // Collinear edges with no adjacency information. // This shows the problematic case where a box shape can hit // an internal vertex. -const edge = world.createBody(); +const edge = world.createBody({ + type: "static", +}); edge.createFixture(new Edge({ x: -8.0, y: 1.0 }, { x: -6.0, y: 1.0 }), 0.0); edge.createFixture(new Edge({ x: -6.0, y: 1.0 }, { x: -4.0, y: 1.0 }), 0.0); edge.createFixture(new Edge({ x: -4.0, y: 1.0 }, { x: -2.0, y: 1.0 }), 0.0); // Chain shape -const chain = world.createBody({ x: 0, y: 0 }, 0.25 * Math.PI); +const chain = world.createBody({ + type: "static", + position: { x: 0, y: 0 }, + angle: 0.25 * Math.PI, +}); chain.createFixture( new Chain([ { x: 5.0, y: 7.0 }, @@ -46,13 +56,17 @@ chain.createFixture( // Square tiles. This shows that adjacency shapes may // have non-smooth collision. There is no solution // to this problem. -const tiles = world.createBody(); +const tiles = world.createBody({ + type: "static", +}); tiles.createFixture(new Box(1.0, 1.0, { x: 4.0, y: 3.0 }, 0.0), 0.0); tiles.createFixture(new Box(1.0, 1.0, { x: 6.0, y: 3.0 }, 0.0), 0.0); tiles.createFixture(new Box(1.0, 1.0, { x: 8.0, y: 3.0 }, 0.0), 0.0); // Square made from an edge loop. Collision should be smooth. -const square = world.createBody(); +const square = world.createBody({ + type: "static", +}); square.createFixture( new Chain( [ @@ -67,7 +81,10 @@ square.createFixture( ); // Edge loop. Collision should be smooth. -const loop = world.createBody({ x: -10.0, y: 4.0 }); +const loop = world.createBody({ + type: "static", + position: { x: -10.0, y: 4.0 }, +}); loop.createFixture( new Chain( [ @@ -89,8 +106,8 @@ loop.createFixture( // Square character 1 const char1 = world.createBody({ - position: { x: -3.0, y: 8.0 }, type: "dynamic", + position: { x: -3.0, y: 8.0 }, fixedRotation: true, allowSleep: false, }); @@ -98,8 +115,8 @@ char1.createFixture(new Box(0.5, 0.5), 20.0); // Square character 2 const char2 = world.createBody({ - position: { x: -5.0, y: 5.0 }, type: "dynamic", + position: { x: -5.0, y: 5.0 }, fixedRotation: true, allowSleep: false, }); @@ -107,8 +124,8 @@ char2.createFixture(new Box(0.25, 0.25), 20.0); // Hexagon character const hex = world.createBody({ - position: { x: -5.0, y: 8.0 }, type: "dynamic", + position: { x: -5.0, y: 8.0 }, fixedRotation: true, allowSleep: false, }); @@ -125,8 +142,8 @@ hex.createFixture(new Polygon(vertices), 20.0); // Circle character const circle = world.createBody({ - position: { x: 3.0, y: 5.0 }, type: "dynamic", + position: { x: 3.0, y: 5.0 }, fixedRotation: true, allowSleep: false, }); @@ -134,8 +151,8 @@ circle.createFixture(new Circle(0.5), 20.0); // Circle character const character = world.createBody({ - position: { x: -7.0, y: 6.0 }, type: "dynamic", + position: { x: -7.0, y: 6.0 }, allowSleep: false, }); character.createFixture(new Circle(0.25), { diff --git a/example/CollisionFiltering.ts b/example/CollisionFiltering.ts index 71eeb26a..8cc9058d 100644 --- a/example/CollisionFiltering.ts +++ b/example/CollisionFiltering.ts @@ -23,13 +23,17 @@ const TRIANGLE_MASK = 0xffff; const BOX_MASK = 0xffff ^ TRIANGLE_CATEGORY; const CIRCLE_MAX = 0xffff; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); // Ground body -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), { friction: 0.3, }); @@ -55,7 +59,7 @@ body1.createFixture( smallTriangle, ); -// Large triangle (recycle definitions) +// Large triangle const largeTriangle = { density: 1.0, filterCategoryBits: TRIANGLE_CATEGORY, @@ -77,7 +81,10 @@ body2.createFixture( largeTriangle, ); -const body = world.createDynamicBody({ x: -5.0, y: 10.0 }); +const body = world.createBody({ + type: "dynamic", + position: { x: -5.0, y: 10.0 }, +}); body.createFixture(new Box(0.5, 1.0), 1.0); world.createJoint( @@ -102,10 +109,13 @@ const smallBox = { filterGroupIndex: SMALL_GROUP, }; -const body3 = world.createDynamicBody({ x: 0.0, y: 2.0 }); +const body3 = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 2.0 }, +}); body3.createFixture(new Box(1.0, 0.5), smallBox); -// Large box (recycle definitions) +// Large box const largeBox = { density: 1.0, restitution: 0.1, @@ -114,7 +124,10 @@ const largeBox = { filterGroupIndex: LARGE_GROUP, }; -const body4 = world.createDynamicBody({ x: 0.0, y: 6.0 }); +const body4 = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 6.0 }, +}); body4.createFixture(new Box(2.0, 1.0), largeBox); // Small circle @@ -125,7 +138,10 @@ const smallCircle = { filterGroupIndex: SMALL_GROUP, }; -const body5 = world.createDynamicBody({ x: 5.0, y: 2.0 }); +const body5 = world.createBody({ + type: "dynamic", + position: { x: 5.0, y: 2.0 }, +}); body5.createFixture(new Circle(1.0), smallCircle); // Large circle @@ -136,5 +152,8 @@ const largeCircle = { filterGroupIndex: LARGE_GROUP, }; -const body6 = world.createDynamicBody({ x: 5.0, y: 6.0 }); +const body6 = world.createBody({ + type: "dynamic", + position: { x: 5.0, y: 6.0 }, +}); body6.createFixture(new Circle(2.0), largeCircle); diff --git a/example/CollisionProcessing.ts b/example/CollisionProcessing.ts index 5e8b6fb7..dd11bee7 100644 --- a/example/CollisionProcessing.ts +++ b/example/CollisionProcessing.ts @@ -7,13 +7,19 @@ import { World, Body, Fixture, Vec2Value, Edge, Polygon, Box, Circle, Math, Test // This test shows collision processing and tests // deferred body destruction. -const world = new World({ x: 0, y: -10 }); + +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); // Ground body -world.createBody().createFixture(new Edge({ x: -50.0, y: 0.0 }, { x: 50.0, y: 0.0 })); +const ground = world.createBody({ + type: "static", +}); +ground.createFixture(new Edge({ x: -50.0, y: 0.0 }, { x: 50.0, y: 0.0 })); const xLo = -5.0; const xHi = 5.0; @@ -21,9 +27,12 @@ const yLo = 2.0; const yHi = 35.0; // Small triangle -const body1 = world.createDynamicBody({ - x: Math.random(xLo, xHi), - y: Math.random(yLo, yHi), +const body1 = world.createBody({ + type: "dynamic", + position: { + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), + }, }); body1.createFixture( new Polygon([ @@ -34,10 +43,13 @@ body1.createFixture( 1.0, ); -// Large triangle (recycle definitions) -const body2 = world.createDynamicBody({ - x: Math.random(xLo, xHi), - y: Math.random(yLo, yHi), +// Large triangle +const body2 = world.createBody({ + type: "dynamic", + position: { + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), + }, }); body2.createFixture( new Polygon([ @@ -49,30 +61,42 @@ body2.createFixture( ); // Small box -const body3 = world.createDynamicBody({ - x: Math.random(xLo, xHi), - y: Math.random(yLo, yHi), +const body3 = world.createBody({ + type: "dynamic", + position: { + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), + }, }); body3.createFixture(new Box(1.0, 0.5), 1.0); -// Large box (recycle definitions) -const body4 = world.createDynamicBody({ - x: Math.random(xLo, xHi), - y: Math.random(yLo, yHi), +// Large box +const body4 = world.createBody({ + type: "dynamic", + position: { + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), + }, }); body4.createFixture(new Box(2.0, 1.0), 1.0); // Small circle -const body5 = world.createDynamicBody({ - x: Math.random(xLo, xHi), - y: Math.random(yLo, yHi), +const body5 = world.createBody({ + type: "dynamic", + position: { + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), + }, }); body5.createFixture(new Circle(1.0), 1.0); // Large circle -const body6 = world.createDynamicBody({ - x: Math.random(xLo, xHi), - y: Math.random(yLo, yHi), +const body6 = world.createBody({ + type: "dynamic", + position: { + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), + }, }); body6.createFixture(new Circle(2.0), 1.0); diff --git a/example/CompoundShapes.ts b/example/CompoundShapes.ts index 8669fad4..de877f98 100644 --- a/example/CompoundShapes.ts +++ b/example/CompoundShapes.ts @@ -6,13 +6,18 @@ // TODO_ERIN test joints on compounds. import { World, Vec2, Transform, Math, Edge, Circle, Polygon, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); { - const ground = world.createBody({ x: 0.0, y: 0.0 }); + const ground = world.createBody({ + type: "static", + position: { x: 0.0, y: 0.0 }, + }); ground.createFixture(new Edge({ x: 50.0, y: 0.0 }, { x: -50.0, y: 0.0 }), 0.0); } @@ -20,7 +25,8 @@ const circle1 = new Circle({ x: -0.5, y: 0.5 }, 0.5); const circle2 = new Circle({ x: 0.5, y: 0.5 }, 0.5); for (let i = 0; i < 10; ++i) { - const body = world.createDynamicBody({ + const body = world.createBody({ + type: "dynamic", position: { x: Math.random(-0.1, 0.1) + 5.0, y: 1.05 + 2.5 * i, @@ -35,7 +41,8 @@ const polygon1 = new Box(0.25, 0.5); const polygon2 = new Box(0.25, 0.5, { x: 0.0, y: -0.5 }, 0.5 * Math.PI); for (let i = 0; i < 10; ++i) { - const body = world.createDynamicBody({ + const body = world.createBody({ + type: "dynamic", position: { x: Math.random(-0.1, 0.1) - 5.0, y: 1.05 + 2.5 * i }, angle: Math.random(-Math.PI, Math.PI), }); @@ -68,7 +75,8 @@ const triangle2 = new Polygon( ); for (let i = 0; i < 10; ++i) { - const body = world.createDynamicBody({ + const body = world.createBody({ + type: "dynamic", position: { x: Math.random(-0.1, 0.1), y: 2.05 + 2.5 * i, @@ -83,7 +91,10 @@ const bottom = new Box(1.5, 0.15); const left = new Box(0.15, 2.7, { x: -1.45, y: 2.35 }, 0.2); const right = new Box(0.15, 2.7, { x: 1.45, y: 2.35 }, -0.2); -const container = world.createBody({ x: 0.0, y: 2.0 }); +const container = world.createBody({ + type: "static", + position: { x: 0.0, y: 2.0 }, +}); container.createFixture(bottom, 4.0); container.createFixture(left, 4.0); container.createFixture(right, 4.0); diff --git a/example/Confined.ts b/example/Confined.ts index c9479907..9c7f1db0 100644 --- a/example/Confined.ts +++ b/example/Confined.ts @@ -12,7 +12,9 @@ testbed.start(world); const e_columnCount = 0; const e_rowCount = 0; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); // Floor ground.createFixture(new Edge({ x: -10, y: 0 }, { x: 10, y: 0 }), 0); @@ -36,7 +38,8 @@ const fd = { for (let j = 0; j < e_columnCount; ++j) { for (let i = 0; i < e_rowCount; ++i) { - const body = world.createDynamicBody({ + const body = world.createBody({ + type: "dynamic", position: { x: -10 + (2.1 * j + 1 + 0.01 * i) * radius, y: (2 * i + 1) * radius, @@ -47,7 +50,8 @@ for (let j = 0; j < e_columnCount; ++j) { } function CreateCircle() { - const body = world.createDynamicBody({ + const body = world.createBody({ + type: "dynamic", position: { x: Math.random() * 10 - 5, y: Math.random() * 10 + 5, diff --git a/example/ContinuousTest.ts b/example/ContinuousTest.ts index 2948de74..5f07f076 100644 --- a/example/ContinuousTest.ts +++ b/example/ContinuousTest.ts @@ -5,7 +5,9 @@ import { World, Body, stats, Circle, Edge, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -13,14 +15,20 @@ testbed.start(world); let bullet: Body; let angularVelocity: number; -const ground = world.createBody({ x: 0.0, y: 0.0 }); +const ground = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 0.0 }, +}); ground.createFixture(new Edge({ x: -10.0, y: 0.0 }, { x: 10.0, y: 0.0 }), 0.0); ground.createFixture(new Box(0.2, 1.0, { x: 0.5, y: 1.0 }, 0.0), 0.0); if (true) { // angle = 0.1; - bullet = world.createDynamicBody({ x: 0.0, y: 20.0 }); + bullet = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 20.0 }, + }); bullet.createFixture(new Box(2.0, 0.1), 1.0); angularVelocity = Math.random() * 100 - 50; @@ -29,15 +37,22 @@ if (true) { bullet.setAngularVelocity(angularVelocity); } else { const shape = new Circle(0.5); - - world.createDynamicBody({ x: 0.0, y: 2.0 }).createFixture(shape, 1.0); - - const body = world.createDynamicBody({ - bullet: true, - position: { x: 0.0, y: 2.0 }, - }); - body.createFixture(shape, 1.0); - body.setLinearVelocity({ x: 0.0, y: -100.0 }); + { + const body = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 2.0 }, + }); + body.createFixture(shape, 1.0); + } + { + const body = world.createBody({ + type: "dynamic", + bullet: true, + position: { x: 0.0, y: 2.0 }, + }); + body.createFixture(shape, 1.0); + body.setLinearVelocity({ x: 0.0, y: -100.0 }); + } } function launch() { diff --git a/example/ConveyorBelt.ts b/example/ConveyorBelt.ts index bcb22b88..2d4cc453 100644 --- a/example/ConveyorBelt.ts +++ b/example/ConveyorBelt.ts @@ -5,23 +5,34 @@ import { World, Edge, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); // Ground -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }), 0.0); // Platform const platform = world - .createBody({ x: -5.0, y: 5.0 }) + .createBody({ + type: "static", + position: { x: -5.0, y: 5.0 }, + }) .createFixture(new Box(10.0, 0.5), { friction: 0.8 }); // Boxes for (let i = 0; i < 5; ++i) { - world.createDynamicBody({ x: -10.0 + 2.0 * i, y: 7.0 }).createFixture(new Box(0.5, 0.5), 20.0); + const body = world.createBody({ + type: "dynamic", + position: { x: -10.0 + 2.0 * i, y: 7.0 }, + }); + body.createFixture(new Box(0.5, 0.5), 20.0); } world.on("pre-solve", function (contact, oldManifold) { diff --git a/example/Debug.ts b/example/Debug.ts index ac76ee36..37e3550e 100644 --- a/example/Debug.ts +++ b/example/Debug.ts @@ -1,4 +1,4 @@ -import { Testbed, World, Vec2, Box } from "planck"; +import { Testbed, World } from "planck"; const testbed = Testbed.mount(); diff --git a/example/DistanceTest.ts b/example/DistanceTest.ts index 210937ce..bc8cd384 100644 --- a/example/DistanceTest.ts +++ b/example/DistanceTest.ts @@ -24,10 +24,14 @@ const transformB = new Transform(positionB, angleB); const polygonB = new Box(2.0, 0.1); -const bodyA = world.createBody(); +const bodyA = world.createBody({ + type: "static", +}); const fixA = bodyA.createFixture(polygonA); -const bodyB = world.createBody(); +const bodyB = world.createBody({ + type: "static", +}); const fixB = bodyB.createFixture(polygonB); testbed.step = function () { diff --git a/example/Dominos.ts b/example/Dominos.ts index ce47e170..f3de3238 100644 --- a/example/Dominos.ts +++ b/example/Dominos.ts @@ -5,33 +5,58 @@ import { World, Edge, Box, RevoluteJoint, DistanceJoint, Circle, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.width = 50; testbed.height = 50; testbed.start(world); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40, y: 0 }, { x: 40, y: 0 }), 0); -world.createBody({ x: -1.5, y: 10 }).createFixture(new Box(6, 0.25), 0); +world + .createBody({ + type: "static", + position: { x: -1.5, y: 10 }, + }) + .createFixture(new Box(6, 0.25), 0); const columnShape = new Box(0.1, 1); for (let i = 0; i < 10; ++i) { - world.createDynamicBody({ x: -6 + 1 * i, y: 11.25 }).createFixture(columnShape, { + const ball = world.createBody({ + type: "dynamic", + position: { x: -6 + 1 * i, y: 11.25 }, + }); + ball.createFixture(columnShape, { density: 20, friction: 0.1, }); } -world.createBody({ x: 1, y: 6 }).createFixture(new Box(7, 0.25, { x: 0, y: 0 }, 0.3), 0); - -const b2 = world.createBody({ x: -7, y: 4 }); +world + .createBody({ + type: "static", + position: { x: 1, y: 6 }, + }) + .createFixture(new Box(7, 0.25, { x: 0, y: 0 }, 0.3), 0); + +const b2 = world.createBody({ + type: "static", + position: { x: -7, y: 4 }, +}); b2.createFixture(new Box(0.25, 1.5), 0); -const b3 = world.createDynamicBody({ x: -0.9, y: 1 }, -0.15); +const b3 = world.createBody({ + type: "dynamic", + position: { x: -0.9, y: 1 }, + angle: -0.15, +}); b3.createFixture(new Box(6, 0.125), 10); const jd = { @@ -40,12 +65,18 @@ const jd = { world.createJoint(new RevoluteJoint(jd, ground, b3, { x: -2, y: 1 })); -const b4 = world.createDynamicBody({ x: -10, y: 15 }); +const b4 = world.createBody({ + type: "dynamic", + position: { x: -10, y: 15 }, +}); b4.createFixture(new Box(0.25, 0.25), 10); world.createJoint(new RevoluteJoint(jd, b2, b4, { x: -7, y: 15 })); -const b5 = world.createDynamicBody({ x: 6.5, y: 3 }); +const b5 = world.createBody({ + type: "dynamic", + position: { x: 6.5, y: 3 }, +}); { const fd = { @@ -60,12 +91,18 @@ const b5 = world.createDynamicBody({ x: 6.5, y: 3 }); world.createJoint(new RevoluteJoint(jd, ground, b5, { x: 6, y: 2 })); -const b6 = world.createDynamicBody({ x: 6.5, y: 4.1 }); +const b6 = world.createBody({ + type: "dynamic", + position: { x: 6.5, y: 4.1 }, +}); b6.createFixture(new Box(1, 0.1), 30); world.createJoint(new RevoluteJoint(jd, b5, b6, { x: 7.5, y: 4 })); -const b7 = world.createDynamicBody({ x: 7.4, y: 1 }); +const b7 = world.createBody({ + type: "dynamic", + position: { x: 7.4, y: 1 }, +}); b7.createFixture(new Box(0.1, 1), 10); world.createJoint( @@ -81,7 +118,13 @@ world.createJoint( const radius = 0.2; const circleShape = new Circle(radius); for (let i = 0; i < 4; ++i) { - const body = world.createDynamicBody({ x: 5.9 + 2 * radius * i, y: 2.4 }); + const body = world.createBody({ + type: "dynamic", + position: { + x: 5.9 + 2 * radius * i, + y: 2.4, + }, + }); body.createFixture(circleShape, 10); } } diff --git a/example/EdgeShapes.ts b/example/EdgeShapes.ts index d31e8826..e81d8c65 100644 --- a/example/EdgeShapes.ts +++ b/example/EdgeShapes.ts @@ -3,22 +3,11 @@ * Licensed under the MIT license */ -import { - Vec2, - World, - Body, - BodyDef, - Fixture, - Shape, - Edge, - Polygon, - Box, - Circle, - Math, - Testbed, -} from "planck"; - -const world = new World({ x: 0, y: -10 }); +import { Vec2, World, Body, Fixture, Shape, Edge, Polygon, Box, Circle, Testbed } from "planck"; + +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -31,7 +20,9 @@ const bodies: Body[] = []; const shapes: Shape[] = []; { - const ground = world.createBody(); + const ground = world.createBody({ + type: "static", + }); let x1 = -20.0; let y1 = 2.0 * Math.cos((x1 / 10.0) * Math.PI); @@ -88,20 +79,15 @@ function createItem(index: number) { world.destroyBody(bodies.shift()!); } - const bd: BodyDef = { + const body = world.createBody({ + type: "dynamic", position: { - x: Math.random(-10.0, 10.0), - y: Math.random(10.0, 20.0), + x: Math.random() * 20 - 10.0, + y: Math.random() * 10 + 10.0, }, - angle: Math.random(-Math.PI, Math.PI), - type: "dynamic", - }; - - if (index === 4) { - bd.angularDamping = 0.02; - } - - const body = world.createBody(bd); + angle: Math.random() * 2 * Math.PI - Math.PI, + angularDamping: index === 4 ? 0.02 : 0, + }); body.createFixture(shapes[index], { density: 20.0, diff --git a/example/EdgeTest.ts b/example/EdgeTest.ts index 77bb29a8..a38fe808 100644 --- a/example/EdgeTest.ts +++ b/example/EdgeTest.ts @@ -5,12 +5,16 @@ import { World, Circle, Box, Edge, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); const v1 = { x: -10.0, y: 0.0 }; const v2 = { x: -7.0, y: -2.0 }; diff --git a/example/Gears.ts b/example/Gears.ts index 50446d85..b16be3c5 100644 --- a/example/Gears.ts +++ b/example/Gears.ts @@ -14,24 +14,37 @@ import { Testbed, } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: 50.0, y: 0.0 }, { x: -50.0, y: 0.0 })); const radius1 = 1.0; const radius2 = 2.0; -const gearA1 = world.createBody({ x: 10.0, y: 9.0 }); +const gearA1 = world.createBody({ + type: "static", + position: { x: 10.0, y: 9.0 }, +}); gearA1.createFixture(new Circle(radius1), 5.0); -const plankA1 = world.createDynamicBody({ x: 10.0, y: 8.0 }); +const plankA1 = world.createBody({ + type: "dynamic", + position: { x: 10.0, y: 8.0 }, +}); plankA1.createFixture(new Box(0.5, 5.0), 5.0); -const gearA2 = world.createDynamicBody({ x: 10.0, y: 6.0 }); +const gearA2 = world.createBody({ + type: "dynamic", + position: { x: 10.0, y: 6.0 }, +}); gearA2.createFixture(new Circle(radius2), 5.0); const jointA1 = world.createJoint(new RevoluteJoint({}, plankA1, gearA1, gearA1.getPosition())); @@ -39,17 +52,26 @@ const jointA2 = world.createJoint(new RevoluteJoint({}, plankA1, gearA2, gearA2. world.createJoint(new GearJoint({}, gearA1, gearA2, jointA1!, jointA2!, radius2 / radius1)); -const gearB1 = world.createDynamicBody({ x: -3.0, y: 12.0 }); +const gearB1 = world.createBody({ + type: "dynamic", + position: { x: -3.0, y: 12.0 }, +}); gearB1.createFixture(new Circle(1.0), 5.0); const jointB1 = world.createJoint(new RevoluteJoint({}, ground, gearB1, gearB1.getPosition())); -const gearB2 = world.createDynamicBody({ x: 0.0, y: 12.0 }); +const gearB2 = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 12.0 }, +}); gearB2.createFixture(new Circle(2.0), 5.0); const jointB2 = world.createJoint(new RevoluteJoint({}, ground, gearB2, gearB2.getPosition())); -const plankB1 = world.createDynamicBody({ x: 2.5, y: 12.0 }); +const plankB1 = world.createBody({ + type: "dynamic", + position: { x: 2.5, y: 12.0 }, +}); plankB1.createFixture(new Box(0.5, 5.0), 5.0); const jointB3 = world.createJoint( diff --git a/example/HeavyOnLight.ts b/example/HeavyOnLight.ts index 82919c0f..4b37ab22 100644 --- a/example/HeavyOnLight.ts +++ b/example/HeavyOnLight.ts @@ -5,13 +5,26 @@ import { World, Edge, Circle, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -world.createBody().createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); +const ground = world.createBody({ + type: "static", +}); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); -world.createDynamicBody({ x: 0.0, y: 4.5 }).createFixture(new Circle(0.5), 10.0); +const light = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 4.5 }, +}); +light.createFixture(new Circle(0.5), 10.0); -world.createDynamicBody({ x: 0.0, y: 10.0 }).createFixture(new Circle(5.0), 10.0); +const heavy = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 10.0 }, +}); +heavy.createFixture(new Circle(5.0), 10.0); diff --git a/example/HeavyOnLightTwo.ts b/example/HeavyOnLightTwo.ts index b1bf6ea0..3f7975d9 100644 --- a/example/HeavyOnLightTwo.ts +++ b/example/HeavyOnLightTwo.ts @@ -5,17 +5,30 @@ import { World, Body, Circle, Edge, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.info("X: Add/Remove heavy circle"); testbed.start(world); -world.createBody().createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); +const ground = world.createBody({ + type: "static", +}); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); -world.createDynamicBody({ x: 0.0, y: 2.5 }).createFixture(new Circle(0.5), 10.0); +const light1 = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 2.5 }, +}); +light1.createFixture(new Circle(0.5), 10.0); -world.createDynamicBody({ x: 0.0, y: 3.5 }).createFixture(new Circle(0.5), 10.0); +const light2 = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 3.5 }, +}); +light2.createFixture(new Circle(0.5), 10.0); let heavy: Body | null = null; @@ -24,7 +37,10 @@ function toggleHeavy() { world.destroyBody(heavy); heavy = null; } else { - heavy = world.createDynamicBody({ x: 0.0, y: 9.0 }); + heavy = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 9.0 }, + }); heavy.createFixture(new Circle(5.0), 10.0); } } diff --git a/example/Mixer.ts b/example/Mixer.ts index 7602eb74..83170f85 100644 --- a/example/Mixer.ts +++ b/example/Mixer.ts @@ -1,12 +1,17 @@ import { World, Edge, Circle, Box, Chain, Math, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.y = 0; testbed.start(world); -const container = world.createKinematicBody(); +const container = world.createBody({ + type: "kinematic", +}); + container.createFixture(new Edge({ x: 15, y: -5 }, { x: 25, y: 5 })); container.createFixture(new Circle({ x: -10, y: -10 }, 3)); container.createFixture(new Circle({ x: 10, y: 10 }, 3)); @@ -29,7 +34,10 @@ const n = 15; for (let i = -n; i <= n; i++) { for (let j = -n; j <= n; j++) { - const particle = world.createDynamicBody({ x: i * 1, y: j * 1 }); + const particle = world.createBody({ + type: "dynamic", + position: { x: i * 1, y: j * 1 }, + }); particle.createFixture(Math.random() > 0.5 ? new Circle(0.4) : new Box(0.4, 0.4)); particle.setMassData({ mass: 2, diff --git a/example/Mobile.ts b/example/Mobile.ts index fd779914..1328d047 100644 --- a/example/Mobile.ts +++ b/example/Mobile.ts @@ -5,7 +5,9 @@ import { Vec2, World, Box, RevoluteJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -1 }); +const world = new World({ + gravity: { x: 0, y: -1 }, +}); const testbed = Testbed.mount(); testbed.y = -15; @@ -16,7 +18,10 @@ testbed.start(world); const DEPTH = 4; const DENSITY = 20.0; -const ground = world.createBody({ x: 0.0, y: 20.0 }); +const ground = world.createBody({ + type: "static", + position: { x: 0.0, y: 20.0 }, +}); const a = 0.5; const h = { x: 0.0, y: a }; diff --git a/example/MobileBalanced.ts b/example/MobileBalanced.ts index e7855814..fb25b2e6 100644 --- a/example/MobileBalanced.ts +++ b/example/MobileBalanced.ts @@ -5,7 +5,9 @@ import { World, Vec2, Box, RevoluteJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.y = -15; @@ -16,7 +18,10 @@ testbed.start(world); const DEPTH = 4; const DENSITY = 20.0; -const ground = world.createBody({ x: 0.0, y: 20.0 }); +const ground = world.createBody({ + type: "static", + position: { x: 0.0, y: 20.0 }, +}); const a = 0.5; const h = { x: 0.0, y: a }; @@ -37,7 +42,10 @@ function addNode(parent, localAnchor, depth, offset, a) { const p = new Vec2(parent.getPosition()).add(localAnchor).sub(h); - const node = world.createDynamicBody(p); + const node = world.createBody({ + type: "dynamic", + position: p, + }); node.createFixture(new Box(0.25 * a, a), DENSITY); diff --git a/example/MotorJoint.ts b/example/MotorJoint.ts index 2b02bb76..f666d71b 100644 --- a/example/MotorJoint.ts +++ b/example/MotorJoint.ts @@ -9,18 +9,25 @@ import { World, MotorJoint, Box, Edge, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); let time = 0; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 })); // Define motorized body -const body = world.createDynamicBody({ x: 0.0, y: 8.0 }); +const body = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 8.0 }, +}); body.createFixture(new Box(2.0, 0.5), { friction: 0.6, density: 2.0, diff --git a/example/OneSidedPlatform.ts b/example/OneSidedPlatform.ts index 68d4a191..23ddd14e 100644 --- a/example/OneSidedPlatform.ts +++ b/example/OneSidedPlatform.ts @@ -5,7 +5,9 @@ import { World, Edge, Box, Circle, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -21,15 +23,23 @@ const BELOW = -1; const state = UNKNOWN; // Ground -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }), 0.0); // Platform -const platform = world.createBody({ x: 0.0, y: 10.0 }); +const platform = world.createBody({ + type: "static", + position: { x: 0.0, y: 10.0 }, +}); const platformFix = platform.createFixture(new Box(3.0, 0.5), 0.0); // Actor -const character = world.createDynamicBody({ x: 0.0, y: 12.0 }); +const character = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 12.0 }, +}); const characterFix = character.createFixture(new Circle(radius), 20.0); character.setLinearVelocity({ x: 0.0, y: -50.0 }); diff --git a/example/Pinball.ts b/example/Pinball.ts index fe6fe59d..8bc11079 100644 --- a/example/Pinball.ts +++ b/example/Pinball.ts @@ -8,13 +8,17 @@ import { World, Circle, Box, Chain, RevoluteJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); // Ground body -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture( new Chain( [ @@ -33,8 +37,14 @@ ground.createFixture( const pLeft = { x: -2.0, y: 0.0 }; const pRight = { x: 2.0, y: 0.0 }; -const leftFlipper = world.createDynamicBody({ x: -2.0, y: 0.0 }); -const rightFlipper = world.createDynamicBody({ x: 2.0, y: 0.0 }); +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 }, +}); leftFlipper.createFixture(new Box(1.75, 0.1), 1.0); rightFlipper.createFixture(new Box(1.75, 0.1), 1.0); @@ -72,8 +82,8 @@ world.createJoint(rightJoint); // Circle character const ball = world.createBody({ - position: { x: 1.0, y: 15.0 }, type: "dynamic", + position: { x: 1.0, y: 15.0 }, bullet: true, }); ball.createFixture(new Circle(0.2), 1.0); diff --git a/example/PolyCollision.ts b/example/PolyCollision.ts index dbf41644..62c67ef6 100644 --- a/example/PolyCollision.ts +++ b/example/PolyCollision.ts @@ -5,7 +5,9 @@ import { World, Transform, Manifold, CollidePolygons, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.info("Use arrow keys to move and Z or X to rotate."); diff --git a/example/PolyShapes.ts b/example/PolyShapes.ts index 8f9fdcb8..bf7002c0 100644 --- a/example/PolyShapes.ts +++ b/example/PolyShapes.ts @@ -15,7 +15,6 @@ import { Testbed, Body, Shape, - BodyDef, Fixture, PolygonShape, } from "planck"; @@ -26,7 +25,10 @@ import { // overlap a circle. Up to 4 overlapped fixtures will be highlighted with a // yellow border. -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); + const testbed = Testbed.mount(); testbed.start(world); @@ -36,7 +38,9 @@ const bodies: Body[] = []; const shapes: Shape[] = []; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); shapes[0] = new Polygon([ @@ -77,17 +81,12 @@ function createBody(index: number) { world.destroyBody(bodies.shift()!); } - const bd: BodyDef = { + const body = world.createBody({ type: "dynamic", position: { x: Math.random() * 0.4 - 2.0, y: 10.0 }, angle: Math.random() * 2 * Math.PI - Math.PI, - }; - - if (index === 4) { - bd.angularDamping = 0.02; - } - - const body = world.createBody(bd); + angularDamping: index === 4 ? 0.02 : 0, + }); body.createFixture(shapes[index % shapes.length], { density: 1.0, diff --git a/example/Prismatic.ts b/example/Prismatic.ts index 96eaeff4..a252f434 100644 --- a/example/Prismatic.ts +++ b/example/Prismatic.ts @@ -7,14 +7,18 @@ import { World, Vec2, PrismaticJoint, Edge, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); const MOTOR_SPEED = 10; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const body = world.createBody({ diff --git a/example/Pulleys.ts b/example/Pulleys.ts index 525ddf58..9181e540 100644 --- a/example/Pulleys.ts +++ b/example/Pulleys.ts @@ -5,7 +5,9 @@ import { World, Circle, Box, PulleyJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -15,7 +17,9 @@ const L = 12.0; const a = 1.0; const b = 2.0; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); // ground.createFixture(new Edge(({ x: -40.0, y: 0.0 }), ({ x: 40.0, y: 0.0 })), 0.0); @@ -25,10 +29,16 @@ ground.createFixture(new Circle({ x: 10.0, y: y + b + L }, 2.0), 0.0); const shape = new Box(a, b); // bd.fixedRotation = true; -const box1 = world.createDynamicBody({ x: -10.0, y: y }); +const box1 = world.createBody({ + type: "dynamic", + position: { x: -10.0, y: y }, +}); box1.createFixture(shape, 5.0); -const box2 = world.createDynamicBody({ x: 10.0, y: y }); +const box2 = world.createBody({ + type: "dynamic", + position: { x: 10.0, y: y }, +}); box2.createFixture(shape, 5.0); const anchor1 = { x: -10.0, y: y + b }; diff --git a/example/Pyramid.ts b/example/Pyramid.ts index 07cc7acd..44c7b497 100644 --- a/example/Pyramid.ts +++ b/example/Pyramid.ts @@ -5,14 +5,18 @@ import { World, Edge, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); const COUNT = 20; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const a = 0.5; @@ -27,7 +31,11 @@ for (let i = 0; i < COUNT; ++i) { y.x = x.x; y.y = x.y; for (let j = i; j < COUNT; ++j) { - world.createDynamicBody(y).createFixture(box, 5.0); + const body = world.createBody({ + type: "dynamic", + position: y, + }); + body.createFixture(box, 5.0); y.x += deltaY.x; y.y += deltaY.y; diff --git a/example/RayCast.ts b/example/RayCast.ts index 0684f70a..9e3a0eec 100644 --- a/example/RayCast.ts +++ b/example/RayCast.ts @@ -10,7 +10,6 @@ import { World, Body, - BodyDef, Fixture, Shape, Vec2, @@ -139,7 +138,9 @@ function callbackMultiple( return 1.0; } -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.width = 40; @@ -195,19 +196,16 @@ function createBody(index: number) { world.destroyBody(bodies.shift()!); } - const x = Math.random() * 20 - 10; - const y = Math.random() * 20; - - const bd: BodyDef = {}; - bd.position = { x: x, y: y }; - bd.angle = Math.random() * 2 * Math.PI - Math.PI; - bd.userData = index; - - if (index === 4) { - bd.angularDamping = 0.02; - } - - const body = world.createBody(bd); + const body = world.createBody({ + type: "static", + position: { + x: Math.random() * 20 - 10, + y: Math.random() * 20, + }, + angle: Math.random() * 2 * Math.PI - Math.PI, + userData: index, + angularDamping: index === 4 ? 0.02 : 0, + }); const shape = shapes[index % shapes.length]; diff --git a/example/Revolute.ts b/example/Revolute.ts index 3a4d6e7f..20716171 100644 --- a/example/Revolute.ts +++ b/example/Revolute.ts @@ -5,13 +5,17 @@ import { World, Edge, Circle, Box, Polygon, RevoluteJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); testbed.info("Z: Limits, X: Motor"); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); const groundFD = { filterCategoryBits: 2, @@ -20,7 +24,10 @@ const groundFD = { }; ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), groundFD); -const rotator = world.createDynamicBody({ x: -10.0, y: 20.0 }); +const rotator = world.createBody({ + type: "dynamic", + position: { x: -10.0, y: 20.0 }, +}); rotator.createFixture(new Circle(0.5), 5.0); const w = 100.0; @@ -44,15 +51,18 @@ const joint = world.createJoint( ), ); -const ball = world.createDynamicBody({ x: 5.0, y: 30.0 }); +const ball = world.createBody({ + type: "dynamic", + position: { x: 5.0, y: 30.0 }, +}); ball.createFixture(new Circle(3.0), { density: 5.0, // filterMaskBits: 1, }); const platform = world.createBody({ - position: { x: 20.0, y: 10.0 }, type: "dynamic", + position: { x: 20.0, y: 10.0 }, bullet: true, }); platform.createFixture(new Box(10.0, 0.2, { x: -10.0, y: 0.0 }, 0.0), 2.0); @@ -71,7 +81,9 @@ world.createJoint( ); // Tests mass computation of a small object far from the origin -const triangle = world.createDynamicBody(); +const triangle = world.createBody({ + type: "dynamic", +}); triangle.createFixture( new Polygon([ diff --git a/example/RopeJoint.ts b/example/RopeJoint.ts index d328890b..1d109c2a 100644 --- a/example/RopeJoint.ts +++ b/example/RopeJoint.ts @@ -14,51 +14,58 @@ import { World, Edge, Box, RevoluteJoint, RopeJoint, Testbed, BodyDef } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.info("X: Toggle the rope joint"); testbed.start(world); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); -const segmentDef = { - density: 20.0, - friction: 0.2, - filterCategoryBits: 0x0001, - filterMaskBits: 0xffff & ~0x0002, -}; - -const segmentJointDef = { - collideConnected: false, -}; - const N = 10; const y = 15.0; let prevBody = ground; for (let i = 0; i < N; ++i) { let shape = new Box(0.5, 0.125); - const bd: BodyDef = { - type: "dynamic", + + const fixDef = { + density: 20.0, + friction: 0.2, + filterCategoryBits: 0x0001, + filterMaskBits: 0xffff & ~0x0002, + }; + + const bodyDef = { + type: "dynamic" as const, position: { x: 0.5 + 1.0 * i, y: y }, + angularDamping: 0, }; + + const jointDef = { + collideConnected: false, + }; + if (i === N - 1) { shape = new Box(1.5, 1.5); - segmentDef.density = 100.0; - segmentDef.filterCategoryBits = 0x0002; - bd.position = { x: 1.0 * i, y: y }; - bd.angularDamping = 0.4; + fixDef.density = 100.0; + fixDef.filterCategoryBits = 0x0002; + bodyDef.position = { x: 1.0 * i, y: y }; + bodyDef.angularDamping = 0.4; } - const body = world.createBody(bd); + const body = world.createBody(bodyDef); - body.createFixture(shape, segmentDef); + body.createFixture(shape, fixDef); const anchor = { x: i, y: y }; - world.createJoint(new RevoluteJoint(segmentJointDef, prevBody, body, anchor)); + world.createJoint(new RevoluteJoint(jointDef, prevBody, body, anchor)); prevBody = body; } diff --git a/example/SensorTest.ts b/example/SensorTest.ts index 624bd508..15de6dd5 100644 --- a/example/SensorTest.ts +++ b/example/SensorTest.ts @@ -7,7 +7,9 @@ import { World, Body, Fixture, Vec2, CircleShape, Box, Edge, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -22,7 +24,9 @@ let sensor: Fixture; const bodies: Body[] = []; const touching: UserDate[] = []; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); if (0) { @@ -42,7 +46,10 @@ const circle = new CircleShape(1.0); for (let i = 0; i < COUNT; ++i) { touching[i] = { touching: false }; - bodies[i] = world.createDynamicBody({ x: -10.0 + 3.0 * i, y: 20.0 }); + bodies[i] = world.createBody({ + type: "dynamic", + position: { x: -10.0 + 3.0 * i, y: 20.0 }, + }); bodies[i].setUserData(touching[i]); bodies[i].createFixture(circle, 1.0); } diff --git a/example/ShapeEditing.ts b/example/ShapeEditing.ts index 6a532363..0ece55eb 100644 --- a/example/ShapeEditing.ts +++ b/example/ShapeEditing.ts @@ -5,7 +5,9 @@ import { World, Fixture, Edge, Circle, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.info("C: Create a shape, X: Destroy a shape, Z: Sensor"); @@ -13,10 +15,15 @@ testbed.start(world); let sensor = true; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); -const body = world.createDynamicBody({ x: 0.0, y: 10.0 }); +const body = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 10.0 }, +}); const fixture1 = body.createFixture(new Box(4.0, 4.0, { x: 0.0, y: 0.0 }, 0.0), 10.0); diff --git a/example/Shuffle.ts b/example/Shuffle.ts index a325677c..3d72c64e 100644 --- a/example/Shuffle.ts +++ b/example/Shuffle.ts @@ -28,24 +28,31 @@ const walls = [ const wallFixDef = { userData: "wall", }; + const ballFixDef = { friction: 0.1, restitution: 0.98, density: 0.8, userData: "ball", }; + const ballBodyDef = { + type: "dynamic" as const, bullet: true, linearDamping: 1.6, angularDamping: 1.6, }; -world.createBody().createFixture(new Chain(walls, true), wallFixDef); +world + .createBody({ + type: "static", + }) + .createFixture(new Chain(walls, true), wallFixDef); row(1, 8, BALL_R, BALL_D) .map((v) => ({ x: v.x + height * 0.4, y: v.y + 0 })) .forEach(function (p) { - const ball = world.createDynamicBody(ballBodyDef); + const ball = world.createBody(ballBodyDef); ball.setPosition(p); ball.setAngle(Math.PI); ball.createFixture(new Circle(BALL_R), ballFixDef); @@ -55,7 +62,7 @@ row(1, 8, BALL_R, BALL_D) row(1, 8, BALL_R, BALL_D) .map((v) => ({ x: v.x + -height * 0.4, y: v.y + 0 })) .forEach(function (p) { - const ball = world.createDynamicBody(ballBodyDef); + const ball = world.createBody(ballBodyDef); ball.setPosition(p); ball.createFixture(new Circle(BALL_R), ballFixDef); ball.style = { fill: "#0077ff", stroke: "black" }; diff --git a/example/SliderCrank.ts b/example/SliderCrank.ts index 77abfa6e..0151cc88 100644 --- a/example/SliderCrank.ts +++ b/example/SliderCrank.ts @@ -7,17 +7,24 @@ import { World, RevoluteJoint, PrismaticJoint, Edge, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); testbed.info("Z: Toggle friction, X: Toggle motor"); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); // Define crank. -const crank = world.createDynamicBody({ x: 0.0, y: 7.0 }); +const crank = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 7.0 }, +}); crank.createFixture(new Box(0.5, 2.0), 2.0); const joint1 = world.createJoint( @@ -34,7 +41,10 @@ const joint1 = world.createJoint( ); // Define follower. -const follower = world.createDynamicBody({ x: 0.0, y: 13.0 }); +const follower = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 13.0 }, +}); follower.createFixture(new Box(0.5, 4.0), 2.0); world.createJoint(new RevoluteJoint({ enableMotor: false }, crank, follower, { x: 0.0, y: 9.0 })); @@ -63,7 +73,10 @@ const joint2 = world.createJoint( ); // Create a payload -const payload = world.createDynamicBody({ x: 0.0, y: 23.0 }); +const payload = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 23.0 }, +}); payload.createFixture(new Box(1.5, 1.5), 2.0); testbed.keydown = function (code, char) { diff --git a/example/Soccer.ts b/example/Soccer.ts index 68f2df78..bf3ca3a6 100644 --- a/example/Soccer.ts +++ b/example/Soccer.ts @@ -42,6 +42,7 @@ const ballFixDef = { userData: "ball", }; const ballBodyDef = { + type: "dynamic" as const, bullet: true, linearDamping: 3.5, angularDamping: 1.6, @@ -54,16 +55,22 @@ const playerFixDef = { userData: "player", }; const playerBodyDef = { + type: "dynamic" as const, bullet: true, linearDamping: 4, angularDamping: 1.6, }; -world.createBody().createFixture(new Chain(walls(), true), wallFixDef); +world + .createBody({ + type: "static", + }) + .createFixture(new Chain(walls(), true), wallFixDef); { // goal left const body = world.createBody({ + type: "static", position: { x: -width * 0.5 - BALL_R, y: 0 }, }); const fixture = body.createFixture(new Chain(goal), goalFixDef); @@ -72,17 +79,18 @@ world.createBody().createFixture(new Chain(walls(), true), wallFixDef); { // goal right const body = world.createBody({ + type: "static", position: { x: +width * 0.5 + BALL_R, y: 0 }, }); const fixture = body.createFixture(new Chain(goal), goalFixDef); } -const ball = world.createDynamicBody(ballBodyDef); +const ball = world.createBody(ballBodyDef); ball.createFixture(new Circle(BALL_R), ballFixDef); ball.style = { fill: "white", stroke: "black" }; team().forEach(function (p) { - const player = world.createDynamicBody(playerBodyDef); + const player = world.createBody(playerBodyDef); player.setPosition(p); player.createFixture(new Circle(PLAYER_R), playerFixDef); player.style = { fill: "#0077ff", stroke: "black" }; @@ -91,7 +99,7 @@ team().forEach(function (p) { team() .map((v) => ({ x: -v.x, y: v.y })) .forEach(function (p) { - const player = world.createDynamicBody(playerBodyDef); + const player = world.createBody(playerBodyDef); player.setPosition(p); player.setAngle(Math.PI); player.createFixture(new Circle(PLAYER_R), playerFixDef); diff --git a/example/SphereStack.ts b/example/SphereStack.ts index 9a8b3ad0..7f1c352d 100644 --- a/example/SphereStack.ts +++ b/example/SphereStack.ts @@ -5,7 +5,9 @@ import { World, Body, Edge, Circle, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -13,13 +15,18 @@ testbed.start(world); const COUNT = 10; const bodies: Body[] = []; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const circle = new Circle(1.0); for (let i = 0; i < COUNT; ++i) { - bodies[i] = world.createDynamicBody({ x: 0.0, y: 4.0 + 3.0 * i }); + bodies[i] = world.createBody({ + type: "dynamic", + position: { x: 0.0, y: 4.0 + 3.0 * i }, + }); bodies[i].createFixture(circle, 1.0); bodies[i].setLinearVelocity({ x: 0.0, y: -50.0 }); } diff --git a/example/TheoJansen.ts b/example/TheoJansen.ts index 8aa45c8c..7b51dcb3 100644 --- a/example/TheoJansen.ts +++ b/example/TheoJansen.ts @@ -19,7 +19,9 @@ import { Vec2Value, } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -31,24 +33,36 @@ const offset = { x: 0.0, y: 8.0 }; const pivot = { x: 0.0, y: 0.8 }; // Ground -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -50.0, y: 0.0 }, { x: 50.0, y: 0.0 }), 0.0); ground.createFixture(new Edge({ x: -50.0, y: 0.0 }, { x: -50.0, y: 10.0 }), 0.0); ground.createFixture(new Edge({ x: 50.0, y: 0.0 }, { x: 50.0, y: 10.0 }), 0.0); // Balls for (let i = 0; i < 40; ++i) { - world.createDynamicBody({ x: -40.0 + 2.0 * i, y: 0.5 }).createFixture(new Circle(0.25), 1.0); + const ball = world.createBody({ + type: "dynamic", + position: { x: -40.0 + 2.0 * i, y: 0.5 }, + }); + ball.createFixture(new Circle(0.25), 1.0); } // Chassis -const chassis = world.createDynamicBody(Vec2.add(pivot, offset)); +const chassis = world.createBody({ + type: "dynamic", + position: Vec2.add(pivot, offset), +}); chassis.createFixture(new Box(2.5, 1.0), { density: 1.0, filterGroupIndex: -1, }); -const wheel = world.createDynamicBody(Vec2.add(pivot, offset)); +const wheel = world.createBody({ + type: "dynamic", + position: Vec2.add(pivot, offset), +}); wheel.createFixture(new Circle(1.6), { density: 1.0, filterGroupIndex: -1, @@ -99,7 +113,8 @@ function createLeg(s: number, wheelAnchor: Vec2Value) { poly2 = new PolygonShape([{ x: 0, y: 0 }, Vec2.sub(p6, p4), Vec2.sub(p5, p4)]); } - const body1 = world.createDynamicBody({ + const body1 = world.createBody({ + type: "dynamic", position: offset, angularDamping: 10.0, }); @@ -108,7 +123,8 @@ function createLeg(s: number, wheelAnchor: Vec2Value) { filterGroupIndex: -1, }); - const body2 = world.createDynamicBody({ + const body2 = world.createBody({ + type: "dynamic", position: Vec2.add(p4, offset), angularDamping: 10.0, }); diff --git a/example/Tiles.ts b/example/Tiles.ts index acbb914e..6a24dbcf 100644 --- a/example/Tiles.ts +++ b/example/Tiles.ts @@ -8,7 +8,9 @@ import { World, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); @@ -19,7 +21,10 @@ let fixtureCount = 0; { const a = 0.5; - const ground = world.createBody({ x: 0, y: -a }); + const ground = world.createBody({ + type: "static", + position: { x: 0, y: -a }, + }); if (true) { const N = 200; @@ -66,7 +71,10 @@ let fixtureCount = 0; for (let j = i; j < COUNT; ++j) { // bd.allowSleep = !(i == 0 && j == 0) - const body = world.createDynamicBody(y); + const body = world.createBody({ + type: "dynamic", + position: y, + }); body.createFixture(shape, 5.0); ++fixtureCount; y.x += deltaY.x; diff --git a/example/Tumbler.ts b/example/Tumbler.ts index 5795902c..53acf8b3 100644 --- a/example/Tumbler.ts +++ b/example/Tumbler.ts @@ -5,18 +5,23 @@ import { World, Box, RevoluteJoint, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); const COUNT = 200; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); -const container = world.createDynamicBody({ - allowSleep: false, +const container = world.createBody({ + type: "dynamic", position: { x: 0, y: 10 }, + allowSleep: false, }); container.createFixture(new Box(0.5, 20, { x: 20, y: 0 }, 0), 5); @@ -40,10 +45,12 @@ world.createJoint( const shape = new Box(0.5, 0.5); let count = 0; while (count < COUNT) { - const body = world.createDynamicBody(); - body.setPosition({ - x: Math.random() * 20 - 10, - y: 10 + Math.random() * 20 - 10, + const body = world.createBody({ + type: "dynamic", + position: { + x: Math.random() * 20 - 10, + y: 10 + Math.random() * 20 - 10, + }, }); body.createFixture(shape, 1); ++count; diff --git a/example/VaryingFriction.ts b/example/VaryingFriction.ts index 4ce82593..fca3ba89 100644 --- a/example/VaryingFriction.ts +++ b/example/VaryingFriction.ts @@ -5,29 +5,66 @@ import { World, Edge, Box, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -world.createBody().createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); +world + .createBody({ + type: "static", + }) + .createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); -world.createBody({ x: -4.0, y: 22.0 }, -0.25).createFixture(new Box(13.0, 0.25), 0.0); +world + .createBody({ + type: "static", + position: { x: -4.0, y: 22.0 }, + angle: -0.25, + }) + .createFixture(new Box(13.0, 0.25), 0.0); -world.createBody({ x: 10.5, y: 19.0 }).createFixture(new Box(0.25, 1.0), 0.0); +world + .createBody({ + type: "static", + position: { x: 10.5, y: 19.0 }, + }) + .createFixture(new Box(0.25, 1.0), 0.0); -world.createBody({ x: 4.0, y: 14.0 }, 0.25).createFixture(new Box(13.0, 0.25), 0.0); +world + .createBody({ + type: "static", + position: { x: 4.0, y: 14.0 }, + angle: 0.25, + }) + .createFixture(new Box(13.0, 0.25), 0.0); -world.createBody({ x: -10.5, y: 11.0 }).createFixture(new Box(0.25, 1.0), 0.0); +world + .createBody({ + type: "static", + position: { x: -10.5, y: 11.0 }, + }) + .createFixture(new Box(0.25, 1.0), 0.0); -world.createBody({ x: -4.0, y: 6.0 }, -0.25).createFixture(new Box(13.0, 0.25), 0.0); +world + .createBody({ + type: "static", + position: { x: -4.0, y: 6.0 }, + angle: -0.25, + }) + .createFixture(new Box(13.0, 0.25), 0.0); const friction = [0.75, 0.5, 0.35, 0.1, 0.0]; const circle = new Box(0.5, 0.5); for (let i = 0; i < friction.length; ++i) { - const ball = world.createDynamicBody({ x: -15.0 + 4.0 * i, y: 28.0 }); + const ball = world.createBody({ + type: "dynamic", + position: { x: -15.0 + 4.0 * i, y: 28.0 }, + }); ball.createFixture(circle, { density: 25.0, friction: friction[i], diff --git a/example/VaryingRestitution.ts b/example/VaryingRestitution.ts index 5c55d1e7..2afbd967 100644 --- a/example/VaryingRestitution.ts +++ b/example/VaryingRestitution.ts @@ -8,12 +8,16 @@ import { World, Circle, Edge, Testbed } from "planck"; -const world = new World({ x: 0, y: -10 }); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); const restitution = [0.0, 0.1, 0.3, 0.5, 0.75, 0.9, 1.0]; @@ -21,7 +25,10 @@ const restitution = [0.0, 0.1, 0.3, 0.5, 0.75, 0.9, 1.0]; const circle = new Circle(1.0); for (let i = 0; i < restitution.length; ++i) { - const ball = world.createDynamicBody({ x: -10.0 + 3.0 * i, y: 20.0 }); + const ball = world.createBody({ + type: "dynamic", + position: { x: -10.0 + 3.0 * i, y: 20.0 }, + }); ball.createFixture(circle, { density: 1.0, restitution: restitution[i], diff --git a/example/VerticalStack.ts b/example/VerticalStack.ts index dc62755f..dc28036b 100644 --- a/example/VerticalStack.ts +++ b/example/VerticalStack.ts @@ -21,7 +21,9 @@ let bullet: Body | null = null; const bodies: Body[] = []; const indices: number[] = []; -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); ground.createFixture(new Edge({ x: 20.0, y: 0.0 }, { x: 20.0, y: 20.0 })); @@ -37,9 +39,11 @@ for (let j = 0; j < columnCount; ++j) { // let x = Math.random() * 0.04 - 0.02; // let x = i % 2 == 0 ? -0.01 : 0.01; - const body = world.createDynamicBody(); - body.setUserData(indices[n]); - body.setPosition({ x: xs[j] + x, y: 0.55 + 1.1 * i }); + const body = world.createBody({ + type: "dynamic", + userData: indices[n], + position: { x: xs[j] + x, y: 0.55 + 1.1 * i }, + }); body.createFixture(shape, { density: 1.0, friction: 0.3, @@ -52,24 +56,7 @@ for (let j = 0; j < columnCount; ++j) { testbed.keydown = function (code, char) { switch (char) { case "X": - if (bullet != null) { - world.destroyBody(bullet); - bullet = null; - } - - bullet = world.createBody({ - type: "dynamic", - bullet: true, - position: { x: -31.0, y: 5.0 }, - }); - - bullet.createFixture({ - shape: new Circle(0.25), - density: 20.0, - restitution: 0.05, - }); - - bullet.setLinearVelocity({ x: 400.0, y: 0.0 }); + fireBullet(); break; case "Z": @@ -85,25 +72,29 @@ testbed.step = function () { testbed.status("Blocksolve", world.m_blockSolve); if (stepCount++ % 300 == 0) { - if (bullet != null) { - world.destroyBody(bullet); - bullet = null; - } + fireBullet(); + } +}; - bullet = world.createBody({ - type: "dynamic", - bullet: true, - position: { x: -31.0, y: 5.0 }, - }); - bullet.createFixture({ - shape: new Circle(0.25), - density: 20.0, - restitution: 0.05, - }); +function fireBullet() { + if (bullet != null) { + world.destroyBody(bullet); + bullet = null; + } - bullet.setLinearVelocity({ + bullet = world.createBody({ + type: "dynamic", + bullet: true, + position: { x: -31.0, y: 5.0 }, + linearVelocity: { x: 400.0, y: Math.random() * 100 - 50, - }); - } -}; + }, + }); + + bullet.createFixture({ + shape: new Circle(0.25), + density: 20.0, + restitution: 0.05, + }); +} diff --git a/example/Web.ts b/example/Web.ts index 158a09f2..d828ba5f 100644 --- a/example/Web.ts +++ b/example/Web.ts @@ -7,28 +7,44 @@ import { World, Body, Joint, Box, DistanceJoint, Testbed } from "planck"; -const world = new World(); +const world = new World({ + gravity: { x: 0, y: -10 }, +}); const testbed = Testbed.mount(); testbed.start(world); -const ground = world.createBody(); +const ground = world.createBody({ + type: "static", +}); const bodies: Body[] = []; -let joints: Joint[] = []; +const joints: Joint[] = []; const box = new Box(0.5, 0.5); -bodies[0] = world.createDynamicBody({ x: -5.0, y: 5.0 }); +bodies[0] = world.createBody({ + type: "dynamic", + position: { x: -5.0, y: 5.0 }, +}); bodies[0].createFixture(box, 5.0); -bodies[1] = world.createDynamicBody({ x: 5.0, y: 5.0 }); +bodies[1] = world.createBody({ + type: "dynamic", + position: { x: 5.0, y: 5.0 }, +}); bodies[1].createFixture(box, 5.0); -bodies[2] = world.createDynamicBody({ x: 5.0, y: 15.0 }); +bodies[2] = world.createBody({ + type: "dynamic", + position: { x: 5.0, y: 15.0 }, +}); bodies[2].createFixture(box, 5.0); -bodies[3] = world.createDynamicBody({ x: -5.0, y: 15.0 }); +bodies[3] = world.createBody({ + type: "dynamic", + position: { x: -5.0, y: 15.0 }, +}); bodies[3].createFixture(box, 5.0); const jd = { @@ -36,97 +52,91 @@ const jd = { dampingRatio: 0.0, }; -world.createJoint( - (joints[0] = new DistanceJoint({ - ...jd, - bodyA: ground, - localAnchorA: { x: -10.0, y: 0.0 }, - bodyB: bodies[0], - localAnchorB: { x: -0.5, y: -0.5 }, - })), -); - -world.createJoint( - (joints[1] = new DistanceJoint({ - ...jd, - bodyA: ground, - localAnchorA: { x: 10.0, y: 0.0 }, - bodyB: bodies[1], - localAnchorB: { x: 0.5, y: -0.5 }, - })), -); - -world.createJoint( - (joints[2] = new DistanceJoint({ - ...jd, - bodyA: ground, - localAnchorA: { x: 10.0, y: 20.0 }, - bodyB: bodies[2], - localAnchorB: { x: 0.5, y: 0.5 }, - })), -); - -world.createJoint( - (joints[3] = new DistanceJoint({ - ...jd, - bodyA: ground, - localAnchorA: { x: -10.0, y: 20.0 }, - bodyB: bodies[3], - localAnchorB: { x: -0.5, y: 0.5 }, - })), -); - -world.createJoint( - (joints[4] = new DistanceJoint({ - ...jd, - bodyA: bodies[0], - localAnchorA: { x: 0.5, y: 0.0 }, - bodyB: bodies[1], - localAnchorB: { x: -0.5, y: 0.0 }, - })), -); - -world.createJoint( - (joints[5] = new DistanceJoint({ - ...jd, - bodyA: bodies[1], - localAnchorA: { x: 0.0, y: 0.5 }, - bodyB: bodies[2], - localAnchorB: { x: 0.0, y: -0.5 }, - })), -); - -world.createJoint( - (joints[6] = new DistanceJoint({ - ...jd, - bodyA: bodies[2], - localAnchorA: { x: -0.5, y: 0.0 }, - bodyB: bodies[3], - localAnchorB: { x: 0.5, y: 0.0 }, - })), -); - -world.createJoint( - (joints[7] = new DistanceJoint({ - ...jd, - bodyA: bodies[3], - localAnchorA: { x: 0.0, y: -0.5 }, - bodyB: bodies[0], - localAnchorB: { x: 0.0, y: 0.5 }, - })), -); +joints[0] = new DistanceJoint({ + ...jd, + bodyA: ground, + localAnchorA: { x: -10.0, y: 0.0 }, + bodyB: bodies[0], + localAnchorB: { x: -0.5, y: -0.5 }, +}); +world.createJoint(joints[0]); + +joints[1] = new DistanceJoint({ + ...jd, + bodyA: ground, + localAnchorA: { x: 10.0, y: 0.0 }, + bodyB: bodies[1], + localAnchorB: { x: 0.5, y: -0.5 }, +}); +world.createJoint(joints[1]); + +joints[2] = new DistanceJoint({ + ...jd, + bodyA: ground, + localAnchorA: { x: 10.0, y: 20.0 }, + bodyB: bodies[2], + localAnchorB: { x: 0.5, y: 0.5 }, +}); +world.createJoint(joints[2]); + +joints[3] = new DistanceJoint({ + ...jd, + bodyA: ground, + localAnchorA: { x: -10.0, y: 20.0 }, + bodyB: bodies[3], + localAnchorB: { x: -0.5, y: 0.5 }, +}); +world.createJoint(joints[3]); + +joints[4] = new DistanceJoint({ + ...jd, + bodyA: bodies[0], + localAnchorA: { x: 0.5, y: 0.0 }, + bodyB: bodies[1], + localAnchorB: { x: -0.5, y: 0.0 }, +}); +world.createJoint(joints[4]); + +joints[5] = new DistanceJoint({ + ...jd, + bodyA: bodies[1], + localAnchorA: { x: 0.0, y: 0.5 }, + bodyB: bodies[2], + localAnchorB: { x: 0.0, y: -0.5 }, +}); +world.createJoint(joints[5]); + +joints[6] = new DistanceJoint({ + ...jd, + bodyA: bodies[2], + localAnchorA: { x: -0.5, y: 0.0 }, + bodyB: bodies[3], + localAnchorB: { x: 0.5, y: 0.0 }, +}); +world.createJoint(joints[6]); + +joints[7] = new DistanceJoint({ + ...jd, + bodyA: bodies[3], + localAnchorA: { x: 0.0, y: -0.5 }, + bodyB: bodies[0], + localAnchorB: { x: 0.0, y: 0.5 }, +}); +world.createJoint(joints[7]); testbed.keydown = function (code, char) { switch (char) { case "X": if (bodies.length) { - world.destroyBody(bodies.pop()!); + const body = bodies.pop(); + if (body) world.destroyBody(body); } break; case "Z": if (joints.length) { - world.destroyJoint(joints.pop()!); + const joint = joints.pop(); + if (joint) world.destroyJoint(joint); } break; } @@ -135,9 +145,11 @@ testbed.keydown = function (code, char) { testbed.info("This demonstrates a soft distance joint.\nX: Delete a body, Z: Delete a joint"); world.on("remove-joint", function (joint) { - for (let i = 0; i < 8; ++i) { - joints = joints.filter(function (j) { - return j !== joint; - }); - } + const index = joints.indexOf(joint); + if (index > -1) joints.splice(index, 1); +}); + +world.on("remove-body", function (body) { + const index = bodies.indexOf(body); + if (index > -1) bodies.splice(index, 1); });