Collision Detection
Circle Segment
Computational Geometry
Game Development
Interactive Graphics

Circle to Circle Segment Collision

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In computational geometry and physics simulations, detecting collisions between shapes is fundamental. A common problem is detecting intersections between a circle and a line segment. This type of collision detection is crucial in various applications such as video games, computer graphics, and robotics. This article delves into the technicalities of circle to circle segment collision detection, offering explanations, examples, and a concise summary.

Basics of Collision Detection

Collision detection between a circle and a line segment involves determining if and where a circle intersects the given segment. A circle, defined by its center (Cx,Cy)(C_x, C_y) and radius rr, can potentially intersect a line segment defined by two endpoints (P1,P2)(P_1, P_2). The goal is to establish whether these two geometric entities overlap.

Mathematical Approach

Line Segment Representation

Assume we have a line segment with endpoints P1(x1,y1)P_1(x_1, y_1) and P2(x2,y2)P_2(x_2, y_2). The parametric equation of the line segment is given by:

P(t)=(1t)P_1+tP_2=(x_1+t(x_2x_1),y_1+t(y_2y_1))P(t) = (1 - t)P\_1 + tP\_2 = (x\_1 + t(x\_2 - x\_1), y\_1 + t(y\_2 - y\_1))

where 0t10 \leq t \leq 1. This parameter tt traces the line from P1P_1 to P2P_2.

Distance Calculation

To determine the intersection, compute the perpendicular distance from the center of the circle to the line defined by the segment. The distance from a point (Cx,Cy)(C_x, C_y) to the line (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2) can be calculated using the formula:

Distance=(y_2y_1)×C_x(x_2x_1)×C_y+x_2×y_1y_2×x_1(y_2y_1)2+(x_2x_1)2\text{Distance} = \frac{|(y\_2 - y\_1) \times C\_x - (x\_2 - x\_1) \times C\_y + x\_2 \times y\_1 - y\_2 \times x\_1|}{\sqrt{(y\_2 - y\_1)^2 + (x\_2 - x\_1)^2}}

Collision Detection

  1. Distance Check: First, check if the perpendicular distance from the circle's center to the line is less than or equal to the radius rr.
  2. Projection: Next, project the center of the circle onto the line segment and determine the closest point on the segment. This involves finding the projection using:
    t_closest=(C_xx_1)(x_2x_1)+(C_yy_1)(y_2y_1)(x_2x_1)2+(y_2y_1)2t\_{\text{closest}} = \frac{(C\_x - x\_1)(x\_2 - x\_1) + (C\_y - y\_1)(y\_2 - y\_1)}{(x\_2 - x\_1)^2 + (y\_2 - y\_1)^2}
  3. Segment Clamping: Clamp tclosestt_{\text{closest}} to ensure the closest point is on the line segment:
    t_clamped=max(0,min(1,t_closest))t\_{\text{clamped}} = \max(0, \min(1, t\_{\text{closest}}))
  4. Intersection Check: If the closest point on the segment is r\leq r from the circle's center, an intersection occurs.

Implementation

Here's a concise pseudo-code implementation:

Coincident Points: If P1P_1 equals P2P_2, treat as a point-to-circle distance check. • Tangency: If the circle is just tangent to the segment, the distance from the center to the segment equals rr. • Early Exit: Skip distance computation if the difference in bounds of xx or yy coordinates exceeds rr. • Spatial Partitioning: Utilize quadtree or grid systems for multiple object collision management.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.