Skip to main content
Fix pseudo code algorithm, signs and distance calculation were incorrect
Source Link

To detect collisions between a circle and an AABB, you should find the closest point from the AABB to the circle. You can run the below code snippet and try to move the purple point which represents the AABB center to watch the position of the closest point.

<iframe src="https://www.desmos.com/calculator/u32th8uhli?embed" width="500px" height="500px" style="border: 1px solid #ccc" frameborder=0></iframe>
The math behind it is to clamp the distance from AABB to circle on each axis separately. The value will be clamped on each axis if the distance is greater than the extent(half-width or height) or less than the negative extent. Like the below figure shows: enter image description here (Forgive my clumsy finger).
The pseudo-code is:

func CheckCollision() {
    vector3 distance = AABBCircle.Center - CircleAABB.Center;
    //Clamp(value,min,max)
    vector3 clampDst = Clamp(distance, -AABB.Bounds, AABB.Bounds);
    vector3 closestPoint = AABB.Center + clampDst;
    return Distance(closestPoint -, Circle.Center).SqrMagnitude ><= Circle.Radius;
}

To detect collisions between a circle and an AABB, you should find the closest point from the AABB to the circle. You can run the below code snippet and try to move the purple point which represents the AABB center to watch the position of the closest point.

<iframe src="https://www.desmos.com/calculator/u32th8uhli?embed" width="500px" height="500px" style="border: 1px solid #ccc" frameborder=0></iframe>
The math behind it is to clamp the distance from AABB to circle on each axis separately. The value will be clamped on each axis if the distance is greater than the extent(half-width or height) or less than the negative extent. Like the below figure shows: enter image description here (Forgive my clumsy finger).
The pseudo-code is:

func CheckCollision(){
    vector3 distance = AABB.Center - Circle.Center;
    //Clamp(value,min,max)
    vector3 clampDst = Clamp(distance, -AABB.Bounds, AABB.Bounds);
    vector3 closestPoint = AABB.Center + clampDst;
    return (closestPoint - Circle.Center).SqrMagnitude > Circle.Radius;
}

To detect collisions between a circle and an AABB, you should find the closest point from the AABB to the circle. You can run the below code snippet and try to move the purple point which represents the AABB center to watch the position of the closest point.

<iframe src="https://www.desmos.com/calculator/u32th8uhli?embed" width="500px" height="500px" style="border: 1px solid #ccc" frameborder=0></iframe>
The math behind it is to clamp the distance from AABB to circle on each axis separately. The value will be clamped on each axis if the distance is greater than the extent(half-width or height) or less than the negative extent. Like the below figure shows: enter image description here (Forgive my clumsy finger).
The pseudo-code is:

func CheckCollision() {
    vector3 distance = Circle.Center - AABB.Center;
    vector3 clampDst = Clamp(distance, -AABB.Bounds, AABB.Bounds);
    vector3 closestPoint = AABB.Center + clampDst;
    return Distance(closestPoint, Circle.Center) <= Circle.Radius;
}
added 1 character in body
Source Link
Ghoster
  • 61
  • 1
  • 3

To detect collisions between a circle and an AABB, you should find the closest point from the AABB to the circle. You can run the below code snippet and try to move the purple point which represents the AABB center to watch the position of the closest point.

<iframe src="https://www.desmos.com/calculator/u32th8uhli?embed" width="500px" height="500px" style="border: 1px solid #ccc" frameborder=0></iframe>
ToThe math behind it is to clamp the distance from AABB to circle on each axis separately. The value will be clamped on each axis if the distance is greater than the extent(half-width or height) or less than the negative extent. Like the below figure shows: enter image description here (Forgive my clumsy finger).
The pseudo-code is:

func CheckCollision(){
    vector3 distance = AABB.Center - Circle.Center;
    //Clamp(value,min,max)
    vector3 clampDst = Clamp(distance, -AABB.Bounds, AABB.Bounds);
    vector3 closestPoint = AABB.Center + clampDst;
    return (closestPoint - Circle.Center).SqrMagnitude > Circle.Radius;
}

To detect collisions between a circle and an AABB, you should find the closest point from the AABB to the circle. You can run the below code snippet and try to move the purple point which represents the AABB center to watch the position of the closest point.

<iframe src="https://www.desmos.com/calculator/u32th8uhli?embed" width="500px" height="500px" style="border: 1px solid #ccc" frameborder=0></iframe>
To math behind it is to clamp the distance from AABB to circle on each axis separately. The value will be clamped on each axis if the distance is greater than the extent(half-width or height) or less than the negative extent. Like the below figure shows: enter image description here (Forgive my clumsy finger).
The pseudo-code is:

func CheckCollision(){
    vector3 distance = AABB.Center - Circle.Center;
    //Clamp(value,min,max)
    vector3 clampDst = Clamp(distance, -AABB.Bounds, AABB.Bounds);
    vector3 closestPoint = AABB.Center + clampDst;
    return (closestPoint - Circle.Center).SqrMagnitude > Circle.Radius;
}

To detect collisions between a circle and an AABB, you should find the closest point from the AABB to the circle. You can run the below code snippet and try to move the purple point which represents the AABB center to watch the position of the closest point.

<iframe src="https://www.desmos.com/calculator/u32th8uhli?embed" width="500px" height="500px" style="border: 1px solid #ccc" frameborder=0></iframe>
The math behind it is to clamp the distance from AABB to circle on each axis separately. The value will be clamped on each axis if the distance is greater than the extent(half-width or height) or less than the negative extent. Like the below figure shows: enter image description here (Forgive my clumsy finger).
The pseudo-code is:

func CheckCollision(){
    vector3 distance = AABB.Center - Circle.Center;
    //Clamp(value,min,max)
    vector3 clampDst = Clamp(distance, -AABB.Bounds, AABB.Bounds);
    vector3 closestPoint = AABB.Center + clampDst;
    return (closestPoint - Circle.Center).SqrMagnitude > Circle.Radius;
}
Source Link
Ghoster
  • 61
  • 1
  • 3

To detect collisions between a circle and an AABB, you should find the closest point from the AABB to the circle. You can run the below code snippet and try to move the purple point which represents the AABB center to watch the position of the closest point.

<iframe src="https://www.desmos.com/calculator/u32th8uhli?embed" width="500px" height="500px" style="border: 1px solid #ccc" frameborder=0></iframe>
To math behind it is to clamp the distance from AABB to circle on each axis separately. The value will be clamped on each axis if the distance is greater than the extent(half-width or height) or less than the negative extent. Like the below figure shows: enter image description here (Forgive my clumsy finger).
The pseudo-code is:

func CheckCollision(){
    vector3 distance = AABB.Center - Circle.Center;
    //Clamp(value,min,max)
    vector3 clampDst = Clamp(distance, -AABB.Bounds, AABB.Bounds);
    vector3 closestPoint = AABB.Center + clampDst;
    return (closestPoint - Circle.Center).SqrMagnitude > Circle.Radius;
}