The camera from the first person

Xtreme3D

Lesson 4
The camera from the first person

Level: Beginner
Version of the Xtreme3D: 3.0.x
Author: Gecko

In Lesson 3 we considered the simplest version of the camera from the first person - it was managed by the arrow. In the meantime, the absolute most modern games in this case management is used with the mouse. Let's look at how to implement his means Xtreme3D.

To begin with, we create the parent of the dummy for the camera - camPos. We will be moving, not the camera as well.

CamPos = DummycubeCreate(global.scene);
ObjectSetPosition(camPos, 0, 2, 0);
Camera = CameraCreate(camPos);
ViewerSetCamera(view1, the camera);

The fact is that the camera should only move in the xz plane - in other words, should not "fly" through the air. We will rotate the object of camPos in the Y-axis, when the user will unseat the mouse horizontally - thus, it will be possible to manage the direction of movement. Click on the offset in the vertical direction will cause the local turn the camera object on the X-axis - this way, the user will be able to look up and down, but this does not affect the direction of movement, because the camera inherits the movement from camPos.

Declare the following variables:

CenterX = display_get_width()/2;
CenterY = display_get_height()/2;

It is the coordinates of the center of the screen. We will read the offset click on this point and then return it to the cursor.

You can also immediately put the cursor in the center of the screen, to the beginning of the game camera watched strictly forward:

Display_mouse_set(centerX and centerY);

Now go to the event Step. The following code calculates the offset of mouse cursor on the center of the screen and turns the camPos and camera on the corners, deltaX and deltaY:

DeltaX = (centerX - display_mouse_get_x()) / 3;
DeltaY = (centerY - display_mouse_get_y()) / 3;
ObjectRotate(camera, DeltaY, 0, 0).
ObjectRotate(camPos, 0, -deltaX, 0);
Display_mouse_set(centerX and centerY);

Left to realize the movement. We will use the standard for games from the first person the layout of the WASD:

Dt = 1.0 / room_speed;
If the keyboard_check(ord('W') ObjectMove(camPos, -10 * dt);
If the keyboard_check(ord('A') ObjectStrafe(camPos, 10 * dt);
If the keyboard_check(ord('D') ObjectStrafe(camPos, -10 * dt);
If the keyboard_check(ord('S') ObjectMove(camPos, 10 * dt);

The meaning of the multiplying by dt in the following. If moving objects with a fixed speed, their actual speed will be tied to human frequency of application. That is, for example, if we move the selected object 10 units for the frame, the speed at 60 FPS will be equal to 10 * 60 = 600 units per second. At 120 FPS, respectively - 10 * 120 = 1200. In the end, the object will move faster or slower, depending on the FPS. This is not what we need, so you need to set the speed of the other values, and not attached to frame. For example, in units per second. Consequently, the manpower speed will be equal to the V / FPS, where V is the speed. We simply explore how an object should move in one shot, if the second it moves to the V units. Thus, the object will move with the correct speed when any personnel frequency.

Not to clutter the code ticks (division, as it is known, relatively slow operation), we instead, multiply the speed of 1 / FPS - This value can be calculated only once. It is also referred to as the step time (this step time should refer to the function of the Update, as mentioned in lesson 2). In the Game Maker 8 hr speed (FPS) usually is fixed and is set in the settings of the room (Room speed). It can be set equal to 60 or 120.