Collisions

When planning motions or deciding on certain actions, it is often important to see whether there are collisions with other objects at certain places within the game world, and often choosing the right collision function for the job is the most important task of all. GameMaker has a number of built-in functions to help you deal with collisions correctly and in accordance with the needs of your project.

How To Check Collisions

Collisions With Objects

The simplest way to check for collisions against objects is to use place_meeting.

Step Event

if (!place_meeting(x + 4, y, obj_rock))
{
    x += 4;
}

This checks for collisions against instances of obj_rock only. If there is no rock at x + 4, it changes the X position to move the instance there.

Collisions With Multiple Objects

You can check for collisions against multiple objects, in two ways:

Using the second method of passing an array, your code would look like this:

if (!place_meeting(x + 4, y, [obj_rock, obj_bush]))
{
    x += 4;
}

This checks for collisions against instances of obj_rock and obj_bush in the same function call.

TIP It is recommended to use the parenting method for multiple collision checks, as you can use the same parent in multiple function calls, without having to keep track of an array.

The place_meeting function only returns true or false. If you need to access the instance that was found in collision, use instance_place, which returns an instance handle.

Collisions With Tile Maps

In addition to objects, you can also pass a Tile Map Element ID to a collision function. This will check for collisions with that Tile Map.

Use layer_tilemap_get_id to get the Tile Map ID for a layer, and then pass that ID into a collision function.

Create Event

tilemap = layer_tilemap_get_id("TileLayer");

Step Event

if (!place_meeting(x + 4, y, tilemap))
{
    x += 4;
}

You can combine objects and Tile Maps when passing an array to a collision function.

Keep in mind:

Bounding Boxes

Some instances will use a rectangular bounding box for checking collisions (unless a different mask shape is selected).

These bounding boxes use an inclusive system, i.e. they include the bottom-most and right-most edges. This means that the bounding box for a 16x16 collision mask is generated from (0.0, 0.0) to (16.0, 16.0) (relative to the instance).

NOTE You can get these values using the bbox_leftbbox_top, bbox_right and bbox_bottom variables.

NOTE Since the bounding box is inclusive (as explained above), it will always extend 1 pixel beyond its bottom-right corner compared to the sprite's bounding box. For example, a 16x16 square sprite's right edge offset will be 15.0 (the 16th pixel when starting from 0), however an instance using this sprite will give you a bbox_right of 16 (+ its X position).

For two instances to be in collision, their bounding boxes have to overlap. At a pixel level, an overlap is counted when the centre of that pixel is covered.

For example, if you're trying to collide with a bounding box covering the area from (0.0, 0.0) to (16.0, 16.0), the edge of your mask has to touch the area between  (0.5, 0.5) and (15.5, 15.5) for a collision to be counted.

The functions collision_point and collision_line are exceptions, where they don't have to cover the pixel centre but can be checked anywhere inside the pixel. In the above example, a point check at (15.99, 15.99) would return true, but (16.0, 16.0) would not.

The following functions deal with the various ways in which you can have an instance check for collisions with another instance:

Advanced Collision Checking

The following functions can be used for this (as well as other functions detailed in the sections relating to Moving Around and Instances). These collision functions all work slightly differently but they maintain three common arguments which we will explain here:

The basic functions will return either the ID of an instance found to be colliding, or the special keyword noone when there is no collision, while the list functions will return the number of instances that are in collision and populate a pre-made DS list with their IDs. Note that if there are multiple collisions with the areas defined by these functions and instances of the given object, only one instance ID is returned, and it can be any one of the instances in the collision.

The following functions exist that deal with advanced collisions.

Collision Checking Without A Mask

All of the above functions are related to collision checking instances, and as such rely on the collision mask that is defined for the instance. However, there are many moments when you require to check for "collisions" with a point or an area, especially when your instance does not have a sprite assigned, or when you are working with the mouse etc... Therefore GameMaker also provides the following functions to help you in these situations:

Physics Collisions

When using the built-in physics, the above collision functions are not guaranteed to work for physics enabled instances. This is mainly due to the fact that these instances no longer use the majority of the regular built in variables (instead, physics enabled instances have their own set of variables) and neither do they use the collision mask or bounding box, as they use fixtures instead. However there is a special function for testing collisions with physics enabled that checks for overlapping fixtures: