3D_for_GML:_First_person_mouselook - hpgDesigns/hpgdesigns-dev.io GitHub Wiki
1.1 Understanding first, second and third person
Basically, there are two types of views in games: first person and third person. Originally, these are grammatical terms used for describing the perspective in language or literature: first being “I”, second being “you” and third being “he/she/it”. In the world of game graphics, these terms are not used to describe the narrative perspective but the visual perspective (or type of view). Also, by the very nature of visual perspective the second person view is non-existent. You may have seen text-based adventure where it says something like: “You are in a desert-like landscape, strewn with large rocks”. This is a second person perspective in language. Any visual illustration of that description would be either first or third person.
This tutorial will show you how to create a first person view with mouselook. Mouselook is when the mouse can be used to have the character look aroundthe virtual 3D world. So, what does first person look like? Well, let’s find out. Basically, the gm6 file that comes with this tutorial uses four objects: world, character, camera and rock. Let’s take a look at what each one of these objects does in the program.
1.2 Creating a world
The object obj_world has one action in its Draw event, namely: execute the script draw_world, which draws the world using this code:
//DRAW WORLD
//draw ground
d3d_draw_floor(0, 8192, 0, 8192, 0, 0,
background_get_texture(bk_ground), 16, 16);
//draw sky
d3d_draw_floor(0, 8192, 128, 8192, 0, 128,
background_get_texture(bk_Sky), 1, 1);
The ground and sky are drawn using ground and sky textures.
1.3 Creating rocks
The rock object will have a child and parent. The chi_rock (child) has one action in its Create event, executing the script init_rock:
//INIT ROCK
//set z value
z=random(16)-16;
//set size
size=random(32)+32;
//set angles
angles=random(5)+3;
Here, the z value (or height in the landscape), its size (how large the rock will be) and the angles (how smooth or pointy the rock will be) are set. Note that in the Object properties the parent object par_rock is selected. The parent par_rock executes the script draw_rock in its Draw event:
//DRAW A ROCK
//draw
d3d_draw_ellipsoid(x-size, y-size, z-size, x+size, y+size, z+size, background_get_texture(bk_rock),1,1,angles);
It uses the set values to draw a rock.
1.4 Creating a character
We will create a character to walk the world that we have created. The initialization script scr_init_character will set the initial values:
//INIT CHARACTER
//set z value
z=0;
In fact, the character will be on the ground (z=0) but the camera will be a lot higher (as we will see later). In other words, the eyes will be higher than the feet which is usually the case in walking characters. Speaking of walking, we will use the keyboard to control the character’s motion, using the script mot_character (motion of the character):
//MOTION OF CHARACTER
//set friction
friction=0.2;
//walk
if keyboard_check(ord('W')) then
{motion_add(obj_camera.direction,0.2);friction=0;}
else if keyboard_check(ord('S')) then
{motion_add(obj_camera.direction+180,0.2 );friction=0;}
//strafe
if keyboard_check(ord('A')) then
{motion_add(obj_camera.direction+90,0.2);friction=0;}
else if keyboard_check(ord('D')) then
{motion_add(obj_camera.direction-90,0.2);friction=0;}
//limit speed
if abs(speed)>16 then speed*=0.98;
The friction is set so the character stops walking if we don’t press a key. Also, the speed will be limited so he (she?) won’t keep running faster and faster as we hold a key. The rest is just checking the keyboard and making sure we’re walking in the right direction.
1.5 Creating a camera
In the initialization script init_camera the usual things are set (see Getting Started With 3D). Also, some instances are created in these lines:
//create instances
instance_create(0,0,obj_character);
instance_create(0,0,obj_world);
repeat (64)
{instance_create(random(8192),random(8192),chi_rock);}
This will have the camera object create all the other objects, so to speak, in its Create event. In its Draw event the script draw_camera will be executed which holds the following code:
//DRAW WHAT CAMERA SEES
xf=obj_character.x; //x to look from
yf=obj_Char acter.y; //y to look from
zf=obj_character.z+64; //z to look from
xt=xf+cos(degto rad(direction)); //x to look to (with direction)
yt=yf-sin(degtorad (direction)); //y to look to (with direction)
zt=zf-sin(degto rad(zdirection)); // z to look to (with z direction)
d3d_set_projection(xf,yf,zf, xt,yt,zt, 0,0,1); //look from & to
As you can see, a value called zdirection is used (for the pitch or vertical angle of the camera). So, we now have a world, with rocks in it, a character that can walk and a camera that can see. Now, all we have to do is add the mouselook script:
‘scr_Mouselook’:
//MOUSELOOK
//get display dimensions
display_w=display_get_width();
display_h=display_get_height();
//calculate motion
change_x=(display_mouse_get_x()-display_w/2)/16;
change_y=(display_mouse_get_y()-display_h/2)/12;
//move cam
direction-=change_x
zdirection+=change_y
//limit the zdirection
if zdirection<- 90 then {zdirection=-90;}
else if zdirection>90 then {zdirection= 90;}
//set mouse back
display_mouse_set(display_w/2,display_h/2);
First, we get the display dimensions (of the monitor). Then we calculate how the mouse has moved. We use those changes to move the camera (direction for yaw and zdirection for pitch). The zdirection is limited to 90 degrees up and down (because of a sine function. Otherwise it would reverse if we looked too far up or down). In the last line, the mouse is set back to its original position so that we can check any changes the next time around.