[024] Circle Rect Collision
[024] Circle Rect Collision
[15h] Math about Circle & Rectangle Collisions
[Feb 23] 2h - Circle Rect Collision Study
[Feb 24] 7h - Circle Rect Collision Study
[Feb 25] 6h - Circle Rect Collision UI
Learned the collision math between Circle & Rectangle,
so I can add it to my flocking logic to avoid walls.
var r: Rect2
var c_pos: Vector2
var c_radius: float
# Circle Rectangle Bounce [stackoverflow.com/a/45373126]
c_nearest.x = maxf(r.position.x, minf(c_pos.x, r.position.x + r.size.x)) # closest point in rectangle
c_nearest.y = maxf(r.position.y, minf(c_pos.y, r.position.y + r.size.y))
var distance: Vector2 = _circle_position - _nearest # nearest distance to circle center
var normal: Vector2 = Vector2(-distance.y, distance.x) # normal vector
var normal_angle: float = atan2(normal.y, normal.x) # normal in radians (3.1416)
var incoming_angle: float = atan2(_circle_velocity.y, _circle_velocity.x) # circle velocity in radians
var theta: float = normal_angle - incoming_angle
_circle_velocity = _circle_velocity.rotate(2 * theta) # bounce velocity
# 2D Geometry Library [youtu.be/QZFH7yrJ4H4]
# Circle overlaps Rectangle
var c_nearest: Vector2
c_nearest.x = maxf(r.position.x, minf(c_pos.x, r.position.x + r.size.x))
c_nearest.y = maxf(r.position.y, minf(c_pos.y, r.position.y + r.size.y))
var c_distance: float = c_pos.distance_to(c_nearest) # (c_nearest - c_pos).length()
if c_distance - c_radius < 0:
# overlaps if distance is less than circle radius
| Status | Released |
| Platforms | HTML5 |
| Author | QuietGodot |
| Made with | Godot |

Leave a comment
Log in with itch.io to leave a comment.