|
Author
|
||||
|
Related articles
|
||||
|
More in Development
|
||||
In Part 1, we learned how to set up a basic camera, and draw the 3D primitives that Game Maker provides functions for. If you took my advice and messed around on your own a bit, you'll have noticed a few limitations to included scripts. For instance, how do you go about rotating primitives? There are no arguments for that in the functions. What if you want to scale primitives in interesting ways, beyond simply changing the size via the draw functions? Well, in this instalment, I'll be answering these questions using a single concept – transformations.
Transformations?
Yes. Transformations.
Transformations are mathematical operations performed on meshes that change their scaling, rotation and positioning. As always, Game Maker provides an easy set of transformation functions that allow you to do them with only a few commands. They're a little more involved in their implementation, but worry not, it's not too complicated.
In this demonstration, we're going to draw a vertical plane, and spin it around on the spot. Firstly, load up the code we wrote last time and create a new object: "Wall". In its Create event, place the following code:
faceDirection=0;
Then, the following in its Step event:
faceDirection+=2;
Those two bits of code will define the angle by which the plane will be rotated, and increment it each frame. Now we get to the Draw event, where the magic happens:
//Set colour to white so we can see this thing. draw_set_color(c_white); //disable backface culling. This is to stop our plane from vanishing when it faces // away from the camera. d3d_set_culling(false); //reset transformation matrix d3d_transform_set_identity(); //rotate about the z axis (this function accepts angles in degrees). d3d_transform_add_rotation_z(faceDirection); //then shift the model to the coordinates we placed it. d3d_transform_add_translation(x,y,0); //draw the wall d3d_draw_wall(0,-10,-10,0,10,10,-1,1,1); //reset transformation matrix d3d_transform_set_identity(); //re-enable culling d3d_set_culling(true);
So what does all that messy code do, exactly? The first thing we do is invoke the d3d_transform_set_identity() function. This resets any transformations we may already have done. If this command isn't used, any other transformations you've already done before this step will carry over into this Draw event. This means that these transformations and any other transformations you've already done to other objects will add up, and place the object in the wrong place, at the wrong angle. I'm sure you can appreciate why this would be a bad thing.
Next, we turn off backface culling. Backface culling stops the renderer from drawing any faces that face away from the camera, to save on rendering time. In this case, we want both sides to be drawn, otherwise our plane will disappear when it spins to face away from the camera, only reappearing again when it spins back to the correct angle. Try removing this code once the code is up and running to see the difference.
The next instruction tells GM to rotate the plane around the z-axis (vertical). You can rotate meshes around the x and y axes too using their respective commands – you can find them in the almighty GM help file. The third command tells GM to shift the rotated mesh by x and y units so that it's displaced to the coordinates of the object. Finally, we specify the mesh we want to draw, but instead of drawing it around its x and y coordinates like we did before, we draw it relative to the origin (coordinates 0,0,0). Finally, we reset the transformation matrix again.
|
Words from the readers
|
||||
|
No comments posted for this article yet. Have something to say? Make yourself heard below.
Have your say:
|
||||