Chapter 24 — Collisions and Hitboxes
Part 2A — 2D Game Fundamentals · Chapter 24 of 62 · 8 h
Chapter 24 — Collisions and Hitboxes
Level: intermediate Estimated time: 8 h Prerequisites: chapter 20 (game loop and delta time), chapter 21 (Canvas and coordinates), chapter 22 (sprites), chapter 23 (velocity and simple physics) What you will be able to do by the end: write all the collision detection of a 2D game by hand — AABB, circles, point, circle-rectangle — then resolve it cleanly axis by axis, avoid tunneling, filter the tests with a spatial grid and masks, and make the game react (damage, pickups, death).
24.0 — Chapter objectives
By the end of this chapter, you will be able to:
- explain why collision is the building block that turns a cartoon into a game;
- clearly distinguish detection from resolution, and know that they are two different problems;
- define what a hitbox is and say why it must not be traced over the sprite;
- tell apart hitbox, hurtbox and trigger;
- define an axis-aligned rectangle (AABB) and write the corresponding class;
- state the AABB test in plain English before writing it in Dart;
- write the AABB test in code, and explain why we test for non-collision;
- write a circle-circle collision without ever calling
sqrt; - write a point-rectangle collision and a circle-rectangle collision;
- compute the overlap depth on X and on Y;
- resolve a collision by pushing back along the smallest axis;
- recognize and fix the bug of the character stuck to the wall;
- split movement into two passes, X then Y;
- detect the floor, the ceiling and the walls, and deduce
estAuSolfrom them; - explain tunneling and fix it with sub-stepping or with swept AABB;
- estimate the cost of an "everyone against everyone" test and reduce it;
- separate the broad phase from the narrow phase;
- implement a partitioning spatial grid;
- define collision layers and masks with bitwise operators;
- make the game react: damage, pickups, death;
- handle temporary invincibility after a hit;
- display the hitboxes in debug mode;
- name precisely what Flame will do for you starting in chapter 32;
- assemble a playable dungeon with walls, coins and a goblin.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion