Matter JS Bridge
Create a flexible suspension bridge using a stack of physics bodies linked by a chain and anchored with world constraints.
800 × 600
class Example extends Phaser.Scene
{
create ()
{
this.matter.world.setBounds();
this.matter.add.mouseSpring();
const group = this.matter.world.nextGroup(true);
const bridge = this.matter.add.stack(160, 290, 15, 1, 0, 0, (x, y) => Phaser.Physics.Matter.Matter.Bodies.rectangle(x - 20, y, 53, 20, {
collisionFilter: { group: group },
chamfer: 5,
density: 0.005,
frictionAir: 0.05
}));
this.matter.add.chain(bridge, 0.3, 0, -0.3, 0, {
stiffness: 1,
length: 0,
render: {
visible: false
}
});
const stack = this.matter.add.stack(250, 50, 6, 3, 0, 0, (x, y) => Phaser.Physics.Matter.Matter.Bodies.rectangle(x, y, 50, 50, Phaser.Math.Between(20, 40)));
this.matter.add.rectangle(30, 490, 220, 380, {
isStatic: true,
chamfer: { radius: 20 }
}),
this.matter.add.rectangle(770, 490, 220, 380, {
isStatic: true,
chamfer: { radius: 20 }
}),
this.matter.add.worldConstraint(bridge.bodies[0], 2, 0.9, {
pointA: { x: 140, y: 300 },
pointB: { x: -25, y: 0 }
});
this.matter.add.worldConstraint(bridge.bodies[bridge.bodies.length - 1], 2, 0.9, {
pointA: { x: 660, y: 300 },
pointB: { x: 25, y: 0 }
});
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#000000',
parent: 'game-container',
physics: {
default: 'matter',
matter: {
gravity: {
y: 0.8
},
debug: true,
debugBodyColor: 0xffffff
}
},
scene: Example
};
const game = new Phaser.Game(config);