When working with CollisionObject2D
, such as physics bodies or Area2D
, I often find myself wanting to check if a point (as Vector2) is touching a CollisionObject.
After looking around online, I couldn't find any clean/easy way to do it besides something like this:
- Get all
CollisionShape2D
of an object - iterate through each
CollisionShape2D
, find theShape
assigned to it. - match the shape found to circle or polygon (not sure if Geometry can handle capsules, partially obscured raycasts may need manual calculations later)
- use Geometry to check is a given point is inside the shape, accounting for relative positions, rotations etc.
- return true if any
CollisionShape2D
'sShape
' is colliding with the point
Alternatively:
- create a scene that is an
Area2D
with aCollisionShape2D
that is a tiny circle. - when collision with point needs to be checked, instance that scene, place it in the right location, give it the right collision layers
- update new Area2D to find intersecting bodies/shapes and see if it is colliding with the tested object
Both methods seem to have notable problems (second one is borderline unhinged, I know). I feel like there really should be a better way to do this, some function built into CollisionObject2D
perhaps, and with access to lower level code, checking collision with a single point must be easier than any methods similar to the ones I outlined.