Question

I am making a simple Asteroids-like game using Java that involves angles, but I'm not sure how I should format the angles and directions of the various objects.

I have three main questions:

  1. Where should 0 radians be? (Should it be pointing upwards, should it point to the right, etc.)

  2. Should turning clockwise increase or decrease the value?

  3. Should the range of the value be from 0 to (2 * pi) or from -pi to pi?

Was it helpful?

Solution

In general, you should always follow the same conventions concerning angles in ANY language, as angles are really more mathematical than computer-related concepts and math is universal (e.g. the angle-related math functions in any reasonable language should, and in my experience do behave the same way).

Therefore, these conventions are not so much unique to java as they are a good set of rules to follow whenever you're dealing with angles:

  • 0 radians is to the right.
  • increasing the angle means moving counterclockwise, so pi/2 radians is up, pi is left, ect.

The last one is a bit trickier. Mathematically speaking, the range of angles around the unit circle is [0, 2pi], and this how I prefer to use angles. In general, this makes things easier, but some java functions seem to utilize the other approach. The main trigonometric functions sin, cos, and tan can accept any number as a input (for instance sin(pi) == sin(3pi)), just as the domain of these functions in mathematics is All Real Numbers. The range of these functions does not relate to pi. However, the inverse trigonometric functions (asin, acos, and atan, but NOT atan2) have limited ranges that are defined in the javadoc:

 the returned angle is in the range -pi/2 through pi/2.

Which is also consistent with mathematics. Therefore:

  • Most of time, you should use [0, 2pi]. It's easier to read and easier to operate on.
  • When dealing with the inverse trigonometric functions, keep in mind that their range is [pi/2, -pi/2] and make conversions as necessary.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top