Ask a Game Dev 2023-05-04 19:02:08

It makes the math easier. 

Computers can’t actually see and have no inherent concept of things colliding with each other, so the only way for them to tell whether two objects collide is through math. We know information like an object’s position, orientation, and shape. We need to do the math to figure out whether that shape interpenetrates another shape.

The simpler the shape, the easier it is to do math on them. The easiest shape is actually spheres - since all points on a sphere are the same distance from the center, I just need to check the distance from the centers of the spheres to each other. The orientation isn’t important. Like so:

image

If the distance between the centers is less than the sum of the radii of the spheres, they have collided. 

image

Boxes are also super simple, because they have a bunch of properties that make the math easier. For example, all boxes have four sides. All boxes have their sides at right angles to each other. Thus, all boxes have sides that are also parallel to each other. As long as the boxes are all oriented in the same direction, we can assume that all box sides of the same orientation are parallel to each other for all boxes. Thus, we only need to know the position, the length, and the width to represent a box. Like so:

image

We can actually use math to represent these boxes. We know the position of the lower left corner of box A (x, y) and height and width, so we know the horizontal and vertical values for each of its edges:

  • Left edge: horizontal position = x, vertical position = all points from y to y + height
  • Right edge: horizontal position = x + width, vertical position = all points from y to y + height
  • Bottom edge: horizontal position = all points from x to x + width, vertical position = y
  • Top edge: horizontal position = all points from x to x + width, vertical position = y + height

We can use this to represent every rectangle, because all rectangles abide by the same mathematical rules.

Now let’s actually get into the detection of the overlap. When dealing with rectangles, it’s actually easier to tell if there isn’t a collision than if there is. I can isolate all of the situations where a collision is impossible, check for those, and then say if any of these conditions comes back true, there’s cannot have been a collision. If all of these conditions come back false, there must have been a collision. 

image

A collision is impossible if…

  • … the leftmost edge of box B is to the right of the rightmost edge of box A
  • … the rightmost edge of box B is to the left of the leftmost edge of box A
  • … the topmost edge of box B is below the bottommost edge of box A
  • … the bottommost edge of box B is above the topmost edge of box A

If any of these requirements are met, there can be no collision. But logic dictates that if none of these requirements are met, then there must have been a collision, because these combined conditions are the only criteria where a collision is impossible. That’s [contrapositive] logic for you!

image

Remember, this is only in 2 dimensions. In three dimensions, we’re not just talking about edges anymore, but sides - topmost side, bottommost side, leftmost side, rightmost side, frontmost side, backmost side. There’s more conditions to consider.

Now… let’s say that the boxes aren’t all oriented in the same direction, but we keep all the other factors the same. 

image

Do you see how this small change suddenly makes the previously “simple” math a lot more complicated? Now we need to start taking the angle of rotation of box B relative to box A into account before we can tell whether the boxes collide, and all of math needs to be updated for this. We can’t make the assumption that the edges are guaranteed to be parallel anymore, and our calculations need to reflect this. Now consider using other geometric shapes instead. That also affects the math, and it affects it in very complex ways.

Then remember that we have to do these collision calculations for each collideable shape against each other collideable shape in the scene. We have to do a lot of these calculations every frame all the time. This is why simple shapes are the best - because it simplifies the math involved.

image

So… why do we use boxes and not silhouettes? Because it makes the math easier.


Got a burning question you want answered?

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *