Using physics to restrict player to area (squeezing & getting stuck)

I’m playing with the thought to create a game where the player is restricted som some kind of area. I’ve done this before, but then typically the player (space ship) just exploded touching the game area edge so it was faily easy to implement.

This time it’s a bit worse…

It involves an element of sqeezing as there will be small passages as shown below:

I’m not sure, however, if the physics engine is a capable tool for implementing this. I envision the player to have at least two physics elements:

  • One outer that when in contact with the “game area edge” will slow down any movement

  • One inner that will stop the player form going further

But is it possible to make such a thing using the physics engine without risking the player to get stuck? Getting stuck can/will be part of the game play, but I would like to have some control over it.

Hi @runewinse,

Yes, this should be relatively simple. I would suggest creating a multi-element physics body of 2 elements:

  1. The “inner” part as a standard dynamic body.

  2. The “outer” part (slightly larger) as a dynamic sensor body.

Then, in the collision handler, detect which element has collided with the game world (walls, etc.). Only if it’s the outer part , then apply linear damping to the object on the “began” phase. This will cause the object to have a certain amount of drag, hindering its movement and giving the impression that it’s being squeezed between the walls. Then, on the “ended” phase of that same outer sensor element (so, when it “exits” the walls), reset the linear damping to normal, such that the object can move about freely once again.

Hope this helps,

Brent

Thanks Brent!

Hi @runewinse,

Yes, this should be relatively simple. I would suggest creating a multi-element physics body of 2 elements:

  1. The “inner” part as a standard dynamic body.

  2. The “outer” part (slightly larger) as a dynamic sensor body.

Then, in the collision handler, detect which element has collided with the game world (walls, etc.). Only if it’s the outer part , then apply linear damping to the object on the “began” phase. This will cause the object to have a certain amount of drag, hindering its movement and giving the impression that it’s being squeezed between the walls. Then, on the “ended” phase of that same outer sensor element (so, when it “exits” the walls), reset the linear damping to normal, such that the object can move about freely once again.

Hope this helps,

Brent

Thanks Brent!