TExcelTAD
Home › Math for Games › Circles and Spheres

Circles and Spheres

Circles (in 2D) and spheres (in 3D) are the simplest collision shapes there are: a center and a radius. They are rotation-invariant, so turning an object never changes its circle, and they make the cheapest possible tests. Bullets, pickups, explosion radii, character "personal space", and bounding spheres for culling all use them.

Circle vs circle

Two circles touch when the distance between their centers is at most the sum of their radii. Compare squared values to skip the square root:

text
|c2 - c1|² <= (r1 + r2)²
cpp · try it yourself
#include <iostream>

struct Vec2 { double x, y; };
struct Circle { Vec2 c; double r; };

bool overlaps(const Circle& a, const Circle& b) {
    double dx = b.c.x - a.c.x, dy = b.c.y - a.c.y;
    double rr = a.r + b.r;
    return dx * dx + dy * dy <= rr * rr;
}

int main() {
    std::cout << std::boolalpha;
    Circle player{{0, 0}, 0.5};
    Circle pickups[] = {{{1.0, 0.0}, 0.6}, {{0.6, 0.6}, 0.3}, {{2.0, 2.0}, 1.0}, {{3.0, 0.0}, 2.5}};
    for (const auto& p : pickups)
        std::cout << "pickup at (" << p.c.x << ", " << p.c.y << ") r=" << p.r << ": " << overlaps(player, p) << "\n";
}
pickup at (1, 0) r=0.6: true
pickup at (0.6, 0.6) r=0.3: false
pickup at (2, 2) r=1: false
pickup at (3, 0) r=2.5: true

Pushing circles apart

Resolving the overlap is just as easy: push along the line between the centers by the penetration depth. Split the push between both objects (or by inverse mass) so neither one "wins". This is the core of a simple crowd or particle system.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>

struct Vec2 { double x, y; };
struct Circle { Vec2 c; double r; };

void separate(Circle& a, Circle& b) {
    double dx = b.c.x - a.c.x, dy = b.c.y - a.c.y;
    double dist = std::sqrt(dx * dx + dy * dy);
    double depth = a.r + b.r - dist;
    if (depth <= 0) return;
    double nx = 1, ny = 0;                 // centers on top of each other: pick any direction
    if (dist > 1e-9) { nx = dx / dist; ny = dy / dist; }
    a.c.x -= nx * depth / 2; a.c.y -= ny * depth / 2;
    b.c.x += nx * depth / 2; b.c.y += ny * depth / 2;
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Circle a{{0, 0}, 1}, b{{1.2, 0.9}, 1};
    separate(a, b);
    double d = std::hypot(b.c.x - a.c.x, b.c.y - a.c.y);
    std::cout << "a (" << a.c.x << ", " << a.c.y << ")  b (" << b.c.x << ", " << b.c.y << ")  distance " << d << "\n";

    Circle p{{5, 5}, 0.5}, q{{5, 5}, 0.5};     // exactly on top of each other
    separate(p, q);
    std::cout << "p (" << p.c.x << ", " << p.c.y << ")  q (" << q.c.x << ", " << q.c.y << ")\n";
}
a (-0.200, -0.150)  b (1.400, 1.050)  distance 2.000
p (4.500, 5.000)  q (5.500, 5.000)

After separating, the distance is exactly 2.000, the sum of the radii. The second case guards against a classic NaN: two objects spawned at the same spot have no direction between them.

Circle vs AABB

To test a circle against a box, find the closest point of the box to the circle's center (clamp the center into the box on each axis), then check that point's distance. It is exact, cheap, and the start of the Closest Point lesson.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <algorithm>

struct Vec2 { double x, y; };
struct AABB { Vec2 min, max; };

bool circleVsBox(Vec2 c, double r, const AABB& b, Vec2& closest) {
    closest = {std::clamp(c.x, b.min.x, b.max.x), std::clamp(c.y, b.min.y, b.max.y)};
    double dx = c.x - closest.x, dy = c.y - closest.y;
    return dx * dx + dy * dy <= r * r;
}

int main() {
    std::cout << std::boolalpha << std::fixed << std::setprecision(1);
    AABB box{{0, 0}, {4, 2}};
    Vec2 balls[] = {{5, 1}, {4.6, 2.6}, {4.8, 2.8}, {2, 1}};
    for (Vec2 c : balls) {
        Vec2 k;
        bool hit = circleVsBox(c, 1.0, box, k);
        std::cout << "ball at (" << c.x << ", " << c.y << ") closest (" << k.x << ", " << k.y << ") hit: " << hit << "\n";
    }
}
ball at (5.0, 1.0) closest (4.0, 1.0) hit: true
ball at (4.6, 2.6) closest (4.0, 2.0) hit: true
ball at (4.8, 2.8) closest (4.0, 2.0) hit: false
ball at (2.0, 1.0) closest (2.0, 1.0) hit: true

The two corner cases show why the closest-point method beats "expand the box by the radius": a ball at (4.8, 2.8) is inside the expanded box but more than 1 unit from the corner.

Bounding spheres

In 3D, every mesh gets a bounding sphere as well as an AABB. Frustum culling, LOD distance and "is anything near me?" queries test spheres first. A sphere is also the natural shape for explosions: the damage falls off with distance / radius.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>

struct Vec3 { double x, y, z; };
double dist(Vec3 a, Vec3 b) { return std::sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z)); }

int main() {
    std::cout << std::fixed << std::setprecision(1);
    Vec3 blast{0, 0, 0};
    const double radius = 8, maxDamage = 100;
    struct { const char* name; Vec3 pos; } units[] = {
        {"tank   ", {1, 0, 1}}, {"soldier", {4, 0, 3}}, {"drone  ", {0, 6, 5}}, {"truck  ", {12, 0, 0}},
    };
    for (auto& u : units) {
        double d = dist(blast, u.pos);
        double falloff = std::clamp(1 - d / radius, 0.0, 1.0);    // linear falloff to the edge
        std::cout << u.name << " at distance " << std::setw(5) << d << ": damage " << maxDamage * falloff << "\n";
    }
}
tank    at distance   1.4: damage 82.3
soldier at distance   5.0: damage 37.5
drone   at distance   7.8: damage 2.4
truck   at distance  12.0: damage 0.0

Tip: For explosion damage, also cast a ray from the center to each target. Without it, grenades kill players through walls.

Test yourself

Two circles with radii 2 and 3 have centers 4 units apart. Do they overlap?

  1. Yes
  2. No
  3. Only if both are moving
  4. It depends on the angle

How do you find the closest point of an AABB to a circle's center?

  1. Take the box center
  2. Clamp the circle center into the box on each axis
  3. Take the nearest corner
  4. Normalize the center

Why check for a zero distance before separating two overlapping circles?

  1. To save time
  2. With identical centers the push direction is undefined and normalizing gives NaN
  3. Because radii can be negative
  4. It is not needed