# Sweep and Prune (SAP)

Sweep and Prune (SAP) is a broad-phase collision detection algorithm commonly used in computer graphics and game development. Its purpose is to efficiently identify potential collisions between objects in a 3D space by reducing the number of object pairs that need to be checked for actual collision.

Here's how the Sweep and Prune algorithm works:

1. Axis Projection:

• The algorithm starts by projecting the bounding boxes of all objects onto the three coordinate axes (X, Y, and Z).
• For each axis, the minimum and maximum coordinates of each object's bounding box are recorded.
2. Sorting:

• The minimum and maximum coordinates of the objects' bounding boxes are sorted along each axis independently.
• This sorting creates three lists, one for each axis, containing the objects' coordinates in ascending order.
3. Sweeping:

• The algorithm then sweeps through each sorted list from the smallest to the largest coordinate.
• As it sweeps, it keeps track of the objects that are currently overlapping on that particular axis.
4. Pruning:

• If two objects are found to be overlapping on all three axes simultaneously, they are considered as a potential collision pair.
• These potential collision pairs are added to a list for further collision detection, such as narrow-phase collision detection.
5. Updating:

• When objects move or change their positions, their bounding box coordinates are updated in the sorted lists.
• The sweeping process is repeated to identify any new potential collision pairs.

The Sweep and Prune algorithm is efficient because it reduces the number of object pairs that need to be checked for collision. By sorting the coordinates and sweeping through them, it can quickly identify objects that are far apart and eliminate them from further consideration. This is particularly useful in scenarios with a large number of objects, as it helps to focus the collision detection efforts on objects that are more likely to collide.

However, it's important to note that Sweep and Prune is a broad-phase collision detection algorithm, meaning it only identifies potential collision pairs. To determine the actual collisions between objects, a narrow-phase collision detection algorithm, such as GJK (Gilbert-Johnson-Keerthi) or SAT (Separating Axis Theorem), is typically used on the potential collision pairs identified by Sweep and Prune.

Overall, Sweep and Prune is a widely used algorithm in collision detection systems due to its efficiency in culling distant objects and reducing the computational overhead of collision checks in complex scenes.