# Prevent objects from passing through walls / tunneling

**URL:** <https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883>\
**Category:** Phaser 3\
**Created:** [March 28, 2019, 6:36pm UTC](https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883 "2019-03-28T18:36:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cflowers](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cflowers](https://phaser.discourse.group/u/cflowers)\
**Post date:** [March 28, 2019, 6:36pm UTC](https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883/1 "2019-03-28T18:36:25Z")

</div>

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](https://github.com/liabru/matter-js/issues/5)? Any help would be very much appreciated!

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yannick/32/732_2.png) [@yannick](https://phaser.discourse.group/u/yannick)\
**Post date:** [March 28, 2019, 6:50pm UTC](https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883/2 "2019-03-28T18:50:09Z")

</div>

A quick fix would be making your walls thicker.

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yannick/32/732_2.png) [@yannick](https://phaser.discourse.group/u/yannick)\
**Post date:** [March 28, 2019, 6:52pm UTC](https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883/3 "2019-03-28T18:52:09Z")

</div>

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

> **[Modular Game Worlds in Phaser 3 (Tilemaps #5) - Matter Physics Platformer](https://itnext.io/modular-game-worlds-in-phaser-3-tilemaps-5-matter-physics-platformer-d14d1f614557?gi=9edff79e7513)**
>
> Making a puzzle-y Matter.js platformer

---

<div class="post-metadata">

**Author:** ![cflowers](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cflowers](https://phaser.discourse.group/u/cflowers)\
**Post date:** [April 1, 2019, 2:29am UTC](https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883/4 "2019-04-01T02:29:59Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![cflowers](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cflowers](https://phaser.discourse.group/u/cflowers)\
**Post date:** [April 1, 2019, 2:31am UTC](https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883/5 "2019-04-01T02:31:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yannick/32/732_2.png) [@yannick](https://phaser.discourse.group/u/yannick)\
**Post date:** [April 1, 2019, 5:34am UTC](https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883/6 "2019-04-01T05:34:40Z")

</div>

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.

```javascript
/**
 * 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 })

```

---

<div class="post-metadata">

**Author:** ![cflowers](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cflowers](https://phaser.discourse.group/u/cflowers)\
**Post date:** [April 18, 2019, 4:26am UTC](https://phaser.discourse.group/t/prevent-objects-from-passing-through-walls-tunneling/1883/7 "2019-04-18T04:26:45Z")

</div>

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.
