How to keep CharacterBody2Ds within a circle?

Godot Version

4.2.1

Question

I have a number of CharacterBody2Ds that I would like to keep within a circular arena.
I could measure the distance of each body from the center of the circle and modify the body’s position if it gets outside, but that way the bodies would have to know of the circle or vice versa, and it is not so easy to account for irregular (non-circular) body shapes. I would prefer a solution where that is not the case, such as a physics based one.
Trying to use the body_exited signal of an Area2D as boundary also does not work well, because it only triggers when the body has completely left the Area2D. It would need it to react to the first pixel that leaves to be useful in this case.
Any ideas?

Using areas you could make an external area around your circle that would detect players leaving (I think this is a bad idea)

Have you tried surrounding your arena with a collider?

Why can’t you just use a static body with a polygon 2d collision shape approximated as a circle?

2 Likes

Thanks for the hint. I did not know about CollisionPolygon2D, as I was only looking at the CollisionObject2D group. Don’t know why CollisionPolygon2D is not in there, since it can only live as the child of a CollisionObject2D object.

1 Like