Skip to content

3D_for_GML:_Specific_use_of_sine

hugh greene edited this page Jun 20, 2022 · 1 revision

1.1. Introduction

What you will learn:

  • Moving the camera in a direction
  • Using sine to yaw
  • Using sine to pitch
  • Moving the camera up and down
  • Running the program

1.2. Moving the camera in a direction

In the previous tutorial about z values we saw how to point a camera at an object. Maybe you recognise the following code from that tutorial? Here it is:

//draw what the camera sees
d3d_set_projection(0, 0, 0, obj_ball.x, obj_ball.y, obj_ball.z, 0, 0, 1);

This works if the camera is facing an object (like a ball). If the camera has to move freely without pointing at anything in particular, there is no object to point it at. If the camera has to glide through space, passing all kinds of objects without pointing at just one, we will need to use another type of code, something like this:

//draw what the camera sees
d3d_set_projection(x, y, z, xt, yt, zt, 0, 0, 1);

The first three values (x,y,z) define the point the camera looks from, as you will probably know by now. The last three values determine what side is up. But what about those values xt, yt and zt?

1.3. Using sine to yaw

This is where sine (and its counterpart: cosine) comes in. To get the projection right when moving through the room we will have to use the following code:

xt = x + cos(direction*pi/180);
yt = y - sin(direction*pi/180);

You don’t have to understand why this code is needed. But you will probably see that there is no target object needed at which to point the camera. The camera will be pointed in the direction it is facing or moving. That can be very useful in games. In many games you will want the camera to see in the direction it is moving in, like, for example, in a first person shooter. With the xt and yt value determined, this leaves us to write code for the zt value. The xt and yt value cover the looking left and right (also known as yaw), but not up and down!

1.4. Using sine to pitch

Not only will we need to declare the z, like we did in previous examples, we will also need to point the camera up or down. For this, we will use the following code:

//declare z value
z = 0;
zdirection = 0;

The zdirection will be set to zero to begin with which means neither up nor down (level). Guess what? This is where sine comes in again:

zt = z + sin(zdirection*pi/180);

The up and down direction (also known as pitch) is calculated based on the zdirection using the sine function. We will now have determined xt, yt and zt, so that takes care of direction. We can make the camera move in the zdirection it is facing. Just read on.

1.5. Moving the camera up and down

If we want the camera to move in the zdirection, we will need to change the z value (its height effectively) which will be done by the following code:

//moving in zdirection
z+= zdirection*speed/128

The value 128 is used to decrease the motion a bit. (Check out the gm6 file that is included with this text to see how and why it works.) As you can see from the code, the z is changed based on zdirection and speed. To make the camera’s pitch return to level automatically, we will have the zdirection wear down over time, using the following code:

//wear down zdirection
zdirection*=0.98

The zdirection is decreased with each Step so that it becomes zero (level) in the end.

1.6. Running the program

Although there is much more to learn about the specific use of sine in 3D, like creating circular or wavy motion, this is all we will talk about for now. Be sure to run and check out the gm6 file that came with this text to see how sine was used to create the effect of flying through space. (Use the arrow keys to move).

Clone this wiki locally