Skip to content

Commit

Permalink
Unify createFixture usage in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shakiba committed Dec 24, 2024
1 parent f0127f4 commit 4b06bb3
Show file tree
Hide file tree
Showing 54 changed files with 977 additions and 303 deletions.
16 changes: 8 additions & 8 deletions example/8-Ball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ class BilliardPhysics {
for (let i = 0; i < ballsData.length; i++) {
const ball = this.world.createBody({
type: "dynamic",
bullet: true,
position: ballsData[i],
linearDamping: 1.5,
angularDamping: 1,
});
this.balls.push(ball);
ball.setBullet(true);
ball.setPosition(ballsData[i]);
const color = ballsData[i].color;
const style = color && STYLES[color];
const shape = new Circle(BALL_RADIUS);
ball.createFixture(shape, {
ball.createFixture({
shape: new Circle(BALL_RADIUS),
friction: 0.1,
restitution: 0.99,
density: 1,
Expand Down Expand Up @@ -166,8 +166,8 @@ class BilliardPhysics {
const body = this.world.createBody({
type: "static",
});
const shape = new Polygon(rails[i]);
const fixture = body.createFixture(shape, {
const fixture = body.createFixture({
shape: new Polygon(rails[i]),
friction: 0.1,
restitution: 0.9,
userData: RAIL,
Expand Down Expand Up @@ -205,8 +205,8 @@ class BilliardPhysics {
type: "static",
position: pockets[i],
});
const shape = new Circle(POCKET_RADIUS);
const fixture = body.createFixture(shape, {
const fixture = body.createFixture({
shape: new Circle(POCKET_RADIUS),
userData: POCKET,
});
}
Expand Down
14 changes: 10 additions & 4 deletions example/AddPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ for (let i = 0; i < 50; ++i) {
y: Math.random() * 2 - 1,
},
});
b.createFixture(circle, 0.01);
b.createFixture({
shape: circle,
density: 0.01,
});
}

const box = world.createBody({
type: "dynamic",
position: { x: -40.0, y: 0.0 },
bullet: true,
position: { x: -40.0, y: 0.0 },
linearVelocity: { x: 100.0, y: 0.0 },
});

box.createFixture(new Box(1.5, 1.5), 1.0);
box.setLinearVelocity({ x: 100.0, y: 0.0 });
box.createFixture({
shape: new Box(1.5, 1.5),
density: 1.0,
});
35 changes: 28 additions & 7 deletions example/ApplyForce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@ const wallFD = {
};

// Left vertical
ground.createFixture(new Edge({ x: -20.0, y: -20.0 }, { x: -20.0, y: 20.0 }), wallFD);
ground.createFixture({
shape: new Edge({ x: -20.0, y: -20.0 }, { x: -20.0, y: 20.0 }),
...wallFD,
});

// Right vertical
ground.createFixture(new Edge({ x: 20.0, y: -20.0 }, { x: 20.0, y: 20.0 }), wallFD);
ground.createFixture({
shape: new Edge({ x: 20.0, y: -20.0 }, { x: 20.0, y: 20.0 }),
...wallFD,
});

// Top horizontal
ground.createFixture(new Edge({ x: -20.0, y: 20.0 }, { x: 20.0, y: 20.0 }), wallFD);
ground.createFixture({
shape: new Edge({ x: -20.0, y: 20.0 }, { x: 20.0, y: 20.0 }),
...wallFD,
});

// Bottom horizontal
ground.createFixture(new Edge({ x: -20.0, y: -20.0 }, { x: 20.0, y: -20.0 }), wallFD);
ground.createFixture({
shape: new Edge({ x: -20.0, y: -20.0 }, { x: 20.0, y: -20.0 }),
...wallFD,
});

const xf1 = new Transform();
xf1.q.set(0.3524 * Math.PI);
Expand Down Expand Up @@ -66,8 +78,14 @@ const jet = world.createBody({
allowSleep: false,
});

jet.createFixture(poly1, 2.0);
jet.createFixture(poly2, 2.0);
jet.createFixture({
shape: poly1,
density: 2.0,
});
jet.createFixture({
shape: poly2,
density: 2.0,
});

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

box.createFixture(new Box(0.5, 0.5), boxFD);
box.createFixture({
shape: new Box(0.5, 0.5),
...boxFD,
});

const gravity = 10.0;
const I = box.getInertia();
Expand Down
22 changes: 10 additions & 12 deletions example/Asteroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,17 @@ class AsteroidPhysics {
},
});

this.ship.createFixture(
new Polygon([
this.ship.createFixture({
shape: new Polygon([
{ x: -0.15, y: -0.15 },
{ x: 0, y: -0.1 },
{ x: 0.15, y: -0.15 },
{ x: 0, y: 0.2 },
]),
{
density: 1000,
filterCategoryBits: SHIP_BITS,
filterMaskBits: ASTEROID_BITS,
},
);
density: 1000,
filterCategoryBits: SHIP_BITS,
filterMaskBits: ASTEROID_BITS,
});
}

steerLeft() {
Expand Down Expand Up @@ -124,7 +122,8 @@ class AsteroidPhysics {
},
});

body.createFixture(new Circle(0.05), {
body.createFixture({
shape: new Circle(0.05),
filterCategoryBits: BULLET_BITS,
filterMaskBits: ASTEROID_BITS,
});
Expand Down Expand Up @@ -163,8 +162,6 @@ class AsteroidPhysics {
path.push({ x: x, y: y });
}

const shape = new Polygon(path);

const asteroidBody = this.world.createBody({
// mass : 10,
type: "kinematic",
Expand All @@ -178,7 +175,8 @@ class AsteroidPhysics {
});
this.asteroids.push(asteroidBody);

asteroidBody.createFixture(shape, {
asteroidBody.createFixture({
shape: new Polygon(path),
filterCategoryBits: ASTEROID_BITS,
filterMaskBits: BULLET_BITS | SHIP_BITS,
});
Expand Down
15 changes: 12 additions & 3 deletions example/BasicSliderCrank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ const crank = world.createBody({
type: "dynamic",
position: { x: -8.0, y: 20.0 },
});
crank.createFixture(new Box(4.0, 1.0), 2.0);
crank.createFixture({
shape: new Box(4.0, 1.0),
density: 2.0,
});
world.createJoint(new RevoluteJoint({}, ground, crank, { x: -12.0, y: 20.0 }));

// Define connecting rod
const rod = world.createBody({
type: "dynamic",
position: { x: 4.0, y: 20.0 },
});
rod.createFixture(new Box(8.0, 1.0), 2.0);
rod.createFixture({
shape: new Box(8.0, 1.0),
density: 2.0,
});
world.createJoint(new RevoluteJoint({}, crank, rod, { x: -4.0, y: 20.0 }));

// Define piston
Expand All @@ -42,6 +48,9 @@ const piston = world.createBody({
position: { x: 12.0, y: 20.0 },
fixedRotation: true,
});
piston.createFixture(new Box(3.0, 3.0), 2.0);
piston.createFixture({
shape: new Box(3.0, 3.0),
density: 2.0,
});
world.createJoint(new RevoluteJoint({}, rod, piston, { x: 12.0, y: 20.0 }));
world.createJoint(new PrismaticJoint({}, ground, piston, { x: 12.0, y: 17.0 }, { x: 1.0, y: 0.0 }));
18 changes: 14 additions & 4 deletions example/BodyTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@ const SPEED = 3.0;
const ground = world.createBody({
type: "static",
});
ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }));
ground.createFixture({
shape: new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }),
});

// Define attachment
const attachment = world.createBody({
type: "dynamic",
position: { x: 0.0, y: 3.0 },
});
attachment.createFixture(new Box(0.5, 2.0), 2.0);
attachment.createFixture({
shape: new Box(0.5, 2.0),
density: 2.0,
});

// Define platform
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), {
platform.createFixture({
shape: new Box(0.5, 4.0, { x: 4.0, y: 0.0 }, 0.5 * Math.PI),
friction: 0.6,
density: 2.0,
});
Expand Down Expand Up @@ -71,7 +77,11 @@ 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 });
payload.createFixture({
shape: new Box(0.75, 0.75),
friction: 0.6,
density: 2.0,
});

testbed.keydown = function (code, char) {
if (char === "Z") {
Expand Down
10 changes: 7 additions & 3 deletions example/Boxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ testbed.start(world);

const bar = world.createBody({
type: "static",
angle: 0.2,
});
bar.createFixture({
shape: new Edge({ x: -20, y: 5 }, { x: 20, y: 5 }),
});
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++) {
Expand All @@ -24,6 +26,8 @@ for (let i = -2; i <= 2; i++) {
center: { x: 0, y: 0 },
I: 1,
});
box.createFixture(new Box(0.5, 0.5));
box.createFixture({
shape: 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 @@ -23,7 +23,10 @@ let broke = false;
const ground = world.createBody({
type: "static",
});
ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0);
ground.createFixture({
shape: new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }),
density: 0.0,
});

// Breakable dynamic body
const body1 = world.createBody({
Expand All @@ -33,10 +36,16 @@ const body1 = world.createBody({
});

const shape1 = new Box(0.5, 0.5, { x: -0.5, y: 0.0 }, 0.0);
const piece1 = body1.createFixture(shape1, 1.0);
const piece1 = body1.createFixture({
shape: shape1,
density: 1.0,
});

const shape2 = new Box(0.5, 0.5, { x: 0.5, y: 0.0 }, 0.0);
let piece2 = body1.createFixture(shape2, 1.0);
let piece2 = body1.createFixture({
shape: shape2,
density: 1.0,
});

world.on("post-solve", function (contact, impulse) {
if (broke) {
Expand Down Expand Up @@ -70,7 +79,10 @@ function breakIt() {
angle: body1.getAngle(),
});

piece2 = body2.createFixture(shape2, 1.0);
piece2 = body2.createFixture({
shape: shape2,
density: 1.0,
});

// Compute consistent velocities for new bodies based on
// cached velocity.
Expand Down
Loading

0 comments on commit 4b06bb3

Please sign in to comment.