Phaser Playground
matter physcis collison between a game object body and the matter world bounds, walls
by fselcukcan
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Invasion</title>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
<script src="gameScene.js"></script>
<script src="game.js"></script>
</head>
<body>
</body>
</html>
JavaScript
const GameScene = new Phaser.Scene();
GameScene.init = function() {};
GameScene.preload = function() {};
GameScene.create = function() {
this.matter.world.setBounds();
const options = { shape: "circle", friction: 0.005, restitution: 0.5 };
const newMachine = this.matter.add.image(50, 0, "newmachine", null, options);
// Collision Detector definitions
this.matter.world.on(
"collisionstart",
function(event, bodyA, bodyB) {
event.pairs.forEach(pair => {
if (
(pair.bodyA === newMachine.body ||
pair.bodyB === newMachine.body) &&
([pair.bodyA, pair.bodyB].some(body => Object.values(this.matter.world.walls).includes(body)))
) {
console.log(pair.bodyA, " collided ", pair.bodyB);
}
});
},
this
);
// END Collision Detector definitions
};
// UPDATE
GameScene.update = function() {};
const config = {
type: Phaser.AUTO,
width: 640,
height: 480,
physics: {
default: 'matter',
matter: {
debug: true,
}
},
scene: [GameScene]
};
const game = new Phaser.Game(config);