You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the physics section, you can add an algorithm that allows you to detect collisions of different types of primitives. This will be useful when creating games, etc.
Axis-Aligned Bounding Box (AABB) Collision Detection
An AABB is a rectangle whose edges are aligned with the coordinate axes. Collision detection between two AABBs is relatively straightforward:
Define Boundaries: Each rectangle is defined by its minimum and maximum x and y coordinates (or alternatively, by its top-left corner and width/height).
Check for Overlap: For two rectangles to collide, they must overlap in both the x and y axes. This can be checked with simple comparisons.
The conditions for two AABBs to intersect can be expressed as:
Rect1.x < Rect2.x + Rect2.width
Rect1.x + Rect1.width > Rect2.x
Rect1.y < Rect2.y + Rect2.height
Rect1.y + Rect1.height > Rect2.y
If all these conditions are true, the rectangles intersect.
In the physics section, you can add an algorithm that allows you to detect collisions of different types of primitives. This will be useful in creating games, etc.
The Collision Detection Algorithm for Different Primitives
Below, I've given an implementation of a collision detection algorithm that includes axis-aligned bounding boxes (AABB) and circles.
importmathclassAABB:
def__init__(self, x, y, width, height):
self.x=xself.y=yself.width=widthself.height=heightclassCircle:
def__init__(self, x, y, radius):
self.x=xself.y=yself.radius=radiusdefcheck_aabb_collision(box1, box2):
"""Check if two axis-aligned bounding boxes (AABB) collide."""if (box1.x<box2.x+box2.widthandbox1.x+box1.width>box2.xandbox1.y<box2.y+box2.heightandbox1.y+box1.height>box2.y):
returnTruereturnFalsedefcheck_circle_collision(circle1, circle2):
"""Check if two circles collide."""distance=math.sqrt((circle1.x-circle2.x) **2+ (circle1.y-circle2.y) **2)
returndistance< (circle1.radius+circle2.radius)
defcheck_aabb_circle_collision(box, circle):
"""Check if an AABB and a circle collide."""closest_x=clamp(circle.x, box.x, box.x+box.width)
closest_y=clamp(circle.y, box.y, box.y+box.height)
distance_x=circle.x-closest_xdistance_y=circle.y-closest_ydistance_squared=distance_x**2+distance_y**2returndistance_squared< (circle.radius**2)
defclamp(value, min_value, max_value):
returnmax(min_value, min(value, max_value))
# Example in usageif__name__=="__main__":
box1=AABB(0, 0, 10, 10)
box2=AABB(5, 5, 10, 10)
box3=AABB(20, 20, 5, 5)
circle1=Circle(5, 5, 3)
circle2=Circle(15, 15, 5)
print(check_aabb_collision(box1, box2)) # Output: Trueprint(check_aabb_collision(box1, box3)) # Output: Falseprint(check_circle_collision(circle1, circle2)) # Output: Falseprint(check_aabb_circle_collision(box1, circle1)) # Output: True
Separating Axis Theorem (SAT) for Convex Polygons
The Separating Axis Theorem (SAT) can be used to detect collisions between any two convex shapes. Here's an extended implementation:
I have extended the collision detection implementation to include:
Axis-Aligned Bounding Boxes (AABB)
Circles
Convex polygons using the Separating Axis Theorem (SAT)
These algorithms, as exhibited above, can now be used to detect collisions between different types of primitives, which is useful for creating games and other applications that require physics simulations.
I'm Experienced when it come to data structures and algorithms. I cracked some leetCode problem in the past concerning collision detection.
I'm Confident I can pull this up.
If you are interested in resolving an open issue, simply make a pull request with your proposed fix. We do not assign issues in this repo so please do not ask for permission to work on an issue.
Do not create an issue to contribute an algorithm. Please submit a pull request instead.
Activity
ayushh0406 commentedon Feb 11, 2025
Axis-Aligned Bounding Box (AABB) Collision Detection
An AABB is a rectangle whose edges are aligned with the coordinate axes. Collision detection between two AABBs is relatively straightforward:
Define Boundaries: Each rectangle is defined by its minimum and maximum x and y coordinates (or alternatively, by its top-left corner and width/height).
Check for Overlap: For two rectangles to collide, they must overlap in both the x and y axes. This can be checked with simple comparisons.
The conditions for two AABBs to intersect can be expressed as:
Rect1.x < Rect2.x + Rect2.width
Rect1.x + Rect1.width > Rect2.x
Rect1.y < Rect2.y + Rect2.height
Rect1.y + Rect1.height > Rect2.y
If all these conditions are true, the rectangles intersect.
Rishiram20757 commentedon Feb 12, 2025
function isColliding(rect1, rect2) {
return (
rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y
);
}
const rectA = { x: 10, y: 20, width: 50, height: 50 };
const rectB = { x: 40, y: 50, width: 50, height: 50 };
console.log(isColliding(rectA, rectB)); // becomes true if they overlap
this is just an snippet for an example
1803053530 commentedon Mar 7, 2025
1️⃣ AABB (Axis-Aligned Bounding Box):
def check_aabb(rect1, rect2): # [(x,y,w,h), ...]
return not (rect1+rect1 < rect2 or
rect2+rect2 < rect1 or
rect1+rect1 < rect2 or
rect2+rect2 < rect1)
Best for simple rectangles, O(1) time complexity
2️⃣ Circle Collision:
import math
def circle_collision(c1, c2): # [(x,y,r), ...]
dx = c2 - c1
dy = c2 - c1
return math.sqrt(dx2 + dy2) <= (c1 + c2)
Distance-based check, accurate for circular shapes
Need further tweaks? Happy to refine!
joey-the-33rd commentedon Mar 11, 2025
Collision Detection Algorithm
Feature Description
In the physics section, you can add an algorithm that allows you to detect collisions of different types of primitives. This will be useful in creating games, etc.
The Collision Detection Algorithm for Different Primitives
Below, I've given an implementation of a collision detection algorithm that includes axis-aligned bounding boxes (AABB) and circles.
Separating Axis Theorem (SAT) for Convex Polygons
The Separating Axis Theorem (SAT) can be used to detect collisions between any two convex shapes. Here's an extended implementation:
Summary
I have extended the collision detection implementation to include:
These algorithms, as exhibited above, can now be used to detect collisions between different types of primitives, which is useful for creating games and other applications that require physics simulations.
Adagedo commentedon Jun 19, 2025
I'm Experienced when it come to data structures and algorithms. I cracked some leetCode problem in the past concerning collision detection.
I'm Confident I can pull this up.
Add collision detection algorithm (TheAlgorithms#12569)
cclauss commentedon Sep 6, 2026
https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md#issues
If you are interested in resolving an open issue, simply make a pull request with your proposed fix. We do not assign issues in this repo so please do not ask for permission to work on an issue.
Do not create an issue to contribute an algorithm. Please submit a pull request instead.