# [LEARN] Yaw, Pitch, And some Math Lesson

Discussion in 'Off-Topic' started by Kenn Fatt, Dec 3, 2016.

1. ### Kenn FattSlime

Messages:
82
GitHub:
kennfatt
Hello everyone!

I have a plan to create this thread since 2 weeks ago, and now i'll explain what this thread for.

Okay, lets go to the main topic,
Maybe some of us (especially i'm) is not understand about "Yaw" and "Pitch" function on Minecraft (Inside Pocketmine, JavaScript, or other code related with Minecraft PE).

And, i've been readed some site about "yaw" and "pitch" for Minecraft PE (See this).
This is a code has been taken from that site,
Code:
```//Inspired by 500 ISE in his Fly on mobs.js
var blocks=5;
var x=Player.getX();
var z=Player.getZ();
var yaw=Player.getYaw();
var deltaX=Math.sin(yaw)*blocks;
var deltaZ=Math.cos(yaw)*blocks;
var block=getTile(Math.round(x+deltaX),Math.round(Player.getY()),Math.round(z+deltaZ)));
//Of course, you have to consider about pitch if you are thinking about a light beam. Method: multiply by Math.cos(Player.getPitch());
```
And then we have a question for this topic.
First, What is Yaw and Pitch? And what is their function for Minecraft?
Second, Is it trigonometry related with this?
Third, Can someone give an example with easiest way to understand.

So, Lets discuss it together! Thanks before.
(I hope SOF3 will explain it to us, or someone else who has much and good knowledge)

Messages:
1,968
GitHub:
sof3
https://en.wikipedia.org/wiki/Yaw_(rotation)
Entity rotation. Look at the images in the wikipedia link. Imagine the plane's nose as the player eyes.
Remember that "body yaw" and "yaw" are two different values client-side. A bit complicated server-side, but I don't think this is important since we don't really care about body yaw.
This can be better explained by "vector components".
A vector can be broken down into multiple components. Adding these component vectors up will result in the vector. If these components are all along/parallel to different axes in a dimension (in the world of vectors, "along" and "parallel to" have no difference), i.e. X-axis, Y-axis and Z-axis in the 3D world in Minecraft, they represent the direction vector of a player.
For example, imagine a player located at (0, 0, 0) looking at the point (1, 1, 0). He is looking 45 degrees up, so his pitch is -pi / 4 rad (we usually use radiains in programming, and in case you don't know, 1 radian = 180 / pi radian) (pitch increases from up to down, so looking at the horizontal is pitch = 0, looking at the sky is pitch = -pi / 2 rad, and looking at the ground is pi / 2 rad). He is looking directly towards the X-axis, so his yaw... I am not very sure about in which directions MCPE draws the X and Z axes (we usually use Z instead of Y as the vertical axis in vector mathematics). But you can imagine that the yaw increases by pi / 2 every time the player horizontally rotates 90 degrees.
A warning is that PocketMine does not have very clear documentation about the units of angles used in its code, so better check carefully before assuming anything. However trigonometric functions from PHP like sin() accept angles in radians, and inverse trigonometric functions in PHP return angles in radians too.
Another warning is that Minecraft/PocketMine sometimes stores yaw values beyond the range of 0-360 degrees (or -180 to 180 degrees), but you can round them back to the normal range using the % modulus operator.
So how are they related to trigonometry? You can easily find the answer when you search about it. Here I'm showing an excellent production from the legendary Microsoft Paint (hope they won't sue me for copyright) (I'm not sure if I mixed up X and Z):

In the figure, the green line is the original vector we are talking about. You can assume the origin (the intersection of XYZ) as the eye position of the player, and the other end of the green line as the point that the player is looking at. This line can be broken down into the two red lines (use some 3D imagination to imagine that if you connect the two end points of the red line and the end of the green line, you can form a right-angled triangle. Same with the other red line, which is located on the plane formed by X and Z. So here, the angle drawn by the purple brush is the angle from the horizontal plane to the player's direction vector, which is (the negative value of) the player's pitch. And then the horizontal red line can be further broken down into the two orange lines on X and Z, which each also form a right-angled triangle with the red line.
I have mentioned right-angled triangle multiple times, and if you know what trigonometric functions are for, you already know how they can be used. Here is a quick image I searched from Google:

The blue angle is the horizontal rotation. I am not very sure where Minecraft starts counting the yaw from, so I just drew a random angle, and I'm too lazy to check. Draw your own diagram if you need to check it against the trigonometric functions; don't refer to this blue angle.
How long is the green line? For convenience, we use the "unit vector", where the length is 1.
This direction vector for entities can actually be retrieved using the \$Entity->getDirectionVector() method.
So how do you use the unit vector you have got? You multiply it to test for things. For example, if you have a command that makes the player teleport to 100 meters forwards at the direction he is looking at, you can use some code like this:
PHP:
``` function teleportForward(Player \$player, float \$distance){    \$delta = \$player->getDirectionVector()->multiply(\$distance); // a vector has XYZ, so \$delta is the distance at each axis the player has to move    \$finalPos = \$player->add(\$delta); // \$player itself is a Vector3 (a position vector actually), so you can carry out vector additions on it directly    \$player->teleport(\$finalPos);} ```
JavaScript itself doesn't contain anything about "yaw"!

#### Attached Files:

File size:
9 KB
Views:
5
Awzaw likes this.
3. ### Kenn FattSlime

Messages:
82
GitHub:
kennfatt
Thanks! Thats explain everything
I Mean JavaScript code for MCPE Script

4. ### PrimusZombie Pigman

Messages:
748
That still makes no sense so as your signature. The best way to learn these things is to practice and way more better is to receive visual results. Try something like Processing or p5, it's a great way to learn how things operate and affect each other in any dimensions.