Jump to content

Questions about collisions with phaser/MatterJS


Razvan-Stafan
 Share

Recommended Posts

Hello,

I have some questions about the physics engine MatterJs.

I try to drag&drop an element but I have problem with the collissions between its and the world bounds or the other objects present in the world.

This is my my code:

preload(): void {
    this.load.image('background', '../../assets/grid.png');
    this.load.image('avatar', '../../assets/avatar.jpg');
    this.load.image('bubbles', '../../assets/avatar1.jpg');
  }

 create(): void {
    this.matter.world.setBounds(200, 200, 10000, 10000);
    this.tile = this.add.tileSprite(0, 0, 10000, 10000, 'background').setOrigin(0, 0);
    this.cameras.main.setBounds(0, 0, 10000, 10000);

    //The item that I want to drag
    let canDrag = this.matter.world.nextGroup(false);
    this.image = this.matter.add.image(400, 200, 'avatar', null, { shape: { type: 'circle', radius: 60 } }).setCollisionGroup(canDrag);
    this.image.setInteractive({ draggable: true });
    this.image.setFixedRotation();

    let noDrag = this.matter.world.nextGroup(true);

    //The item that is fix in the world.
    this.bubbles = this.matter.add.image(500, 500, 'bubbles', null, { shape: 'circle', ignorePointer: true }).setCollisionGroup(canDrag);
    this.bubbles.setFixedRotation();

    //I use this constraint to have a little bounce when the collision appeared
    this.matter.add.worldConstraint(this.bubbles, 1, 0.2, {
      pointA: { x: this.bubbles.x, y: this.bubbles.y }
    });


    this.matter.add.mouseSpring({ length: 1, stiffness: 0, collisionFilter: { group: canDrag } });
  }

My questions:

  1. When I drag the item close to the world bounds there are moments that the collision is ignored so my item disappear from the world.

  2. When my item collides with the elements present a force is applied and push my item in the other direction

Do you have some solutions for my issues ?

Link to comment
Share on other sites

Hi,

I have the exact same problem, the only solution I found is to set the velocity to 0 each update but it's a really dirty workaround.

As for collisions I had to create objects in each corners of the screen to avoid my main character passing through...

Anyone got better solutions?

Thanks,
 

update(): void {

    this.image.setVelocity(0);

}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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