# Collision detection not working as expected

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.

Video-1:

Video-2:

Video-3:

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…

@Cedric the king of Physix might be able to help

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.

@Cedric Thank you for the response,

I’ll check the collision behaviour by adding linear velocity

1 Like

@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

Thank you

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)

@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

Thank you

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.

3 Likes

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

yes, that’s how I would do it.