19 Feb 2017

The basics of Arduino game programming: Objects Collision Detection. (Part 5)

Let's try to understand how we can to know are objects touching each other or not. This method could be implemented in game development, automatization, tracking etc.

We know that in most cases especially in programming a top left corner is a beginning of two-dimensional coordinates and object itself as well. Height and width are parameters of a box. This simple box is often called a bounding box.

0,0          X
  +--------------------->
  |
  |
  |    X,Y    W
  |      ---------+
Y |      |         |
  |      |         |
  |    H |         |
  |      |         |
  |      |         |
  |      +---------+
  |
  v

Imagine that two boxes are in collision. There are four situations when objects are touched. Firstly, we have a look at X's of Right edge of A and Left edge of B.

0,0          X
  +--------------------->
  |
  |      Offset
  |      +-------->X
  |    X,Y    W
  |      +---------
Y |      |         |
  |      |         |
  |    H |    A    |
  |      |         |
  |      |       ---------+
  |      +---------+       |
  |              |         |
  v              |    B    |
                 |         |
                 |         |
                 +---------+
thus,

A.x + A.width >= B.x

What we do here? Just look at the X values of two boxes. To check X's on the right edge of A box, we add an offset which is width. Follow for on the diagrams.

Next, is Left edge of A and Right edge of B. At similar manner, we are analyzing this case:

0,0          X
  +--------------------->
  |
  |
  |
  |    X,Y    W
  |      ---------+
Y |      |         |
  |      |       Offset
  |    H |    A  +-------->X
  |      |         |
  |      |       +---------
  |      +---------+       |
  |              |         |
  v              |    B    |
                 |         |
                 |         |
                 +---------+

A.x <= B.x + B.width

Ok, let's look at Y's. Bottom of A and top of B boxes.

0,0          X
  +--------------------->
  |
  |
  |
  |    X,Y    W
  |  +   +---------+
|  |   |         |
  |  |   |         |
  |  | H |    A    |
  |  |   |         |
  |  v   |       ---------+
  |  X   ---------+       |
  |              |         |
  v              |    B    |
                 |         |
                 |         |
                 +---------+

A.y + A.height >= B.y

Last one. Top of A and bottom of B.

0,0          X
  +--------------------->
  |
  |
  |
  |    X,Y    W
  |      ---------+
Y |      |         |
  |      |         |
  |    H |    A    |
  |      |         |
  |      |     + +---------+
  |      +---------+       |
  |            | |         |
  v            | |    B    |
               | |         |
               v |         |
               X ---------+

A.y <= B.y + B.height

Finally we have conclusion:

If

(A.x + A.width >= B.x)

and

(A.x <= B.x + B.width)

and

(A.y + A.height >= B.y)

and

(A.y <= B.y + B.height)

then:

"Object in a collision!"


Here is Arduino code. Look at checkCollision() function, which is identical to theoretical explanation.

void checkCollision() {
  if (( (targetX + targetW > playerX) and
        (targetY + targetH > playerY) and
        (targetX < playerX + playerW) and
        (targetY < playerY + playerH) ))
  {
    digitalWrite(LED, HIGH);
    soundStart();
  } else {
    digitalWrite(LED, LOW);
  }
}

No comments:

Post a Comment

Popular Posts