# Solved Yaw + 180 equals a negative number?

Discussion in 'Spigot Plugin Development' started by ReadySetPawn, Sep 13, 2015.

1. ### ReadySetPawn

Okay so this is really frustrating me because it's simple math but it's causing me a lot of trouble.
In Minecraft, a player's yaw ranges from -180 to 180 so I'm adding 180 to the yaw so the range becomes 0 to 360.

Here is my code:

Code (Java):
public static int getRotation(Player player){

int yaw = Math.round(player.getLocation().getYaw() + 180);

Bukkit.broadcastMessage((player.getLocation().getYaw() + 180) + "");
Bukkit.broadcastMessage(yaw + "");

if (yaw > 270 && yaw <= 360){
return 90;
}

if (yaw > 180 && yaw <= 270){
return 0;
}

if (yaw > 90 && yaw <= 180){
return 270;
}

if (yaw > 0 && yaw <= 90){
return 180;
}
return 360;
}
It works fine when my original yaw is negative, but when my original yaw is positive, the value of "yaw" becomes negative.

Here's a simple table to demonstrate this monstrosity:

Original Yaw...........................Value of "yaw" variable
--------------------------------------------------------------------
12...........................................-168
64...........................................-116
113..........................................-67
164..........................................-16
--------------------------------------------------------------------

It seems like if you were to add the original yaw and the absolute value of the "yaw" variable, you will get 180. Can somebody please explain to me what's going on? And how I can fix it?

#1
Last edited: Sep 13, 2015
2. ### mattrick

Is it nessecary for you to convert it to 0 to 360? Why not use -180 to 180 and keep it simple?

• Like x 1
3. ### ReadySetPawn

I was using that in the beginning but I was experiencing mathematical issues as well. I can try converting it back and see if it works.

EDIT:

Here's my updated code:

Code (Java):
public static int getRotation(Player player){

int yaw = Math.round(player.getLocation().getYaw());

Bukkit.broadcastMessage(yaw + "");

if (yaw > 90 && yaw <= 180){
return 90;
}

if (yaw > 0 && yaw <= 90){
return 0;
}

if (yaw > -90 && yaw <= 0){
return 270;
}

if (yaw > -180 && yaw <= -90){
return 180;
}
return 360;
}
The positives are still messed up though. Here's another table to demonstrate:

Original Yaw............................Value of "yaw" variable
--------------------------------------------------------------------
20.............................................-340
70.............................................-290
130...........................................-230
150...........................................-210
---------------------------------------------------------------------

So it seems now that the sum of the original yaw and the absolute value of the "yaw" variable is 360.

#3
Last edited: Sep 13, 2015

Anybody?

5. ### t7seven7t

What is it you are trying to do?

Also it is is in fact 0 to 360. From the javadocs:

6. ### ReadySetPawn

I'm trying to return either 0;90;180; or 270 from the player's yaw so I can rotate a schematic in the direction they are facing. Also, I don't understand how it's 0 to 360 because in Minecraft the yaw ranges from -180 to 180. You can see this when looking at the yaw value in the f3 menu.

7. ### t7seven7t

I still don't understand how you're getting negative values. Every time an entity's yaw is updated the NMS code sets it as paramYaw % 360.0 which will always be a positive value between 0 and 360. Are you sure you're not doing any other weird magic?

8. ### ReadySetPawn

Hmm, it shows my yaw ranging from -180 to 180 in the debug menu. Here's a screenshot of it being -179.2

9. ### Rutherther

Why are you using if and else statements? You can use: int direction = yaw / 90 * 90
Yes, It looks weird, but 100 / 90 would return 1, you multiple it by 90 and you have 90

//e: if location in bukkit has yaw in minus too, this would also solve your problem

10. ### ReadySetPawn

That's a great idea! Going to use that instead. Thanks
But my yaw value is still messed up. It's still some funky number when my yaw is positive.

11. ### ReadySetPawn

Okay so it's working for me now. But it suddenly just changed from positives not working to negatives not working. I added a small fix for that so I guess it doesn't really matter lol

Here's the updated code:

Code (Java):
public static int getRotation(Player player){

int yaw = Math.round(player.getLocation().getYaw());

if (yaw < 0){
yaw+= 360;
}

return yaw / 90 * 90;
}
Thanks to everybody that helped!

12. ### Rutherther

It looks like really yaw is X and yaw you get is -(360 - X), so you can do 360 + yaw

//e: I was writing it before your post, you have it same

13. ### ReadySetPawn

Yeah xD That's the exact equation I used to come up with the solution.