Question

I am working a tile-based platform game which currently uses bounding boxes and overlap checks for collision detection. I find this approach inflexible. I want to have different collision responses depending upon the type of tile.

My new idea is to use 9 points which surround my player object and check which of these points is within the bounds of the tile. One point on each corner, each side and one in the center. Each tile type would use a different set of rules for collision handling.

However each tile type would have to use a complex set of conditional statements for collision testing - perhaps overly complicated. Any guidance?

Was it helpful?

Solution

This sounds like a case for Strategy patterns: http://en.wikipedia.org/wiki/Strategy_pattern

Define an interface that is "process collision" and then a number of implementations of that interface for different collision rules.

For each tile you then just setCollisionStrategy(MyRuleHere).

To check for a collision you just identify the tile then

if (tile.getCollisionStrategy.collide(player)) {
  // Collision
}

The collide method could even return an enumeration for different types of collision - or return a more complex object - or even process the collision itself and make changes in the player. The right approach there would depend on your overall architecture.

OTHER TIPS

Accurate collision detection usually has a huge impact on performance, so simply increasing the accuracy won't do it for a game.

A very good approach is to chain collision detection strategies for each tile.
Coarse strategies such as bounding box checks can be executed very quickly and can already rule out situations where there is certainly no collision (this is the most common case anyway).

The finer the strategies get, the more computation is involved, but again the goal is to detect certain collision/no-collision situations quickly and avoid computation time.

enter image description here

This example shows some common strategies you could chain to get fast and accurate collision detection:

  1. Bounding box (fastest)
  2. Shape (polygon, circle, bezier curve, ...)
  3. Alpha channel (slowest, most accurate)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top