YoYo Games Wiki

3D Transformations

From YoYoGames Wiki

Portions of content from this page are originally found in issue 8 of GMTech Magazine, written by rup13.

One thing I’ve encountered which is difficult to get the hang of is 3D transformations. Yes these are things such as rotations, translations and scales. To rotate a simple cube, you have to understand a lot more than just knowing what function to use. You have to set the identity, translate the cube to where you want the object’s origin to be, add your rotation and then draw what you want to rotate, and end it all by setting the identity again. Confused? I certainly was at first.

One thing I couldn’t understand is why Game Maker couldn’t handle transformations different, specifically with few functions. Take a look at my example for rotating a ball forwards:

   d3d_transform_set_identity();
   d3d_transform_add_rotation_x(i);
   i+=5;
   d3d_transform_add_translation(x,y,0);
   d3d_draw_ellipsoid(-4,-4,-4,4,4,4, background_get_texture(tex_cube) ,1,1,64);
   d3d_transform_set_identity();

The following example rotates the ball by adding i to the angle each step. Even though once you’ve learn how to do this, it can get very confusing when adding in multiple rotations. Game Maker won’t let you have this code anywhere other than around where the function is for drawing the rotated object. This means if your object is planned to have multiple rotations, you must plan a system out for enabling and disabling rotations to make sure that the right one is shown according to your actions in the game. To overcome this, I wrote a script which is fairly simple to try and bypass some of these problems. My system works that you use variables for your objects, so in the Create event my ball variable consisted of this:

Create event:

   ball='d3d_draw_ellipsoid(-4,-4,-4,4,4,4,background_get_texture(tex_cube), 1,1,64)';

Now my ellipsoid is stored in my variable appropriately called ball. Now all I do is add my script to the Draw event, calling upon the ball variable as one of the arguments:

d3d_add_rotation:

   d3d_transform_set_identity();
   d3d_transform_add_rotation_axis(argument4,argument5,argument6,argument7);
   d3d_transform_add_translation(argument0,argument1,argument2);
   execute_string(argument3);
   d3d_transform_set_identity();

Draw event:

   d3d_add_rotation(x,y,0,ball,x,,,i);
   i+=5;

There you go, simple rotations using only one script. The format of the d3d_add_rotation() script is as followed:

d3d_add_rotation(xorigin,yorigin,zorigin,objvar,xaxis,yaxis,zaxis,angle);