<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript" language="JavaScript">
function reDo() {
if (innerWidth != origWidth || innerHeight != origHeight)
location.reload();
}
if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
origWidth = innerWidth;
origHeight = innerHeight;
onresize = reDo;
}
onerror = null;
</script>
<style type="text/css">
< !-- div.WebHelpPopupMenu {
position: absolute;
left: 0px;
top: 0px;
z-index: 4;
visibility: hidden;
}
p.WebHelpNavBar {
text-align: right;
}
-->
</style>
<script type="text/javascript">
gRootRelPath = "../../../..";
gCommonRootRelPath = "../../../..";
gTopicId = "9.2.4.3";
<script type="text/javascript" src="../../../../template/scripts/rh.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/common.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/topic.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/topicwidgets.min.js"></script>
<script type="text/javascript" src="../../../../whxdata/projectsettings.js"></script>
<link rel="stylesheet" type="text/css" href="../../../../template/styles/topic.min.css"/>
<link rel="stylesheet" type="text/css" href="../../../../template/Charcoal_Grey/topicheader.css"/>
<meta name="topic-status" content="Draft"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Collisions</title>
<meta name="generator" content="Adobe RoboHelp 2019"/>
<link rel="stylesheet" href="../../../../assets/css/default.css" type="text/css"/>
<meta name="rh-authors" content="Mark Alexander"/>
<meta name="topic-comment" content="Reference section for Collision functions and variables"/>
<meta name="rh-index-keywords" content=""/>
<meta name="search-keywords" content="collisions,collision functions"/>
</head>
<body>
<div class="topic-header rh-hide" id="rh-topic-header" onclick="rh._.goToFullLayout()">
<div class="logo">
</div>
<div class="nav">
<div class="title" title="Collisions">
<span>Collisions</span>
</div>
<div class="gotohome" title="Click here to see this page in full context">
<span>Click here to see this page in full context</span>
</div>
</div>
</div>
<div class="topic-header-shadow rh-hide" id="rh-topic-header-shadow"></div>
<h1>Collisions</h1>
<p>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 for the job is the most important task
of all. 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.</p>
<p class="note"><b>IMPORTANT!</b> All collision functions work using integer positions, regardless of the input values. Any non-integer values will be rounded to the nearest integer.</p>
<p> </p>
<h2><label for="one">Simple Collision Checking</label></h2>
<p>The following functions deal with the various ways in which you can have an instance check for collisions with another instance:</p>
<p> </p>
<ul class="colour">
<li><a href="place_empty.htm">place_empty</a></li>
<li><a href="place_free.htm">place_free</a></li>
<li><a href="place_meeting.htm">place_meeting</a></li>
<li><a href="position_empty.htm">position_empty</a></li>
<li><a href="position_meeting.htm">position_meeting</a></li>
<li><a href="position_change.htm">position_change</a></li>
<li><a href="position_destroy.htm">position_destroy</a></li>
<li><a href="../../Asset_Management/Instances/instance_place.htm">instance_place</a></li>
<li><a href="../../Asset_Management/Instances/instance_place_list.htm">instance_place_list</a></li>
<li><a href="../../Asset_Management/Instances/instance_position.htm">instance_position</a></li>
<li><a href="../../Asset_Management/Instances/instance_position_list.htm">instance_position_list</a></li>
</ul>
<p> </p>
<h2>Advanced Collision Checking</h2>
<p>The following routines can be used for this (as well as other functions detailed in the sections relating to <b>Moving Around</b> and <b>Instances</b>). These collision functions all work slightly differently but they maintain three common arguments
which we will explain here:</p>
<ul class="colour">
<li><b>obj</b>. 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 <b>all</b>. It is also worth noting that if the object type chosen is the designated
<b>parent</b> of other objects, then all <i>children</i> of that object will be included in this check too.<b></b></li>
<li><b>prec</b>. This can be either <tt>true</tt> or <tt>false</tt> and relates to the precision of the collision checking. If set to <tt>true</tt>, 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 <tt>false</tt> then collisions will be calculated based on the bounding box of the object to be checked
for.<b></b></li>
<li><b>notme</b>. 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 <tt>true</tt>. If you are specifically looking
for the instance of the object running the code, or it's not important, it can be left as <tt>false</tt>.</li>
</ul>
<p>The basic functions will return either the ID of an instance found to be colliding, or the special keyword <tt>noone</tt> when there is no collision, while the <i>list</i> 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 <i>one</i> instance ID is returned, and it can be <i>any one of the instances in the collision</i>.</p>
<p>The following functions exist that deal with advanced collisions.</p>
<p> </p>
<ul class="colour">
<li><a href="collision_circle.htm">collision_circle</a></li>
<li><a href="collision_circle_list.htm">collision_circle_list</a></li>
<li><a href="collision_ellipse.htm">collision_ellipse</a></li>
<li><a href="collision_ellipse_list.htm">collision_ellipse_list</a></li>
<li><a href="collision_line.htm">collision_line</a></li>
<li><a href="collision_line_list.htm">collision_line_list</a></li>
<li><a href="collision_point.htm">collision_point</a></li>
<li><a href="collision_point_list.htm">collision_point_list</a></li>
<li><a href="collision_rectangle.htm">collision_rectangle</a></li>
<li><a href="collision_rectangle_list.htm">collision_rectangle_list</a></li>
</ul>
<p> </p>
<h2>Collision Checking Without A Mask</h2>
<p>All of the above functions are related to collision checking <i>instances</i>, 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:</p>
<p> </p>
<ul class="colour">
<li><a href="point_in_rectangle.htm">point_in_rectangle</a></li>
<li><a href="point_in_triangle.htm">point_in_triangle</a></li>
<li><a href="point_in_circle.htm">point_in_circle</a></li>
<li><a href="rectangle_in_rectangle.htm">rectangle_in_rectangle</a></li>
<li><a href="rectangle_in_triangle.htm">rectangle_in_triangle</a></li>
<li><a href="rectangle_in_circle.htm">rectangle_in_circle</a></li>
</ul>
<p> </p>
<h2>Physics Collisions</h2>
<p>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 <a href="../../Physics/Physics_Variables/Physics_Variables.htm">own set of variables</a>) and neither do they use the collision mask or bounding box, as they use <a href="../../Physics/Fixtures/Fixtures.htm">fixtures</a> instead. However there is a special function for testing collisions with physics enabled that checks for overlapping fixtures:</p>
<p> </p>
<ul class="colour">
<li><a href="../../Physics/physics_test_overlap.htm">physics_test_overlap</a></li>
</ul>
<p> </p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
<div style="float:left">Back: <a href="../Movement_And_Collisions.htm">Movement And Collisions</a></div>
<div style="float:right">Next: <a href="../Movement/Movement.htm">Movement</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
</body></html>