Skip to content

Commit

Permalink
Unify createBody usage in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shakiba committed Dec 24, 2024
1 parent 3b496b8 commit 5b611d7
Show file tree
Hide file tree
Showing 55 changed files with 1,035 additions and 466 deletions.
5 changes: 4 additions & 1 deletion example/8-Ball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion example/AddPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions example/ApplyForce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
24 changes: 18 additions & 6 deletions example/BasicSliderCrank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand Down
23 changes: 18 additions & 5 deletions example/BodyTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,33 @@

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");
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,
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 11 additions & 5 deletions example/Boxes.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
20 changes: 16 additions & 4 deletions example/Breakable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
42 changes: 33 additions & 9 deletions example/Breakout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
23 changes: 18 additions & 5 deletions example/Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 };
Expand All @@ -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 },
Expand All @@ -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);
}
13 changes: 10 additions & 3 deletions example/BulletTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 5b611d7

Please sign in to comment.