3D for GML: Collisions

From ENIGMA
Revision as of 22:21, 14 November 2013 by X (talk | contribs) (Fixed numbering)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

10.1 Using collisions in 2D

Collisions are a vital part of almost any game. You probably have used collision detection in a 2D Game Maker game. If you think of a 2D game, you will probably know that the built-in collision detection of GM is very useful. You can make a ball bounce off a wall, for instance. But what happens when there are three dimensions in stead of two? Let’s take a look at the gm6 that comes with this tutorial. If you run it, you will see a ball object that bounces off walls. If you study the editable file, you will find that the standard method of collision checking and bouncing off solid objects was used. The wall object (chi_block) is a solid object that the ball object bounces off. That should not surprise you too much.

10.2 Using three collision methods in 3D

Now, let’s look at the gates. When the ball object (obj_ball) collides with the gate object (chi_gate), what happens? Well, the script scr_ball which holds the following code, is executed:

//squash ball?
if collision_circle(x, y, 8, chi_gate, false, false) && other.z<32 then
{game_restart ();}
else if collision_circle(x, y, 16, chi_gate, false, false) && other.z<32 then
{
	direction = point_direction(other.x, other.y, x, y);
	speed = 12;
}

This script does two things: first, it checks to see if the ball is squashed and if so, restarts the game. And second, it checks to see if the ball is almost squashed but not entirely and if so, it send the ball in a direction away from the crushing gates. The gate object is not solid, by the way. This means that the ball can pass through the gate, even when the crusher is all the way down to the ground. That is where script scr_ball comes in. As you can see from this gm6 file, there are three collision detection methods at work. The drag-and-drop method, the collision_circle method and the z value method. You can use all of these collision detection systems when you are creating games in 3D. It depends on your game which methods you will use for which part of your program. Maybe the gm6 file of this tutorial doesn’t show the most efficient way to use these methods, but it serves its purpose of demonstrating which tools are available. Combining the three methods of collision detection means you can detect practically any collision in 3D.

10.3 Creating games using collision detection

I’ve been using collision detection in previous tutorials, so you can go back and learn from those and see how the methods have been used:

  • For more information about other functions like collision_line, see Z Detection.
  • For more information about creating a 3D platform game using collision detection, see Platforms.
  • For more information about creating an elevator using collision detection, see Elevators.
  • For more information about creating ramps or stairs using collision detection, see Stairs.




All rights reserved. Copyright © 2004 by John J.A.H. Weeren. Unauthorized use or reproduction, whole or in part, without written permission from the author is strictly prohibited. This page is part of the tutorial 3D For GML. Reproduced with permission. For more information, please see the talk page.