Jump to content

Matter Physics doesn't detect multiple collisions


EvilPixi
 Share

Recommended Posts

Hi, I was using matter physics and tried to check collisions from a lot of objects and then I got some issues.

The problem:

I have a lot of objects (called them "luz" in the code) and I want to release them on air and making them fall. Then when they touch the floor I want to trigger something:
- if it is a even object: I want to reduce its Alpha to 30%.
- if is an odd object: I reduce their scale by half.

Currently I'm handling this situation in my real project by checking 

update() {
  if (luz[i].y > floor.Y) { /* do something */ }
/*...*/ }

but I'm looking for a better and more flexible and robust solution.

So here is my code:

class GameScene extends Phaser.Scene {
    constructor ()
    {
        super('GameScene');
    }
    preload() {
        this.load.image('luz', 'luz.png');
    }
    create() {
        // create elements on air
        for (var i=1; i<=7; i++) {
            var luz = this.matter.add.image(i*100 + 10, 50, 'luz')
            luz.body.label = i%2 == 0 ? "even luz" : "odd luz"
        }

        // add the floor
        this.matter.add.rectangle(400, 550, 800, 50, {isStatic: true}).label = "floor"

        // check collisions
        this.matter.world.on('collisionstart', function (event, object1, object2) {
            // this only prints the first object
            console.log(object1.label + " collides with " + object2.label)

            switch (object1.label) {
                case "odd luz": object1.gameObject.alpha = 0.3; break;
                case "even luz": object1.gameObject.setScale(0.5); break;
            }
        })
    }
}

Live example here: https://codepen.io/akuma119/pen/zYvKzgW
Video showing the problem: https://i.imgur.com/jU7UiaW.mp4

My questions are:
- Am I handling collisions correctly? is this the proper way?
- Should I implement my own collision checking system?
- Is checking by label a bad design/practice?

Thank you for your time and help!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...