(Wiki Note: This article could use some images.)
In game development, a collision occurs when two
instances or shapes move to overlap each other
(even if it's just 1-pixel overlap). This concept is useful for
triggering a specific event. For instance, when a
player is falling, and then comes into contact with a platform, he
should stop. If he comes into contact with an enemy, he should either
take damage or damage the enemy. To detect when something comes into
contact with something else, you would use a collision event.
The exact specifics of when a collision occurs depend on the Mask
settings and Collision system selected, but generally, we can say that a
collision is deemed true when two things are overlapping or intersect by
any amount.
First, understand an event, and the difference
between an object and an
instance.
An object obj_0 will define an event with another object obj_1 (or
even the same object). When any instance of obj_0 comes into contact
with any instance of obj_1, the event is fired inside of that instance
of obj_0. If multiple instances are involved in collisions in one
step, they will all fire collision events. If obj_1 also has a
collision event with obj_0, then both collision events will fire. If
you choose a collision event with the same object, then whenever any two
instances of that object collide, both of them will fire their collision
event.
A collision event will continue to occur every step
for as long as the two objects continue to overlap. Because of this, in
order to avoid repeatedly executing code (like constantly taking
damage), it can be wise to, when a collision occurs, do something to
make the collision stop happening. A few suggestions;
For your convenience, within a collision event, the
other variable can be used to refer to the other
object in the collision. For DND users, many
actions have an Applies to section at the top, with other listed
as one of the options. Other can be used to execute some piece of
code or action on the other object. For instance, collecting coins could
be done as follows:
In obj_player's collision event with obj_coin:
Set the score relative to 1
For other object: Destroy the instance.
The fundamental formulas for collision detection are rather complex,
usually involving geometric polygon intersection algorithms, and even
pixel-overlap algorithms for pixel-perfect collision. Fortunately,
ENIGMA comes packed with several Collision
Systems built in, and a default selected,
so all you need to do is add collision events to the instances that you
are interested in, and handle the event however you like. The selected
collision system will decide when to trigger the event and execute the
actions inside of it.
Using all defaults can quickly make your game feel very clunky, so once
you are familiar with the basics of collisions, you can then move on to
fine tuning the system. The sections below will provide you with the
different levels of tuning your collisions.
The region of an object that determines which parts are "solid" and
which parts are "air" is defined by the Mask. In the simplest sense, you
can think of a mask as a
silhouette of your object's
sprite. The dark regions are solid, and the light regions are air. A
collision occurs when the solid (dark) region of one object comes into
contact with the solid region of another object.
The mask of an object is determined by selecting a
sprite with the desired mask properties. By default, an object's mask is
set equal to its Sprite. To alter properties for
the object's mask, you can either alter the mask properties of the
sprite, or select a separate mask for that object. Note that the mask is
not visible in the game - only the sprite is drawn. The mask is only
used to determine collisions.
The mask can be read/set programmatically in-game via the
mask_index global
local variable, or with the appropriate DND
action.
The exact properties of a mask are determined by which Collision System
you have selected. Please consult the respective Collision System's
documentation, under their Mask section. Obviously, if one collision
system's mask properties are insufficient for your game, you can select
another collision system.
The selected collision system employs
the underlying formulas and algorithms to determine whether a collision
occurs. It determines the exact behavior of collision functions, as well
as the mask properties available. Since a collision event is determined
by collision functions, the collision system then also determines when
to trigger a collision event, based on the object's mask properties and
coordinates.
Different systems behave very differently, so choose carefully.
Generally, collision is a trade-off between efficiency (or calculation
speed) and precision (or how accurate the collision seems to be). We try
to select a default that is appropriate for the general case of most
games - that is, a nice balance between efficiency and precision.
To select a collision system, go to ENIGMA Settings > API >
Collisions, and select the desired system from the dropdown. Then click
the green checkmark to submit your changes.
The following systems are currently available for selection:
The following systems we hope to make available in the future:
(Wiki note: The above could probably be made into a table.)
If none of the collision systems provided are suitable for your game,
and you would like to handle your own collisions, consider one of the
following:
Wiki: Action
Wiki: Collision
Wiki: Collision_Detection
Wiki: Collision_Systems
Wiki: Collision_detection
Wiki: ENIGMA
Wiki: ENIGMA_compiler
Wiki: Event
Wiki: Other
Wiki: Sprite
Wiki: User:IsmAvatar