XMLGameEngine

An engine for describing games built around Xerces, exprtk, and SFML.

View the Project on GitHub beefviper/XMLGameEngine

11. Collision detection and response

Status: implemented (swept object-against-object collision, edge-based screen collision, riding and rule exceptions; rough spots listed)

Decision

Separate the three things that were tangled in the first prototype:

  1. Detection: what happened, and where? Pure geometry, no side effects (CollisionDetector).
  2. Dispatch: what did the XML declare should happen for that event? (Game, via typed Commands.)
  3. Execution: do it (CommandExecutor).

In the prototype a single function both detected an edge hit and executed the action, and it parsed action tokens by walking a vector of strings with an iterator, which was fragile and hard to extend. Now makeCommand() (command.cpp) is the only place that turns a command tag into a typed command (CmdBounce, CmdStick, CmdMove, and so on), and responses are one std::visit per context.

The declarative form of the idea: the XML does not spell out an if-the-ball-touches-the-edge test. It says the ball has a collision with the vertical edge and the response is bounce(). The conditional lives in the engine’s detection, not in the data.

Current algorithm

Pixel collisions

A collision can say <type>pixel</type>. The box or circle sweep runs first, as always; only a pair it reports as touching is then walked in half-pixel steps, and bisected, to the first step where a drawn pixel meets a solid one. The response sees the same edge as for a box. Design 34 has the decisions.

Known weaknesses

Design notes worth keeping

Second batch: alternatives

Detection alternatives.

Naming and targets.

Response alternatives.

Sources