Collision Detection For Dummiez

The fifth level is almost done, gah!! It’ll be finished by next week, but right now I’m stuck re-doing one last boss. I swear I’m not just re-doing every boss over and over — I did three this week that only needed minor touch-ups and difficulty balancing — but sometimes I come back to my old work and can’t imagine what I was thinking. This boss is definitely one of those, and needs to be totally redone. I still like the art and animations, at least.

 

Without anything more meaningful to share, I thought I’d do an article that simply explains collision detection! If technical stuff puts you to sleep (or you’re already familiar with the topic, which is equally likely) you might wanna just stop reading here. I warned you!!

 

Remember this? Everything is an entity!

So in Bleed! Anything you can interact with is called an ‘entity’, and all entities have a width, a height, and a center point. Using these values, you can find the edges of a rectangular area called a ‘hitbox’. The hitboxes let entities know when they overlap (or ‘collide’) with each other.

I call the right edge the ‘maxX’, since that’s the rectangle’s max value in the X-axis.

 

How do the rectangles know when they’re overlapping? It’s actually pretty easy — they do four simple tests, comparing the edges of one rectangle to the other. If all four checks are passed, then the two rectangles must be colliding.

If the answer to all four questions is ‘yes!’ then the rectangles are overlapping.

 

Now the rectangles know if they’re in the same space, but what if they’re supposed to block each other? How does one entity tell where to move to get itself out of the other? That needs a little bit extra work. The one entity compares itself to the second, asking “am I above or below?” and “am I to the left or right?”. If it’s above, then moving upwards will be the quickest way out of the other rectangle, etc. Two of these questions are usually true (eg, one entity is both above the other and to the right of it), so the entity moves in the direction requiring the least movement.

A is above and to the left of B. It could either move up 6 or over 2, so it’ll move over 2.

 

So, yeah! I hope that wasn’t too much of a snoozefest. I’ll keep working away over here… and maybe play a little MGSV next week. Probably. As long as I get the fifth level done.