Setup Matter.js
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'matter',
matter: {
gravity: { y: 1 },
debug: true
}
},
scene: GameScene
};
Creare Bodies
create() {
const rect = this.matter.add.rectangle(400, 200, 80, 80);
const circle = this.matter.add.circle(300, 100, 40);
const polygon = this.matter.add.polygon(500, 200, 6, 50);
const sprite = this.matter.add.sprite(400, 300, 'player');
}
Proprietà
const body = this.matter.add.rectangle(400, 200, 80, 80, {
density: 0.001,
friction: 0.1,
frictionAir: 0.01,
restitution: 0.8
});
const ground = this.matter.add.rectangle(400, 580, 800, 40, {
isStatic: true
});
const sensor = this.matter.add.rectangle(400, 300, 100, 100, {
isSensor: true
});
Compound Bodies
const bodyA = this.matter.bodies.rectangle(0, 0, 40, 40);
const bodyB = this.matter.bodies.circle(40, 0, 20);
const compoundBody = this.matter.body.create({
parts: [bodyA, bodyB]
});
this.matter.world.add(compoundBody);
Constraints
const bodyA = this.matter.add.rectangle(300, 200, 80, 80);
const bodyB = this.matter.add.rectangle(500, 200, 80, 80);
this.matter.add.constraint(bodyA, bodyB, 200, 0.5);
Collisioni
this.matter.world.on('collisionstart', (event) => {
event.pairs.forEach((pair) => {
const { bodyA, bodyB } = pair;
console.log('Collision!', bodyA, bodyB);
});
});
const player = this.matter.add.sprite(400, 300, 'player', null, {
collisionFilter: {
category: 0x0001,
mask: 0x0002
}
});
Best Practices
- Usa Matter.js per fisica realistica
- Arcade Physics per giochi arcade semplici
- Compound bodies per forme complesse
- Collision filtering per performance