Mixed collision detection

We have seen that a single shape does not fit all cases, so we are going to update our game to allow us to define which body shape each ScreenGameObject uses for collisions. For this, we are going to create an enum of body types and have a variable to store that information in ScreenGameObject.

The enum BodyType is as follows:

public enum BodyType {
  None,
  Circular,
  Rectangular
}

In the case of sprites, we will add a parameter to the constructor that specifies body type. Note that we have a special type called None. This is used for sprites that do not collide with others. While there are none of those in our game yet, other types of games can have them—for example, floor tiles on a dungeon crawler.

Note

We may want to have ...

Get Android Game Programming: A Developer’s Guide 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.