# Collision detection not working as expected

**URL:** <https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077>\
**Category:** Questions\
**Created:** [December 15, 2021, 8:12am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077 "2021-12-15T08:12:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Swastik\_K](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/swastik_k/32/20550_2.png) [@Swastik\_K](https://forum.babylonjs.com/u/Swastik_K)\
**Post date:** [December 15, 2021, 8:12am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/1 "2021-12-15T08:12:55Z")

</div>

I’am creating a game where player can change the game object using mouse wheel and he/she have to pass through the incoming obstacle without colliding by using keys WASD

The problem I’m facing here is collision between obstacle and player is not properly detected when obstacle is moving as shown in below videos.

**Playground link** : [Playground](https://playground.babylonjs.com/#TPNZWN)

**Video-1:**

[https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/e/5ea87325d19e2a8ddd4e4e66648b39f5cb845561.mp4](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/e/5ea87325d19e2a8ddd4e4e66648b39f5cb845561.mp4)

**Video-2:**

[https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/1/312b7e40de8353c2f49398252a7616f2d7881243.mp4](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/1/312b7e40de8353c2f49398252a7616f2d7881243.mp4)

**Video-3** :

[https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/d/1dbb481bb3650075116c9b9349962e65a885abc7.mp4](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/d/1dbb481bb3650075116c9b9349962e65a885abc7.mp4)

**3 cases:**

- Collision between player and object is properly detected when player collides with obstacle at the edges as shown in video-1
- Collision between player and object is not detected when player is colliding with obstacle completely as shown in video-2
- Collision is working as expected in both cases(colliding with edges and colliding completely with the obstacle) when I stop moving obstacle by commenting line number 110 `obstacle.position.z += 0.05;` and start moving my player towards the obstacle as shown in video-3

What may be the reason for this behaviour and how to solve this?

**NOTE: Click on game screen once to move game object**

It would be really helpful if someone helps me in solving this…

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [December 15, 2021, 10:05am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/2 "2021-12-15T10:05:30Z")

</div>

@Cedric the king of Physix might be able to help

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [December 15, 2021, 10:13am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/3 "2021-12-15T10:13:25Z")

</div>

I would not change position of `player` but change the linear velocity instead. I think changing position each frame, when using a physics engine, is not a good solution in this case.

---

<div class="post-metadata">

**Author:** ![Swastik\_K](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/swastik_k/32/20550_2.png) [@Swastik\_K](https://forum.babylonjs.com/u/Swastik_K)\
**Post date:** [December 15, 2021, 10:16am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/4 "2021-12-15T10:16:39Z")

</div>

@Cedric Thank you for the response,

I’ll check the collision behaviour by adding linear velocity

---

<div class="post-metadata">

**Author:** ![Swastik\_K](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/swastik_k/32/20550_2.png) [@Swastik\_K](https://forum.babylonjs.com/u/Swastik_K)\
**Post date:** [December 15, 2021, 11:02am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/5 "2021-12-15T11:02:08Z")

</div>

@Cedric In order to add linear velocity to my obstacle I need to change the mass property of my obstacle which was zero earlier. Now when I set my obstacles’s `mass=1` & `restitution=0` it is bouncing and falling off the ground. Even with ground `restitution=0` same thing is happening.

How can I make Obstacle to stand and move without falling ?

Playground: [https://playground.babylonjs.com/#TPNZWN#1](https://playground.babylonjs.com/#TPNZWN#1)

Thank you

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [December 15, 2021, 11:10am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/6 "2021-12-15T11:10:17Z")

</div>

set its mass to 0 to make it static but you won’t be able to set a linear velocity. Or set its linear/angular velocity to 0:

[collision | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#TPNZWN#2)

---

<div class="post-metadata">

**Author:** ![Swastik\_K](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/swastik_k/32/20550_2.png) [@Swastik\_K](https://forum.babylonjs.com/u/Swastik_K)\
**Post date:** [December 15, 2021, 11:35am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/7 "2021-12-15T11:35:17Z")

</div>

@Cedric This doesn’t solve my root problem and also creates one more problem, i.e, now my player(sphere) cannot pass through the hole in obstacle as shown in the below video

[https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/6/76bf1788447bcf8a48a325e861a516d9d13ec671.mp4](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/6/76bf1788447bcf8a48a325e861a516d9d13ec671.mp4)

Thank you

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [December 15, 2021, 11:38am UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/8 "2021-12-15T11:38:39Z")

</div>

You can use a compound with multiple boxes making it dynamic.  
or leave it as a static mesh impostor (with mass = 0)  
I don’t think it’s a good idea to move the static world around the player: I’d suggest to move the camera and the sphere and keep everything else static.

---

<div class="post-metadata">

**Author:** ![Swastik\_K](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/swastik_k/32/20550_2.png) [@Swastik\_K](https://forum.babylonjs.com/u/Swastik_K)\
**Post date:** [December 15, 2021, 12:10pm UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/9 "2021-12-15T12:10:41Z")

</div>

@Cedric Thank you for the suggestion. I will move my sphere and camera and keep all obstacles static.

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [December 15, 2021, 12:13pm UTC](https://forum.babylonjs.com/t/collision-detection-not-working-as-expected/26077/10 "2021-12-15T12:13:02Z")

</div>

yes, that’s how I would do it.
