0

I am programming a Minecraft Bukkit plugin and need a way to calculate an input number from 0 to 360 for displaying a custom compass. So if the player directly looks at the object (shouldn't handle viewing height or position height), this number would be 0 and if the player's back is looking on the object it would be 180.

I already successfully calculated both numbers I need:

  1. The absolute looking angle of the player. Is 0 when the player looks in north direction and 180 in south direction.

  2. The location angle between the player's position and the object's position. Using Math.atan2 to get the angle between [X, Z] of these locations.

Both values seems to be calculated correctly. But I can't find out what to do to get the number I described at first. Tried substraction, addition. Any ideas?

Aich
  • 962
  • 2
  • 9
  • 19
  • 1
    Might rather be a question for http://gamedev.stackexchange.com/ ? – dot_Sp0T Feb 21 '14 at 11:17
  • I guess it's still common geometry math regardless of where it is going to be used in. Is it possible to move questions though? – Aich Feb 21 '14 at 11:27
  • True that. Though I suppose you could be luckier over there as it is way more common in a game context. – dot_Sp0T Feb 21 '14 at 11:29

1 Answers1

0

It should be the difference between them - if the player is looking north, and the object delta x, delta z gives a bearing of 45 degrees then the needle should be in front and to the right at 45 (=45-0), if the player is looking south and tho object x,z is 45 then the needle should be behind and to the left at 225 or -135 (=45-180).

Check that you've converted the result of Math.atan2 to degrees so you're subtracting values in the same units, and that the axes conventions are consistent. This says that +ve x-axis is east and +ve z-axis is south. A bearing of 0, North is given by atan2(0,1), which implies that you should be using Math.toDegrees(Math.atan2(deltaX, -deltaZ)) to get the bearing.

When doing these sort of things, it's much easier to write up half a dozen unit tests which cover the cases and play with the signs to see what the effects are.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171