Arduboy Course - Part IV: Collision Detection
date
Jun 12, 2024
type
KnowledgeBase
year
slug
arduboy-basics-4
status
Published
tags
Arduino
C++
Microcontroller
summary
Let’s collect coins and bump into platforms!
Previous Chapter: Part III: ANIMATED SPRITES
Collision Detection
Onwards to the next piece of the puzzle: Things we can run into!
1. Making an Obstacle
Our level is going to be made out of individual obstacles.
So let’s define our own custom data type that holds all the data we need to describe an obstacle:
- x position
- y position
- width
- height
We’ll make a
struct and we’ll call it Obstacle, then we define what it can contain:Now we can create a variable of type
Obstacle and fill it with data!We can now access and even change the values in our variable
oBut most importantly: we can use it to draw our obstacle:
Full example:

2. Checking if a position is inside our Obstacle
How do we check if our player character with it’s
x position, y position and radius is colliding with our obstacle?Let’s make a function that checks if the bounding box of our character intersects with the obstacle!
Let’s look at the signature of the function:
bool isColliding(const Obstacle& obstacle, Entity& entity)boolis the return type, meaning our function will eventually have to return a value oftrueorfalse
isCollidingis the name of our function
const Obstacle& obstacleis the first argument the function expects. Let’s break it down furtherconstmeans the value of this argument cannot be changed within the functionObstacleis the expected type of the argument- The
&means that we’re handing over a reference to the original variable, not a copy. obstacleis the name under which we can access this variable inside the function
Entity& entityis the second argument the function expectsEntity&is the expected type of the argument - again with a&to make sure we’re getting a reference to the actual object and not a copy. And this time there’s no const, so we can change it the object - which we will do later!
We can now call this function like this:
Since the function returns
true or false, we can simply wrap it in an if to do something when a collision is detected!So what’s an “Obstacle” for us? It can be anything that we can collide with. So far we’re not preventing the collision, we’re just detecting the intersection. Let’s work on 2 things next: collecting a 🪙 coin and colliding with a 🔳 platform…
Collecting a 🪙 Coin Obstacle
For this we need to a add 1 thing to our
Obstacle class:- A bool to say if the obstacle is gone (collected, destroyed, whatever), let’s call it
gone
Updated code:
We can still create an instance the same way (
gone will simply get the default bool value of false if we don’t specify it), or we can add a value for gone explicitly:React to the Collision
- Now when we collide with a coin obstacle, we can set
gone = true;
- And when we render the obstacle we check if
gone == true, and simply don’t render it if it is.
Let’s bring it all together into a playable little program where you can walk around to collect a coin!
We now have a coin we can collect!

Colliding with a Platform Obstacle
Let’s make a second type of Obstacle! One we can actually collide with: a platform.
Let’s add another new variable to our
Obstacle struct that lets us specify an obstacleType:- An integer called
obstacleType- so we can distinguish what type of Obstacle it is that we’re colliding with. Is it a collectible, or is it a platform?
Then we’ll also need code to keep the player from going into the Obstacle. Let’s change several things:
- Let’s change the
isCollidingmethod to take oneObstacleand oneEntityvariable as its arguments
- Let’s add code for keeping the player outside the obstacle if the obstacle is of
obstacleType0 (we arbitrarily decided that type “0” would mean “Platform”).

It works! We can move our sphere around, collect the coin and jump on top of the platform! BUT you can see how the code is turning into a somewhat unwieldy wall of code. So our next job will be to deal with that and make everything more reusable in the process.


Next Chapter: Part V: Level