Adding world boundaries for 2D rigidbodies

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Turnovus

I’m working on a top-down space shooter using rigidbodies like in Asteroids, but I intend to allow the player to travel through a very large area. However, I don’t want the play area to be infinite, so I plan to add invisible walls so that the player will be bounced away when they go farther than I want them to be able to go. What is the best way to implement this?

:bust_in_silhouette: Reply From: Magso

If you’re using add_force or apply_impulse you can use colliders and give the player a bouncy physics material. Another way would be to use an area and use body_exited when the player exits it, direct the player back in or towards the area using translate or look_at, or you could clamp the translation x and y values but that would stop the player instantly.

:bust_in_silhouette: Reply From: kidscancode

The easiest would be to stick 4 rectangular static bodies at the edges. Give them collision shapes but no sprites and they’ll be invisible.

Indeed, so far works alright for me, just make sure the walls are thick enough to avoid any possibility for objects to go through them.

Xrayez | 2019-06-23 19:16