11.2. Collision-handling
Here’s an overview of how the Pop Framework handles collisions.
Make a utility class for holding pairs of critters, and let this class specify which critter has the priority to control the behavior of a collision.
Maintain a collection of all pairs of critters that might meaningfully collide.
During each game step, iterate through this collection of candidate pairs.
For each candidate pair, collide the critters by letting the higher priority critter call its collide method on the other.
A critter’s collide(pcritter) method checks if it’s touching pcritter, and if so, changes the two critters’ velocities and positions in a physically reasonable fashion. Child classes may override collide to alter the behavior.
We said a bit ...
Get Software Engineering and Computer Games now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.