3D for GML: Z values

From ENIGMA
Revision as of 23:17, 14 November 2013 by X (talk | contribs) (→‎2.4 Declaring the z value: Fixed typos)
Jump to navigation Jump to search


2.1 Relative drawing of objects

This tutorial builds on the previous tutorial, so check out that one first. Like in the previous example we will create something to see in 3D. This time we won’t create a block but again, we will create a basic shape: a ball. Assuming you have understood what was shown in the previous tutorial, let’s create a room (rm_tutorial), a background (bk_ball), an object (obj_ball) and a script (scr_ball), containing the code that draws the ball which looks like this:

//draw the bubble
d3d_draw_ellipsoid(x-16, y-16, z-16, x+16, y+16, z+16, background_get_texture(bk_ball), 1, 1, 16);

An ellipsoid (or sphere, bubble or ball) will be drawn at a position relative (x, y, z). We will find out what the z value does by experimenting with it. The ball will be drawn relative to the center. Its center will be located at position (x,y,z) but we will draw it: from one side (x-16, y-16, z-16) to the other side (x+16, y+16, y+16). Drawing the ball relative to its x, y and z value will mean we can change its position (and other things). If you are familiar with 2D games in Game maker you will know what x and y values are. The z value is just another value like x and y, only in a different direction. You will find out what it does by playing around with it. Later on, we will change the x, y and z values and see what results we will get.

2.2 Pointing a camera at an object

Let’s have the object (obj_ball) execute the script (scr_ball). We will have the script executed in the Draw event of the ball object. Like in the previous example we’ll need a camera to view our object 'ball'. Let’s create a script for this, named (scr_camera) that holds the following code:

//draw what the camera sees
d3d_set_projection(0, 0, 0, obj_ball.x, obj_ball.y, obj_ball.z, 0, 0, 1);

As you can see, the camera will be placed at position (0, 0, 0) and it will look in the direction of our object 'ball' (obj_ball.x, obj_ball.y, obj_ball.z). It will point at the center of the 'ball' object.

2.3 Exporting and importing scripts

We will need an object that makes our camera script run, just like in the previous example. So, we’ll create a camera object (obj_camera). In its Draw event we will have our camera script (scr_camera) run. This should sound familiar by now. Okay that’s one script added. In the Create event of the camera object we will put the exact same script we used in the previous example that initializes the 3D mode, remember?

We can do this by opening our previous tutorial, opening the Scripts folder, open the right script (scr_start3D), select the code text, copy it and paste it back into our new example. But there is a better way! Open the gm6 of the previous example and select the script (scr_start3D) by clicking on it. Now, if we right-click it, we can select ‘Export Selected Script …’. This way we can save the script anywhere we want, to open it later on. Okay, we will do that. I suggest saving under the file name ‘scr_start3D’.

Let’s return to the example we are working on and import the camera script we just exported from the previous example. Simply select ‘Import Scripts’ from the Scripts menu (in GM’s main view) and select the script that you saved as ‘scr_start3D’. The script will have shown up in the Scripts folder, ready to use in our current program.

2.4 Declaring the z value

Just like in the previous example, we will add another script named ‘scr_make’, containing this code:

//create ball object
instance_create(64, 64, obj_ball);

This code will be executed right after the initialization script (scr_start3D). This will mean our camera object will create a ball object for us. If we place a camera object in the Room (in the Room editor window) and run the program, we will get an error saying that there is an unknown variable z. This is because GM is intended for 2D games and z is not a built-in variable. In 2D games we only need x and y values, in 3D we need a third value: z. 2D means two values, 3D means three. makes perfect sense. We will have to create the z value because it is unknown. Let’s create a script for it so we can use it for future objects. We will create a script named scr_declare_z that will be put in the ball object’s Creation event. This will hold the following code to declare the z-value:

//declare z value
z = 0;

If we run the script now, it works perfectly and we can see a big blue ball in the center of the screen.

2.5 Adding motion to the z value

Let’s add a last script called ‘scr_motion’ which uses an argument0 that holds the amount of motion (or speed). It should look like this:

//apply motion
z += argument0;

This means we can make the ball move along the z axis. Let’s make this script (scr_motion) run in the ball object’s Step event so that the ballkeeps moving (away from the camera) We will write this piece of code and run it in the Step event. We will not make a separate script for this, just this piece of code:

//apply motion
script_execute(scr_motion, 1);

The motion script (scr_motion) will now be executed with a motion amount (speed) of 1, as you probably will have guessed. If you run the script, you will see that the ball gradually disappears into the foggy distance.


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.