I have a circle and a rectangle one of the two objects is moving the other is static. How can I prevent them from overlapping each other? For now there is only one thing that I don't know how to calculate - the depth between the two objects. Could you tell me how to do that? I am using monogame
public void CircleVsRectangle(CircleBody circle, RectangleBody rect)
{
float circleDistanceX = Math.Abs(circle.CenterX - rect.CenterX);
float circleDistanceY = Math.Abs(circle.CenterY - rect.CenterY);
Vector2 circleDistance = new Vector2(circleDistanceX, circleDistanceY);
if (circleDistance.X > (rect.Width / 2 + circle.Radius)) { return false; }
if (circleDistance.Y > (rect.Height / 2 + circle.Radius)) { return false; }
if (circleDistance.X <= (rect.Width / 2)) { return true; }
if (circleDistance.Y <= (rect.Height / 2)) { return true; }
float cornerDistance_sq = ((circleDistance.X - rect.Width / 2) * (circleDistance.X - rect.Width / 2)) + ((circleDistance.Y - rect.Height / 2) * (circleDistance.Y - rect.Height / 2));
if( (cornerDistance_sq <= (circle.Radius * circle.Radius)))
{
float dept = CalcDept() // ??????
if(circle.IsMoving) circle.position += dept;
else if(rectangle.IsMoving) rect.position += dept;
}
}