GameMaker Lesson 3: Collisions & Bounce

HS

🎮 Overview

Topic: Physics — Collisions & Bounce
Goal: Detect collision with walls/obstacles and respond by stopping or bouncing.
Duration: 45–60 minutes • Level: High School

Prerequisite: A project with a controllable obj_player from the previous lesson.
Skills: Collision events, solid objects, bounce/stop responses, debugging stuck movement.
Extension: Add a reset or game-over on collision.

Learning Objectives

  1. Create solid wall/obstacle objects.
  2. Detect collisions between the player and walls.
  3. Respond to collisions using stop or bounce logic.
  4. Debug stuck/overlap issues at contact edges.
  5. Implement a reset or game-over flow (extension).

Key Concepts & Vocabulary

Core terms used in this lesson.
TermDefinition
CollisionWhen two objects come into contact during gameplay.
Solid objectAn object that prevents other objects from passing through.
BounceDirection change on impact, often reflecting velocity.
Reset/Game OverRestarting the room or ending the run after a condition.

Step-by-Step Build

1) Create a Wall Object

  1. Right-click ObjectsCreate Object → name it obj_wall.
  2. Assign a wall/brick sprite (or create a simple colored sprite).
  3. Check the “Solid” box so the player can’t overlap it.

2) Place Walls in the Room

  1. Open your room in the Room Editor.
  2. Place obj_wall instances around borders and as interior obstacles.
  3. Save the room.

3) Add a Collision Event to the Player

  1. Open obj_player.
  2. Add EventCollision → select obj_wall.
  3. In the code block for this event, add one of the behaviors below (bounce or stop).
Tip: If movement is handled by setting x/y directly, consider bounce; if using hsp/vsp or speed/direction, you may prefer a stop-and-separate approach.

GML Code Snippets

A) Bounce off walls

// Collision Event in obj_player with obj_wall
// Make the player bounce off a solid wall (no "precise" angle calc)
move_bounce_solid(false);

B) Stop on collision (simple)

// Collision Event in obj_player with obj_wall
// Stop all motion when hitting a wall
speed = 0;

C) Stop and separate (common platformer/top-down approach)

// Example if you use hsp (horizontal speed) and vsp (vertical speed)
// 1) Horizontal step (in Step event):
//   x += hsp;
//   if (place_meeting(x, y, obj_wall)) {
//       while (!place_meeting(x - sign(hsp), y, obj_wall)) {
//           x -= sign(hsp);
//       }
//       hsp = 0;
//   }
//
// 2) Vertical step:
//   y += vsp;
//   if (place_meeting(x, y, obj_wall)) {
//       while (!place_meeting(x, y - sign(vsp), obj_wall)) {
//           y -= sign(vsp);
//       }
//       vsp = 0;
//   }
//
// Collision Event version (when present) can zero speeds too:
hsp = 0;
vsp = 0;

D) Extension: Reset/Game-Over on collision

// Option 1: Quick reset of the room with a message
show_message("Game Over!");
room_restart();

// Option 2: Reset player to a spawn point (store spawn_x/spawn_y in Create)
x = spawn_x;
y = spawn_y;

Debugging Stuck Movement

Pro tip: In the Room Editor, test different angles and speeds. If bounce feels odd, try move_bounce_solid(true) for “advanced” bounce which considers precise angles.

Pseudocode Summary

IF player collides with wall THEN
    EITHER:
        bounce off wall
    OR:
        stop movement and separate from wall
    IF extension enabled THEN
        show "Game Over"
        restart the room OR reset player position
END IF

Assessment Rubric (100 pts)

TaskPoints
Create obj_wall and mark as Solid20
Implement player–wall Collision Event30
Working bounce or stop behavior30
Fix stuck/overlap issues10
Extension (reset or game-over)10
Total100

Reflection Questions

  1. How does a Solid object change collision behavior compared to a non-Solid?
  2. When would you choose bounce vs. stop in your game’s design?
  3. What steps help prevent the player from getting stuck in walls?

Extension Challenge

Build a mini-maze. If the player touches a “danger wall,” trigger a message and reset or end the run. Add a timer and track the best time to complete the maze.

Submission

Submit 3 files:

Save the files into your google drives under your class period and upload to Google Classroom.