I figured one approach would be to divide the trapezoid in three parts, two triangles and a rectangle in the middle.
Then for the circle-rectangle case I would check collision like this:
function testCircleRectCollision(circle, rect) {
closestX = clamp(circle.x, rect.left, rect.right)
closestY = clamp(circle.y, rect.top, rect.bottom)
distanceX = circle.x - closestX
distanceY = circle.y - closestY
return pow(distanceX, 2) + pow(distanceY, 2) < pow(circle.radius, 2)
}
For the circle-trangle case, I could only think of checking if the vertices of the triangle are inside the circle like this:
function vertexInCircle(circle, v) {
return pow(v.x - circle.x, 2) + pow(v.y - circle.y, 2) < pow(circle.radius, 2)
}
But I still need to check two cases: 1) if the circle is completely inside the rectangle and 2) if the circle is intersecting with any of the triangle's edges. How can I do this?
Or is there a better way to check for circle-trapezoid collision?