3D_for_GML:_Z_detection - hpgDesigns/hpgdesigns-dev.io GitHub Wiki

4.1. Introduction

What you will learn:

  • Detecting z values
  • About collision detection

4.2. Detecting z values

In the tutorial ‘Z values’ we have seen that there are three values to determine the location of an object: x, y and z. If you’re familiar with 2D functions of GM, you will know that there is a built-in collision checking system. There is no such system for the z value. Height or depth or whatever you want to call it can not be detected like the collision of 2D sprites can.

Or can it? There are several ways to detect z values. The z value is not even a built-in variable to begin with, so things like collision checking are not possible from the outset. But, as we have seen earlier, the z value can simply be declared. If it has been declared, it exists and can be checked.

If you take a look at the example gm6 file that came with the tutorial, you will see that you can check the z value and perform events depending on what the z value is. In the example gm6, you will see that things change as the z value drops below the water level. Z detection is used interactively in the 3D world. There are a million and one ways to use z detection creatively.

4.3. About collision detection

After the declaration of the z value, an object is located in three-dimensional space located at position (x,y,z). This means you can change the object’s position in space, check for collision with other objects and so on. You can check collisions by using the built-in system, but you can also use the following additional functions:

collision_point(x, y, obj, prec, notme)
collision_rectangle(x1, y1, x2, y2, obj, prec, notme)
collision_circle(xc, yc, radius, obj, prec, notme)
collision_ellipse(x1,y1, x2, y2, obj, prec, notme)
collision_line(x1, y1, x2, y2, obj, prec, notme)

We will get back to collision checking in a future tutorial. Z detection is needed to find out where an object is in 3D space. Detecting the z value is essential if you want to create an object that exist in 3D space, like for instance a character that can walk, jump, climb and so on.