Richard's Arcade Tips #6 - Hitboxes, Overlaps, and Collisions (Part 1)

Hey folks!

It’s been a while since I did one of these!

Today I want to talk about hitboxes, overlaps, and collisions. This is something I’ve posted about many times when helping people with their games, but I figured it would be a good idea to have a post that people could point to in the future with an explanation of how these work in Arcade.

This is going to be a three part post. In this first part, we’re going to look at how these mechanics work. In part 2, we’re going to talk about wall collisions and clipping. Part 3 will have some tips and tricks for avoiding common pitfalls when it comes to collisions in your game!

Anyways, read on! You might learn something new!

The two types of overlaps

In Arcade, there are two types of checks that we perform to see if two things are overlapping:

  • Pixel-perfect overlap - used in on sprite overlaps with otherSprite checks
  • Hitbox overlap - used in tilemap on tile overlap events and wall collisions

Let’s take a look at how both of these work! Also, you can click on any of the images/animations below to get the code for that example!

Pixel-perfect overlaps

Pixel-perfect overlaps work by placing two sprites on top of each other and looping over all of the pixels to see if any of them touch. Here’s an animation showing how that works:

overlap-animation

The important thing to note here is that only filled-in pixels count! Transparent pixels are completely ignored in pixel-perfect checks:

no-overlap-animation

You can even have one sprite be completely within another! As long as they don’t have filled-in pixels overlapping one another, it doesn’t count:

circle-overlap

Hitbox overlaps

Hitbox overlaps are simpler than pixel-perfect overlaps. Instead of looping over all of the pixels, hitboxes work by drawing a rectangle around all of the filled-in pixels and checking to see if that rectangle overlaps with anything. In the animation below, the red rectangle is the hitbox:

hitbox-overlap

Even though the tile in that animation is only touching transparent pixels in the sprite, it still counts as an overlap! Another thing to note about hitboxes is that each sprite only has one hitbox. If you have an image that has two or more completely disconnected groups of pixels, the hitbox will be a rectangle that encompasses all of them. For example:

hitbox-overlap2

To show how different hitbox overlaps are from pixel-perfect ones, let’s take a look at our green circle sprite again:

hitbox-overlap-ring

When the tile is fully within the ring it counts as an overlap even though only transparent pixels are touching it.

Tile Hitboxes

Finally, let’s talk about the hitboxes of tiles. Unlike sprite hitboxes, the hitboxes for tiles and walls always take up the full width and height of the tile. Transparent pixels don’t matter when calculating the hitboxes of tiles! If you have a 16x16 tile, the hitbox will always be 16x16 pixels.

Anyways, that’s all for part one! I’ll be posting part two later this afternoon after livestream.

20 Likes

“Wow, cool animations!”
*accidentally clicks on one of them*
“Wait, the animations are makecode projects!/!?1!/?!/1!”
*clicks on the rest of them*

2 Likes

AMazing but is there a way tweek tile overlapping or change your overall hitbox

I’ll be giving some tips on how to do this in part 3 of the series, which should be coming out tomorrow!

1 Like