FlutterIntermediate 3 min read

Collision detection is a question about shapes

Check circle overlaps, distinguish detection from response and test edge cases before adding graphics.

On this page

Collision detection asks whether shapes overlap. Collision response decides what happens next. Keeping these questions separate lets you test game rules without rendering a complete scene.

Compare circles without a square root

Two circles overlap when the distance between their centers is no greater than the sum of their radii. Comparing squared distances avoids calculating the square root.

main.dart
bool overlaps({required double ax, required double ay,
    required double ar, required double bx,
    required double by, required double br}) {
  final dx = ax - bx;
  final dy = ay - by;
  final radius = ar + br;
  return dx * dx + dy * dy <= radius * radius;
}

void main() {
  print(overlaps(ax: 0, ay: 0, ar: 10, bx: 15, by: 0, br: 10));
  print(overlaps(ax: 0, ay: 0, ar: 10, bx: 30, by: 0, br: 10));
}

Choose what touching means

The <= comparison counts exact contact as a collision. A strict < would require overlap. Either can be reasonable, but the game rule should be consistent.

Avoid repeated rewards

An overlap can persist across many frames. If a collectible grants a point on every frame, one collision may award many points. Remove the collectible or record that the collision has already been handled.

Know the simple model’s limits

A fast object can move from one side of a thin obstacle to the other between updates. Checking only the end positions may miss the collision. Swept tests or a physics engine can address more demanding simulations.

Compare squared distance to squared combined radius

Subtracting the centers gives dx and dy. Their squared sum is the squared distance. If that is no greater than the square of both radii added together, the shapes touch or overlap under this rule. Comparing squared values avoids an unnecessary square root while preserving the same inequality for non-negative distances.

Detection does not decide the reward or bounce. A collectible overlapping for several frames must not grant a new point on every update. Remove it or mark the event handled at the rule boundary.

Follow time-based movement and test exact contact, overlap and fast motion before using the check inside a game.

Try it yourself

Test circles that are far apart, exactly touching, overlapping and sharing a center. Write down the expected result before running each case.

Turn an overlap into one game event

Consider a player standing on a coin for three frames. Detection can report an overlap three times, while the game rule should award one coin. Keep a stable coin ID, mark it collected in the round’s rules and then remove or hide its component. An animation finishing later must not grant the reward again.

Test no contact, exact edge contact and full overlap with the same coordinate system. Then test two objects arriving in the same update. If event ordering matters, define it in the rules rather than depending on the order of component callbacks.

The AI planning example includes an idempotent collection rule. Use it to verify the event boundary before combining collision callbacks, sound and visual effects. Keep score updates independent of whether an effect was successfully loaded.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.