Prevent objects from passing through walls / tunneling

I am using Matter.js for our physics. Our player is a matter image and our map is a static matter image with a complex collider made with PhysicsEditor. I was wondering if anyone knows of a quick fix since I wasn’t able to find much other than this https://github.com/liabru/matter-js/issues/5? Any help would be very much appreciated!

1 Like

A quick fix would be making your walls thicker.

A more complex fix would be adding sensors on both sides of the player, like described here:

1 Like

We have gone through the process to make our walls thicker and this has helped a lot. However we are using PhysicsEditor for our complex maze which seems to have odd axis connections which can cause clipping a lot easier (seen in debug mode).

I have slowed down the rotation speed of our maze, which has caused a lot less tunneling to occur. There are still some locations it happens but I believe it is related to my previous comment.

I do not know what type of game you are making, but in a platformer I made with matter, I use applyForce instead of setVelocity, which helped a lot. See the snippet below.

/**
 * We use setVelocity to jump
 * and applyForce to move right and left
 */

// Jump
if (y !== 0) this.Matter.Body.setVelocity(this.body, { x: this.body.velocity.x, y })

// Move
this.Matter.Body.applyForce(this.body, { x: 0, y: 0 }, { x, y: 0 })

// check max velocity
let maxVelocityX =
  this.body.velocity.x > this.maxVelocity.x ? 1 : this.body.velocity.x < -this.maxVelocity.x ? -1 : null
if (maxVelocityX)
  this.Matter.Body.setVelocity(this.body, { x: this.maxVelocity.x * maxVelocityX, y: this.body.velocity.y })

Update: So we slowed down the rotation of our map and made our walls thicker, both helped a lot. We also switched from using WEBGL to CANVAS and our game runs 10 times smoother which also seems to have helped.

1 Like