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 collisions a few ways.
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 asks “if I were to move to position(x, y), which obj would I be colliding with?” The answer to that question is the id of the colliding obj, or noone (-4) if no collision would take place.
instance_position(x, y, obj)
position_meeting asks “Which obj would I touch at position(x,y)?” The answer to that question is the id of the colliding obj, or noone (-4) if no collision would take place.