🎮 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.
Extension: Add a reset or game-over on collision.
Learning Objectives
- Create solid wall/obstacle objects.
- Detect collisions between the player and walls.
- Respond to collisions using stop or bounce logic.
- Debug stuck/overlap issues at contact edges.
- Implement a reset or game-over flow (extension).
Key Concepts & Vocabulary
| Term | Definition |
|---|---|
| Collision | When two objects come into contact during gameplay. |
| Solid object | An object that prevents other objects from passing through. |
| Bounce | Direction change on impact, often reflecting velocity. |
| Reset/Game Over | Restarting the room or ending the run after a condition. |
Step-by-Step Build
1) Create a Wall Object
- Right-click Objects → Create Object → name it
obj_wall. - Assign a wall/brick sprite (or create a simple colored sprite).
- Check the “Solid” box so the player can’t overlap it.
2) Place Walls in the Room
- Open your room in the Room Editor.
- Place
obj_wallinstances around borders and as interior obstacles. - Save the room.
3) Add a Collision Event to the Player
- Open
obj_player. - Add Event → Collision → select
obj_wall. - 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
- Ensure
obj_wallis marked Solid. - If using continuous input, make sure you’re not forcing the player into the wall every step (zero out speed on collision).
- Prefer moving one axis at a time (separate horizontal/vertical resolution).
- Check sprite collision masks: tight masks reduce “phantom” hits; use a rectangle for simplicity while learning.
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)
| Task | Points |
|---|---|
Create obj_wall and mark as Solid | 20 |
| Implement player–wall Collision Event | 30 |
| Working bounce or stop behavior | 30 |
| Fix stuck/overlap issues | 10 |
| Extension (reset or game-over) | 10 |
| Total | 100 |
Reflection Questions
- How does a Solid object change collision behavior compared to a non-Solid?
- When would you choose bounce vs. stop in your game’s design?
- 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:
- Your PX_lastname_Collision.png with your Gamermaker Player visible.
- Your PX_lastname_Collision.mp4 Video showing game running.
- Your PX_lastname_Collision.yyp actual GameMaker program.
Save the files into your google drives under your class period and upload to Google Classroom.