Scratch: Collision Detection

Collision Detection

Collisions are an intersection of two or more objects. Collision detection is the name for the game mechanic that detects intersecting objects. This is often a part of a game’s physics engine.

Collision detection must be contained inside a forever loop. The loop always runs and asks “is this sprite touching ___?” There are several sensor options for collision detection. Two of the most common are color collisions and sprite collisions.

Color Collisions

Color collisions are collisions detected using color. Each sprite can be scripted to trigger an event if it’s touching a certain color. Here’s an example script:

Scratch script for color collision detection with comments

The collision detection is created by using a conditional statement combined with the “touching color [ ]” which is Boolean (true or false) value.

This script returns a true or false value. It returns true if the sprite is touching the color, and false if not. If it returns true then a collision is detected and the script is triggered.

Sprite Collisions

Sprite collisions are collisions detected using a sprite. Each sprite can be scripted to trigger an event if it’s touching another sprite. Here’s an example script:

Scratch script for sprite collision detection with comments

The collision detection is created by using a conditional statement combined with the “touching sprite [ ]” which is Boolean (true or false) value. Select the name of the sprite you want to detect from the dropdown menu. In the above example, the sprite is named “Crystal.”

This script returns a true or false value. It returns true if the sprite is touching the selected sprite, and false if not. If it returns true then a collision is detected and the script is triggered.

Create some examples yourself. If you want to see the Scratch demo project where the examples are from, click this link: Example: Beginner Collisions.