Skip to content

Commit 5b611d7

Browse files
committed
Unify createBody usage in examples
1 parent 3b496b8 commit 5b611d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1035
-466
lines changed

example/8-Ball.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ class BilliardPhysics {
163163
rails.push(topLeftRail.map((v) => ({ x: -v.x, y: -v.y })));
164164

165165
for (let i = 0; i < rails.length; i++) {
166-
const body = this.world.createBody();
166+
const body = this.world.createBody({
167+
type: "static",
168+
});
167169
const shape = new Polygon(rails[i]);
168170
const fixture = body.createFixture(shape, {
169171
friction: 0.1,
@@ -200,6 +202,7 @@ class BilliardPhysics {
200202

201203
for (let i = 0; i < pockets.length; i++) {
202204
const body = this.world.createBody({
205+
type: "static",
203206
position: pockets[i],
204207
});
205208
const shape = new Circle(POCKET_RADIUS);

example/AddPair.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { World, Circle, Box, Testbed } from "planck";
77

8-
const world = new World({ x: 0, y: 0 });
8+
const world = new World();
99

1010
const testbed = Testbed.mount();
1111
testbed.y = 0;

example/ApplyForce.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const testbed = Testbed.mount();
1111
testbed.y = -20;
1212
testbed.start(world);
1313

14-
const ground = world.createBody({ x: 0.0, y: 20.0 });
14+
const ground = world.createBody({
15+
type: "static",
16+
position: { x: 0.0, y: 20.0 },
17+
});
1518

1619
const wallFD = {
1720
density: 0.0,
@@ -72,7 +75,10 @@ const boxFD = {
7275
};
7376

7477
for (let i = 0; i < 10; ++i) {
75-
const box = world.createDynamicBody({ x: 0.0, y: 5.0 + 1.54 * i });
78+
const box = world.createBody({
79+
type: "dynamic",
80+
position: { x: 0.0, y: 5.0 + 1.54 * i },
81+
});
7682

7783
box.createFixture(new Box(0.5, 0.5), boxFD);
7884

example/BasicSliderCrank.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,40 @@
77

88
import { World, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck";
99

10-
const world = new World({ x: 0, y: -10 });
10+
const world = new World({
11+
gravity: { x: 0, y: -10 },
12+
});
1113

1214
const testbed = Testbed.mount();
1315
testbed.y = -15;
1416
testbed.start(world);
1517

16-
const ground = world.createBody({ x: 0.0, y: 17.0 });
18+
const ground = world.createBody({
19+
type: "static",
20+
position: { x: 0.0, y: 17.0 },
21+
});
1722

1823
// Define crank.
19-
const crank = world.createDynamicBody({ x: -8.0, y: 20.0 });
24+
const crank = world.createBody({
25+
type: "dynamic",
26+
position: { x: -8.0, y: 20.0 },
27+
});
2028
crank.createFixture(new Box(4.0, 1.0), 2.0);
2129
world.createJoint(new RevoluteJoint({}, ground, crank, { x: -12.0, y: 20.0 }));
2230

2331
// Define connecting rod
24-
const rod = world.createDynamicBody({ x: 4.0, y: 20.0 });
32+
const rod = world.createBody({
33+
type: "dynamic",
34+
position: { x: 4.0, y: 20.0 },
35+
});
2536
rod.createFixture(new Box(8.0, 1.0), 2.0);
2637
world.createJoint(new RevoluteJoint({}, crank, rod, { x: -4.0, y: 20.0 }));
2738

2839
// Define piston
29-
const piston = world.createDynamicBody({
30-
fixedRotation: true,
40+
const piston = world.createBody({
41+
type: "dynamic",
3142
position: { x: 12.0, y: 20.0 },
43+
fixedRotation: true,
3244
});
3345
piston.createFixture(new Box(3.0, 3.0), 2.0);
3446
world.createJoint(new RevoluteJoint({}, rod, piston, { x: 12.0, y: 20.0 }));

example/BodyTypes.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,33 @@
55

66
import { World, Edge, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck";
77

8-
const world = new World({ x: 0, y: -10 });
8+
const world = new World({
9+
gravity: { x: 0, y: -10 },
10+
});
911

1012
const testbed = Testbed.mount();
1113
testbed.info("Z: Dynamic, X: Static, C: Kinematic");
1214
testbed.start(world);
1315

1416
const SPEED = 3.0;
1517

16-
const ground = world.createBody();
18+
const ground = world.createBody({
19+
type: "static",
20+
});
1721
ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }));
1822

1923
// Define attachment
20-
const attachment = world.createDynamicBody({ x: 0.0, y: 3.0 });
24+
const attachment = world.createBody({
25+
type: "dynamic",
26+
position: { x: 0.0, y: 3.0 },
27+
});
2128
attachment.createFixture(new Box(0.5, 2.0), 2.0);
2229

2330
// Define platform
24-
const platform = world.createDynamicBody({ x: -4.0, y: 5.0 });
31+
const platform = world.createBody({
32+
type: "dynamic",
33+
position: { x: -4.0, y: 5.0 },
34+
});
2535

2636
platform.createFixture(new Box(0.5, 4.0, { x: 4.0, y: 0.0 }, 0.5 * Math.PI), {
2737
friction: 0.6,
@@ -57,7 +67,10 @@ world.createJoint(
5767
);
5868

5969
// Create a payload
60-
const payload = world.createDynamicBody({ x: 0.0, y: 8.0 });
70+
const payload = world.createBody({
71+
type: "dynamic",
72+
position: { x: 0.0, y: 8.0 },
73+
});
6174
payload.createFixture(new Box(0.75, 0.75), { friction: 0.6, density: 2.0 });
6275

6376
testbed.keydown = function (code, char) {

example/Boxes.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import { World, Edge, Box, Testbed } from "planck";
22

3-
const world = new World({ x: 0, y: -10 });
3+
const world = new World({
4+
gravity: { x: 0, y: -10 },
5+
});
46

57
const testbed = Testbed.mount();
68
testbed.start(world);
79

8-
const bar = world.createBody();
10+
const bar = world.createBody({
11+
type: "static",
12+
});
913
bar.createFixture(new Edge({ x: -20, y: 5 }, { x: 20, y: 5 }));
1014
bar.setAngle(0.2);
1115

1216
for (let i = -2; i <= 2; i++) {
1317
for (let j = -2; j <= 2; j++) {
14-
const box = world.createBody().setDynamic();
15-
box.createFixture(new Box(0.5, 0.5));
16-
box.setPosition({ x: i * 1, y: -j * 1 + 20 });
18+
const box = world.createBody({
19+
type: "dynamic",
20+
position: { x: i * 1, y: -j * 1 + 20 },
21+
});
1722
box.setMassData({
1823
mass: 1,
1924
center: { x: 0, y: 0 },
2025
I: 1,
2126
});
27+
box.createFixture(new Box(0.5, 0.5));
2228
}
2329
}

example/Breakable.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import { World, Vec2, Edge, Box, Testbed } from "planck";
99

10-
const world = new World({ x: 0, y: -10 });
10+
const world = new World({
11+
gravity: { x: 0, y: -10 },
12+
});
1113

1214
const testbed = Testbed.mount();
1315
testbed.start(world);
@@ -18,11 +20,17 @@ let breakAngularVelocity: number;
1820
let broke = false;
1921

2022
// Ground body
21-
const ground = world.createBody();
23+
const ground = world.createBody({
24+
type: "static",
25+
});
2226
ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0);
2327

2428
// Breakable dynamic body
25-
const body1 = world.createDynamicBody({ x: 0.0, y: 40.0 }, 0.25 * Math.PI);
29+
const body1 = world.createBody({
30+
type: "dynamic",
31+
position: { x: 0.0, y: 40.0 },
32+
angle: 0.25 * Math.PI,
33+
});
2634

2735
const shape1 = new Box(0.5, 0.5, { x: -0.5, y: 0.0 }, 0.0);
2836
const piece1 = body1.createFixture(shape1, 1.0);
@@ -58,7 +66,11 @@ function Break() {
5866

5967
body1.destroyFixture(piece2);
6068

61-
const body2 = world.createDynamicBody(body1.getPosition(), body1.getAngle());
69+
const body2 = world.createBody({
70+
type: "dynamic",
71+
position: body1.getPosition(),
72+
angle: body1.getAngle(),
73+
});
6274

6375
piece2 = body2.createFixture(shape2, 1.0);
6476

example/Breakout.ts

+33-9
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,45 @@ class BreakoutPhysics {
215215

216216
createBoardPhysics() {
217217
{
218-
const wall = this.world.createBody({ x: +9, y: -0.5 });
218+
const wall = this.world.createBody({
219+
type: "static",
220+
position: { x: +9, y: -0.5 },
221+
});
219222
wall.createFixture(new EdgeShape({ x: 0, y: -12.5 }, { x: 0, y: +11.5 }), wallFix);
220223
}
221224
{
222-
const wall = this.world.createBody({ x: -9, y: -0.5 });
225+
const wall = this.world.createBody({
226+
type: "static",
227+
position: { x: -9, y: -0.5 },
228+
});
223229
wall.createFixture(new EdgeShape({ x: 0, y: -12.5 }, { x: 0, y: +11.5 }), wallFix);
224230
}
225231
{
226-
const wall = this.world.createBody({ x: 0, y: +12 });
232+
const wall = this.world.createBody({
233+
type: "static",
234+
position: { x: 0, y: +12 },
235+
});
227236
wall.createFixture(new EdgeShape({ x: -8, y: 0 }, { x: +8, y: 0 }), wallFix);
228237
}
229238
{
230-
const wall = this.world.createBody({ x: 9, y: 12 });
239+
const wall = this.world.createBody({
240+
type: "static",
241+
position: { x: 9, y: 12 },
242+
});
231243
wall.createFixture(new EdgeShape({ x: -1, y: 0 }, { x: 0, y: -1 }), wallFix);
232244
}
233245
{
234-
const wall = this.world.createBody({ x: -9, y: 12 });
246+
const wall = this.world.createBody({
247+
type: "static",
248+
position: { x: -9, y: 12 },
249+
});
235250
wall.createFixture(new EdgeShape({ x: 1, y: 0 }, { x: 0, y: -1 }), wallFix);
236251
}
237252
{
238-
const wall = this.world.createBody({ x: 0, y: -13 });
253+
const wall = this.world.createBody({
254+
type: "static",
255+
position: { x: 0, y: -13 },
256+
});
239257
wall.createFixture(new EdgeShape({ x: -9, y: 0 }, { x: +9, y: 0 }), wallFix);
240258

241259
wall.setUserData(new WallData());
@@ -273,7 +291,8 @@ class BreakoutPhysics {
273291
}
274292

275293
addBallPhysics(data: BallData) {
276-
const body = this.world.createDynamicBody({
294+
const body = this.world.createBody({
295+
type: "dynamic",
277296
bullet: true,
278297
angle: Math.random() * Math.PI * 2,
279298
});
@@ -301,7 +320,10 @@ class BreakoutPhysics {
301320
addBrickPhysics(data: BrickData) {
302321
const shape = data.subtype == "small" ? smallBrickShape : normalBrickShape;
303322
const pos = { x: (data.i - 3) * 2, y: 9 - data.j * 2 };
304-
const body = this.world.createBody(pos);
323+
const body = this.world.createBody({
324+
type: "static",
325+
position: pos,
326+
});
305327
body.createFixture(shape, brickFix);
306328

307329
body.setUserData(data);
@@ -321,7 +343,9 @@ class BreakoutPhysics {
321343
}
322344

323345
addDropPhysics(drop: DropData) {
324-
const body = this.world.createDynamicBody();
346+
const body = this.world.createBody({
347+
type: "dynamic",
348+
});
325349
if (drop.subtype == "+") {
326350
body.createFixture(new BoxShape(0.08, 0.32), dropFix);
327351
body.createFixture(new BoxShape(0.32, 0.08), dropFix);

example/Bridge.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import { World, Body, Edge, Box, Polygon, Circle, RevoluteJoint, Testbed } from "planck";
77

8-
const world = new World({ x: 0, y: -4 });
8+
const world = new World({
9+
gravity: { x: 0, y: -4 },
10+
});
911

1012
const testbed = Testbed.mount();
1113
testbed.start(world);
@@ -14,7 +16,9 @@ const COUNT = 30;
1416

1517
let middle: Body;
1618

17-
const ground = world.createBody();
19+
const ground = world.createBody({
20+
type: "static",
21+
});
1822
ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0);
1923

2024
const bridgeRect = new Box(0.5, 0.125);
@@ -26,7 +30,10 @@ const bridgeFD = {
2630

2731
let prevBody = ground;
2832
for (let i = 0; i < COUNT; ++i) {
29-
const body = world.createDynamicBody({ x: -14.5 + 1.0 * i, y: 5.0 });
33+
const body = world.createBody({
34+
type: "dynamic",
35+
position: { x: -14.5 + 1.0 * i, y: 5.0 },
36+
});
3037
body.createFixture(bridgeRect, bridgeFD);
3138

3239
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 };
4249
world.createJoint(new RevoluteJoint({}, prevBody, ground, anchor));
4350

4451
for (let i = 0; i < 2; ++i) {
45-
const body = world.createDynamicBody({ x: -8.0 + 8.0 * i, y: 12.0 });
52+
const body = world.createBody({
53+
type: "dynamic",
54+
position: { x: -8.0 + 8.0 * i, y: 12.0 },
55+
});
4656

4757
const vertices = [
4858
{ x: -0.5, y: 0.0 },
@@ -54,6 +64,9 @@ for (let i = 0; i < 2; ++i) {
5464

5565
const shape = new Circle(0.5);
5666
for (let i = 0; i < 3; ++i) {
57-
const body = world.createDynamicBody({ x: -6.0 + 6.0 * i, y: 10.0 });
67+
const body = world.createBody({
68+
type: "dynamic",
69+
position: { x: -6.0 + 6.0 * i, y: 10.0 },
70+
});
5871
body.createFixture(shape, 1.0);
5972
}

example/BulletTest.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55

66
import { World, Edge, Box, stats, Testbed } from "planck";
77

8-
const world = new World({ x: 0, y: -10 });
8+
const world = new World({
9+
gravity: { x: 0, y: -10 },
10+
});
911

1012
const testbed = Testbed.mount();
1113
testbed.start(world);
1214

13-
const ground = world.createBody();
15+
const ground = world.createBody({
16+
type: "static",
17+
});
1418
ground.createFixture(new Edge({ x: -10.0, y: 0.0 }, { x: 10.0, y: 0.0 }), 0.0);
1519
ground.createFixture(new Box(0.2, 1.0, { x: 0.5, y: 1.0 }, 0.0), 0.0);
1620

17-
const body = world.createDynamicBody({ x: 0.0, y: 4.0 });
21+
const body = world.createBody({
22+
type: "dynamic",
23+
position: { x: 0.0, y: 4.0 },
24+
});
1825
body.createFixture(new Box(2.0, 0.1), 1.0);
1926

2027
// x = Math.random(-1.0, 1.0);

0 commit comments

Comments
 (0)