PhaserMatterCollisionPlugin - Collision with Bounds

Hello, how can I achieve this situation? I have Matter sprite and bounds set to world dimensions, but when object collides nothing happend. I have own function to create enemy( createNewMachine() ), and when I create new one I add this collision, but it’s not working :(:
eventData show nothing. Colud you help me?

function createNewMachine(x: number, y: number) {
   let machineID = Phaser.Math.Between(1, this.numberOfMachines);
   let newMachine = this.matter.add
     .sprite(x, y, `mach${machineID}-sprites`, `mach${machineID}_1.png`, {
       shape: this[`mach${machineID}Shapes`][`mach${machineID}_1`]
     })
     .play(`play-mach${machineID}`)
     .setIgnoreGravity(true);
  
   this.matterCollision.addOnCollideStart({
      objectA: this.matter.world.walls, //world bounds
      objectB: newMachine, // some object to detect collision with
      callback: eventData => {
       // run logic here
        console.log(eventData);
      },
     context: this // Context to apply to the callback function
    });
    this.machinesGroup.push(newMachine);
    (window as any).mach = this.machinesGroup;
 }

My answer/suggestions will not specific to a 3rd party plugin:

Use this.matter.world.setBounds(); to set world bounds with matter physics engine.

@fselcukcan i did it, but I have problem with detect collision. I need method that returns something when object collides with bounds.

Oh I see. I will look into that to help you. I can help quicker if you already have a running playground.

@fselcukcan I sent you a message.

I am preparing a jsfiddle for your use case.

here is the fiddle.

It does mainly that:

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
};

Yee, but problem is before.I console.log the event data, and there is no elements in pair array (is empty) :frowning:
image
image

@fselcukcan I’m debbuging the code and object are different:

pair.bodyA:
image

pair.bodyB:
image

And no one is similar to my machinObject

machineObject.body
image

Can it be because these are the physical bodies but not the game objects? And wht code are you debugging? Fiddle? your code?

Also the pairs console logs are looking empty. I have run into that before. I have an idea but not sure. I have experimented with it in console but I saw unreliable results. I was even writing it as the cause but I have decided to delete it. But I can say that much even the pairs look empty the moment you check them the it was not once, it left a trail like Array(1), which means it had the pair inside it once.

I figured it out with matterCollision plugin. I just added all of my eniemes to Phaser.group ( array ) and call function below in update() , but call it in update() is not good idea, because for example function this.this.pigCollisionFunction() which decrements player’s life instead do that once it do several times, and the result is very laggy game and this(screen below):
image

 const { left } = this.matter.world.walls; //left side of game bounds
    this.matterCollision.addOnCollideStart({
      objectA: this.machinesGroup.children.entries, // Array of enemies
      objectB: [left],
      callback: event => {
        event.gameObjectA.setAlpha(0);
        event.gameObjectA.destroy();
      }
    });

you had said you were trying to detect collision between a game object called like new machine and the wirld bounds, not a pig

@fselcukcan Yes, i Edited the post.

So you have solved it