当你计划使用特定动作时，你需要特别注意物体是否与世界中其他物体有碰撞。为物体设定正确的碰撞通常是很重要的。（这句话的意思是，主角被棍棒打中和被汽车撞到的效果可能是不一样的，你需要在设计时提前考虑好）。GameMaker Studio 2 has a number of built in functions to help you deal with collisions correctly and in accordance with the needs of your project.
The following functions deal with the various ways in which you can have an instance check for collisions with another instance:
The following routines 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:
- obj. This is where we specify the object that the function has to check for a collision with. It can be an instance id, an object type, or the special GM keyword all. It is also worth noting that if the object type chosen is the designated parent of other objects, then all children of that object will be included in this check too.
- prec. This can be either true or false and relates to the precision of the collision checking. If set to true, then precise collisions are to be calculated (but only if the object being checked for has "precise" marked for its collision detection in its mask properties) which is a slightly more complex task for the CPU and so should be used with caution. If false then collisions will be calculated based on the bounding box of the object to be checked for.
- notme. You may need to check for collisions with an object that has the same index as the object that runs the code, so to prevent the code finding a collision with itself, you can set this to true. If you are specifically looking for the instance of the object running the code, or it's not important, it can be left as false.
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.
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 Studio 2 also provides the following functions to help you in these situations:
When using the build 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: