fromClassToGame

A journey from a simple class to a game with the Java language https://mcgivrer.github.io/fromClassToGame/ , and open Hub https://www.openhub.net/p/fromClassToGame.

View the Project on GitHub mcgivrer/fromClassToGame

Collision System

Introduction

When in a game multiple objects are moving on screen, it often happened there are colliding together. The CollisionSystem intends to provide events on collision, and a mechanism to respond to such event.

To let Collision detection works, there are a bunch of way to encapsulate the colliding objects, and some of the algorithm are well known: Axis-Aligned Bounding Box, Oriented Bounding Box, and the shape of the bjects can be POINT, CIRCLE, ELLIPSE or RECTANGLE.

BoundingBox ?

A bounding box is a box containing the object the engine must manage, and delimiting the edges of this object. Collission detection between 2 objects is process against the respective bounding box of those objects:

+--------+
|  ****  |
| ****** |
|********|
|********|
| ****** |
|  ****  |
+--------+
   +--+
  /****\
 /******\
|********|
|********|
 \******/
  \****/
   +--+
    p1
     +**+  p2
    ******
   *******+ p3
p4 +*******
    ******
  p5 +**+ p6

The points p1 to p6 are juste tested against the other object. And if one of the point collides, boom !

Collision !

And detecting collision between Bounding bow will rely on the collision test between those shapes.

Rectangular bounding box

 (x1,y1)  w1
     O--------+
     |  ****  | shape1
     | ****** |
     |***(x2,y2) w2
  h1 |*****O--------+
     | ****|\ |***  | shape2
     |  ***| \|**** |
     +-----|--o*****| h2
           |********|
           | ****** |
           |  ****  |
           +--------+

In this diagram, a shape 1 collide a shape 2. shape 1 at (x1,y1) is sized of (w1,h1) and shape 2 is at (x2,y2) and sized of (w2,h2).

The collision penetration vector is (Oo).

The response will be defined accordingly to the shpae 1 and shape 2 velocity and mass.

Circular bounding box

For the case where bounding boxes are CIRCLEs, and the shapes position are centre aligned :

     d1
   +-^-+
  /**|**\ shape1
 /***|***\
|****O(x1,y1)
 \****\+---+
  \***/\****\
   +-/--\****\
    |****O(x2,y2)
     \***|***/
      \**|**/ shape2
       +-v-+
         d2

The collision detection is more simple :

if the distance between O(x1,y1) and O(x2,y2) is less than (d1 + d2), shape1 and shape2 are colliding !

Rectangles at center

You can also aligned your rectangular bounding box to the shapes center:

          w1
     +---------+
     |  *****  | shape1
     | ******* |
     |****O(x1,y1)
  h1 |******+--|------+
     | *****|  |****  | shape2
     |  ****| ******* |
     +------|-*+*O(x2,y2)        
            | ******* |
            |  *****  | h2
            +--------+
                w2

In this case, the collison detection is performed in the half height and half width of the shapes.