Skip to content

Commit 4b06bb3

Browse files
committed
Unify createFixture usage in examples
1 parent f0127f4 commit 4b06bb3

Some content is hidden

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

54 files changed

+977
-303
lines changed

example/8-Ball.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ class BilliardPhysics {
9292
for (let i = 0; i < ballsData.length; i++) {
9393
const ball = this.world.createBody({
9494
type: "dynamic",
95+
bullet: true,
96+
position: ballsData[i],
9597
linearDamping: 1.5,
9698
angularDamping: 1,
9799
});
98100
this.balls.push(ball);
99-
ball.setBullet(true);
100-
ball.setPosition(ballsData[i]);
101101
const color = ballsData[i].color;
102102
const style = color && STYLES[color];
103-
const shape = new Circle(BALL_RADIUS);
104-
ball.createFixture(shape, {
103+
ball.createFixture({
104+
shape: new Circle(BALL_RADIUS),
105105
friction: 0.1,
106106
restitution: 0.99,
107107
density: 1,
@@ -166,8 +166,8 @@ class BilliardPhysics {
166166
const body = this.world.createBody({
167167
type: "static",
168168
});
169-
const shape = new Polygon(rails[i]);
170-
const fixture = body.createFixture(shape, {
169+
const fixture = body.createFixture({
170+
shape: new Polygon(rails[i]),
171171
friction: 0.1,
172172
restitution: 0.9,
173173
userData: RAIL,
@@ -205,8 +205,8 @@ class BilliardPhysics {
205205
type: "static",
206206
position: pockets[i],
207207
});
208-
const shape = new Circle(POCKET_RADIUS);
209-
const fixture = body.createFixture(shape, {
208+
const fixture = body.createFixture({
209+
shape: new Circle(POCKET_RADIUS),
210210
userData: POCKET,
211211
});
212212
}

example/AddPair.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@ for (let i = 0; i < 50; ++i) {
2323
y: Math.random() * 2 - 1,
2424
},
2525
});
26-
b.createFixture(circle, 0.01);
26+
b.createFixture({
27+
shape: circle,
28+
density: 0.01,
29+
});
2730
}
2831

2932
const box = world.createBody({
3033
type: "dynamic",
31-
position: { x: -40.0, y: 0.0 },
3234
bullet: true,
35+
position: { x: -40.0, y: 0.0 },
36+
linearVelocity: { x: 100.0, y: 0.0 },
3337
});
3438

35-
box.createFixture(new Box(1.5, 1.5), 1.0);
36-
box.setLinearVelocity({ x: 100.0, y: 0.0 });
39+
box.createFixture({
40+
shape: new Box(1.5, 1.5),
41+
density: 1.0,
42+
});

example/ApplyForce.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,28 @@ const wallFD = {
2222
};
2323

2424
// Left vertical
25-
ground.createFixture(new Edge({ x: -20.0, y: -20.0 }, { x: -20.0, y: 20.0 }), wallFD);
25+
ground.createFixture({
26+
shape: new Edge({ x: -20.0, y: -20.0 }, { x: -20.0, y: 20.0 }),
27+
...wallFD,
28+
});
2629

2730
// Right vertical
28-
ground.createFixture(new Edge({ x: 20.0, y: -20.0 }, { x: 20.0, y: 20.0 }), wallFD);
31+
ground.createFixture({
32+
shape: new Edge({ x: 20.0, y: -20.0 }, { x: 20.0, y: 20.0 }),
33+
...wallFD,
34+
});
2935

3036
// Top horizontal
31-
ground.createFixture(new Edge({ x: -20.0, y: 20.0 }, { x: 20.0, y: 20.0 }), wallFD);
37+
ground.createFixture({
38+
shape: new Edge({ x: -20.0, y: 20.0 }, { x: 20.0, y: 20.0 }),
39+
...wallFD,
40+
});
3241

3342
// Bottom horizontal
34-
ground.createFixture(new Edge({ x: -20.0, y: -20.0 }, { x: 20.0, y: -20.0 }), wallFD);
43+
ground.createFixture({
44+
shape: new Edge({ x: -20.0, y: -20.0 }, { x: 20.0, y: -20.0 }),
45+
...wallFD,
46+
});
3547

3648
const xf1 = new Transform();
3749
xf1.q.set(0.3524 * Math.PI);
@@ -66,8 +78,14 @@ const jet = world.createBody({
6678
allowSleep: false,
6779
});
6880

69-
jet.createFixture(poly1, 2.0);
70-
jet.createFixture(poly2, 2.0);
81+
jet.createFixture({
82+
shape: poly1,
83+
density: 2.0,
84+
});
85+
jet.createFixture({
86+
shape: poly2,
87+
density: 2.0,
88+
});
7189

7290
const boxFD = {
7391
density: 1.0,
@@ -80,7 +98,10 @@ for (let i = 0; i < 10; ++i) {
8098
position: { x: 0.0, y: 5.0 + 1.54 * i },
8199
});
82100

83-
box.createFixture(new Box(0.5, 0.5), boxFD);
101+
box.createFixture({
102+
shape: new Box(0.5, 0.5),
103+
...boxFD,
104+
});
84105

85106
const gravity = 10.0;
86107
const I = box.getInertia();

example/Asteroid.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,17 @@ class AsteroidPhysics {
7575
},
7676
});
7777

78-
this.ship.createFixture(
79-
new Polygon([
78+
this.ship.createFixture({
79+
shape: new Polygon([
8080
{ x: -0.15, y: -0.15 },
8181
{ x: 0, y: -0.1 },
8282
{ x: 0.15, y: -0.15 },
8383
{ x: 0, y: 0.2 },
8484
]),
85-
{
86-
density: 1000,
87-
filterCategoryBits: SHIP_BITS,
88-
filterMaskBits: ASTEROID_BITS,
89-
},
90-
);
85+
density: 1000,
86+
filterCategoryBits: SHIP_BITS,
87+
filterMaskBits: ASTEROID_BITS,
88+
});
9189
}
9290

9391
steerLeft() {
@@ -124,7 +122,8 @@ class AsteroidPhysics {
124122
},
125123
});
126124

127-
body.createFixture(new Circle(0.05), {
125+
body.createFixture({
126+
shape: new Circle(0.05),
128127
filterCategoryBits: BULLET_BITS,
129128
filterMaskBits: ASTEROID_BITS,
130129
});
@@ -163,8 +162,6 @@ class AsteroidPhysics {
163162
path.push({ x: x, y: y });
164163
}
165164

166-
const shape = new Polygon(path);
167-
168165
const asteroidBody = this.world.createBody({
169166
// mass : 10,
170167
type: "kinematic",
@@ -178,7 +175,8 @@ class AsteroidPhysics {
178175
});
179176
this.asteroids.push(asteroidBody);
180177

181-
asteroidBody.createFixture(shape, {
178+
asteroidBody.createFixture({
179+
shape: new Polygon(path),
182180
filterCategoryBits: ASTEROID_BITS,
183181
filterMaskBits: BULLET_BITS | SHIP_BITS,
184182
});

example/BasicSliderCrank.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,21 @@ const crank = world.createBody({
2525
type: "dynamic",
2626
position: { x: -8.0, y: 20.0 },
2727
});
28-
crank.createFixture(new Box(4.0, 1.0), 2.0);
28+
crank.createFixture({
29+
shape: new Box(4.0, 1.0),
30+
density: 2.0,
31+
});
2932
world.createJoint(new RevoluteJoint({}, ground, crank, { x: -12.0, y: 20.0 }));
3033

3134
// Define connecting rod
3235
const rod = world.createBody({
3336
type: "dynamic",
3437
position: { x: 4.0, y: 20.0 },
3538
});
36-
rod.createFixture(new Box(8.0, 1.0), 2.0);
39+
rod.createFixture({
40+
shape: new Box(8.0, 1.0),
41+
density: 2.0,
42+
});
3743
world.createJoint(new RevoluteJoint({}, crank, rod, { x: -4.0, y: 20.0 }));
3844

3945
// Define piston
@@ -42,6 +48,9 @@ const piston = world.createBody({
4248
position: { x: 12.0, y: 20.0 },
4349
fixedRotation: true,
4450
});
45-
piston.createFixture(new Box(3.0, 3.0), 2.0);
51+
piston.createFixture({
52+
shape: new Box(3.0, 3.0),
53+
density: 2.0,
54+
});
4655
world.createJoint(new RevoluteJoint({}, rod, piston, { x: 12.0, y: 20.0 }));
4756
world.createJoint(new PrismaticJoint({}, ground, piston, { x: 12.0, y: 17.0 }, { x: 1.0, y: 0.0 }));

example/BodyTypes.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,28 @@ const SPEED = 3.0;
1818
const ground = world.createBody({
1919
type: "static",
2020
});
21-
ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }));
21+
ground.createFixture({
22+
shape: new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }),
23+
});
2224

2325
// Define attachment
2426
const attachment = world.createBody({
2527
type: "dynamic",
2628
position: { x: 0.0, y: 3.0 },
2729
});
28-
attachment.createFixture(new Box(0.5, 2.0), 2.0);
30+
attachment.createFixture({
31+
shape: new Box(0.5, 2.0),
32+
density: 2.0,
33+
});
2934

3035
// Define platform
3136
const platform = world.createBody({
3237
type: "dynamic",
3338
position: { x: -4.0, y: 5.0 },
3439
});
3540

36-
platform.createFixture(new Box(0.5, 4.0, { x: 4.0, y: 0.0 }, 0.5 * Math.PI), {
41+
platform.createFixture({
42+
shape: new Box(0.5, 4.0, { x: 4.0, y: 0.0 }, 0.5 * Math.PI),
3743
friction: 0.6,
3844
density: 2.0,
3945
});
@@ -71,7 +77,11 @@ const payload = world.createBody({
7177
type: "dynamic",
7278
position: { x: 0.0, y: 8.0 },
7379
});
74-
payload.createFixture(new Box(0.75, 0.75), { friction: 0.6, density: 2.0 });
80+
payload.createFixture({
81+
shape: new Box(0.75, 0.75),
82+
friction: 0.6,
83+
density: 2.0,
84+
});
7585

7686
testbed.keydown = function (code, char) {
7787
if (char === "Z") {

example/Boxes.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ testbed.start(world);
99

1010
const bar = world.createBody({
1111
type: "static",
12+
angle: 0.2,
13+
});
14+
bar.createFixture({
15+
shape: new Edge({ x: -20, y: 5 }, { x: 20, y: 5 }),
1216
});
13-
bar.createFixture(new Edge({ x: -20, y: 5 }, { x: 20, y: 5 }));
14-
bar.setAngle(0.2);
1517

1618
for (let i = -2; i <= 2; i++) {
1719
for (let j = -2; j <= 2; j++) {
@@ -24,6 +26,8 @@ for (let i = -2; i <= 2; i++) {
2426
center: { x: 0, y: 0 },
2527
I: 1,
2628
});
27-
box.createFixture(new Box(0.5, 0.5));
29+
box.createFixture({
30+
shape: new Box(0.5, 0.5),
31+
});
2832
}
2933
}

example/Breakable.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ let broke = false;
2323
const ground = world.createBody({
2424
type: "static",
2525
});
26-
ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0);
26+
ground.createFixture({
27+
shape: new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }),
28+
density: 0.0,
29+
});
2730

2831
// Breakable dynamic body
2932
const body1 = world.createBody({
@@ -33,10 +36,16 @@ const body1 = world.createBody({
3336
});
3437

3538
const shape1 = new Box(0.5, 0.5, { x: -0.5, y: 0.0 }, 0.0);
36-
const piece1 = body1.createFixture(shape1, 1.0);
39+
const piece1 = body1.createFixture({
40+
shape: shape1,
41+
density: 1.0,
42+
});
3743

3844
const shape2 = new Box(0.5, 0.5, { x: 0.5, y: 0.0 }, 0.0);
39-
let piece2 = body1.createFixture(shape2, 1.0);
45+
let piece2 = body1.createFixture({
46+
shape: shape2,
47+
density: 1.0,
48+
});
4049

4150
world.on("post-solve", function (contact, impulse) {
4251
if (broke) {
@@ -70,7 +79,10 @@ function breakIt() {
7079
angle: body1.getAngle(),
7180
});
7281

73-
piece2 = body2.createFixture(shape2, 1.0);
82+
piece2 = body2.createFixture({
83+
shape: shape2,
84+
density: 1.0,
85+
});
7486

7587
// Compute consistent velocities for new bodies based on
7688
// cached velocity.

0 commit comments

Comments
 (0)