3D_for_GML:_Locations_&_dimensions - hpgDesigns/hpgdesigns-dev.io GitHub Wiki
1.1 Understanding 3D locations
The Room editor window of Game Maker only shows twodimensions: x and y. Three dimensional games need three dimensions: x, yand z. When placing an object in a room, only two dimensions will be used to determine its location in space. The third dimension is not built into GM as such. GM is a 2D game creation program that enables the user to program using 3D functions (the D3D functions). The x-dimension is shown as horizontal lines in GM’s Room editor window, the-y dimension as vertical lines. This results in a x-y grid. There is no mention of a zdimension. Z is not a built-in variable, like x andy. If you want to use a variable to store values, you will need to declare this variable yourself. If you want to use x, y and z, you will have to declare the z-value yourself. For all built-in x- and y-variables and functions you will have to create equivalents for z. For example, gravity and gravity_direction are built into GM but they exist in the x-y-plane only.
1.2 Understanding dimensions
Each location in the 3D world is determined by its x, y and z. The 3D functions of GM use x, y and z to determine where to draw things. For instance, to draw a block in 3D, you would use this function:
d3d_draw_block(x1, y1, z1, x2, y2, z2, texid, hrepeat, vrepeat)
Dimensions (or: measures) of a 3D block are determined by two points: x1, y1, z1 and x2, y2 and z2. These are the outer points (extreme corners) of the block. By changing the two pairs of x-y-z values you can create cubes, blocks, slabs and so on. Let’s look at another code that is used to drawround 3D shapes:
d3d_draw_ellipsoid(x1, y1, z1, x2, y2, z2, texid, hrepeat, vrepeat, steps)
Again, you will see the two pairs of xyz values, determining the invisible box inside which the ellipsoid (round shape) will fit.
1.3 Going from 2D to 3D
Let’s imagine a 2D game with several objects in a room. Let’s say it’s a 2D space shooter in which UFOs move around the room. The player has a space ship that stays in the center, spinning around its axis, firing a laser cannon in all directions. There would be objects at several locations, each with their own x and y value in the room. When a UFO moves, its x and y value changes. Now, let’s imagine the same game in 3D. We would have several objects in a 2D room, but those objects would draw shapes at a 3D location. There would not be any 3D objects in the Room editor window because GMonly knows 2D objects in there. There would only be 2D objects, drawing 3D shapes, you see? There will be 2D objects performing 3D functions. Get it? In a typical 2D game, the sprite of an object is drawn at its 2d location. In a typical 3D game, you can have objects at one location doing the 3D drawing at another location. Understanding this distinction isthe important thing. The location of an object in a 3D game is not necessarily the same as the location or dimension of the 3D shape it draws. For example, you can have an object at location 0,0,0 which performs the following code:
d3d_draw_floor(-400, -300, -100, 100, 200, 300, texid, hrepeat, vrepeat);
The location and dimensions of the object that does the drawingare completely different from the object that is drawn. This is where the power and possibilities of 3D lie. Simple 2D objects doing complicated 3D things.
1.4 Moving around in 3D
If we take a look at the gm6 file that comes with this tutorial, we will see two types of object in the Room editor: a camera objectand a block object. The camera object’s x-y-location can be changed by pressing the WASD keys. Its zlocation can be changed by pressing the Up and Downarrow keys. The camera always looks along the x axis, or in other words: it always faces east in the Room editor. In the room many block objects have been placed. The z-position of each block is set in its initialization script (init_block):
z = random(512);
The x-y-location of the block in the room is determined by its placement in the Room editor while its z-location is set in code (script init_block). When we run the gm6 file, we will have a camera that can move in the x-y-plane and along the z-axis by pressing keys. We will see blocks that are placed in a grid with each block at a different z-location. All blocks have the same dimensions, set in their draw event:
d3d_draw_block(x-16, y-16, z-16, x+16, y+16, z+16, background_get_texture(bk_block),1,1);
The block is drawn around its center (location: x, y, z). Try moving around the room while imagining where the camera would be in the Room editor window. In other words, try to picture the position the cameraobject has in 2D. Now, do the same for some of the 3D block objects you see: where would each block be in the 2D Room editor? You see, a 2D object in a 2D room can perform 3D functions in a virtual 3D space.