Collision detection is when you check if two things are touching. It happens all the time:
- A player touching a powerup
- A sword touching an enemy
- The mouse touching a button
Every interactive object in your game will have a space around it that checks for these collisions called a collision mask.
The red rectangles around the chicken and the nest are those objects’ collision masks. We can check for collision lots of ways, but here are the most common.
place_meeting(x, y, obj)
place_meeting asks “if I were to move to position(x, y), would my collision mask touch the collision map of obj?” The answer to that question is true (yes) or false (no).
instance_place(x, y, obj)
instance_place checks what instance of obj
will this instance collide with if this instance moves to position x
and y
. It returns the id of the colliding obj, or noone (-4) if no collision would take place.
instance_position(x, y, obj)
instance_position returns the first instance of obj
as position x
and y
. If there’s no instance there, noone (-4) is returned.