For completeness, I'll document the "reinventing the wheel" approach.
I recently wanted to do this too, but I wanted to do it statically (due to some code-structure decisions made before that I didn't want to break). So I didn't want to create a sensor body and World.Step
, as previously suggested.
Instead, I figured that a convex polygon intersects with a circle if
- some of the polygon's corners are in the circle, or
- a raycast from the center of the circle toward its center hits in the circle
Both can be determined statically, with some simple math and World.RayCast
.
For the corners: Get them by calling fixture.GetShape().GetVertices()
. Make sure to transform them from the Body
's local coordinate space into world coordinates using body.GetWorldPoint
. Then determine intersection by comparing their distances from the circle center to the circle's radius.
For the sides: RayCast
a vector from the center of the circle toward the center of the tank with the length of the radius. If you get anything, it's a hit.
Why you need to check both cases:
If you only check the corners, a side between them might still intersect.
If your raycast toward the middle misses, a corner might still intersect.
Depending on your objects' relative dimensions and the required precision, doing just one of these could be a good approximation.