Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Written by Marco Pennekamp.
- arctan2 results:
- 1e6 iterations.
- Fast arctan2:
- 14763917 ns
- 13036519 ns
- 13084908 ns
- 12826451 ns
- 12405162 ns
- 12806097 ns
- 12592956 ns
- 12683205 ns
- 12366759 ns
- 12316065 ns
- Math atan2:
- 127117003 ns
- 124614619 ns
- 122801197 ns
- 126679586 ns
- 123187922 ns
- 124409927 ns
- 122724773 ns
- 121995871 ns
- 122014689 ns
- 122691362 ns
- So, about 10x faster and 12 - 14ns per arctan2 call.
- Accuracy: (randomly taken values)
- Fast Java
- x<0;y>0 -1.215991 -1.2179305185113805
- x<0;y<0 -2.1207047 -2.1160234823321717
- x>0;y>0 1.5273854 1.5273896081693032
- x>0;y<0 2.3276424 2.3288875696242464
- Conclusion: Not a very good accuracy, but enough for most games, especially when the yaw is calculated each update.
- */
- public static float arctan (float x) {
- if (x > 1) {
- return 1.57079632675f - x / (x * x + 0.28f);
- }else if (x < -1) {
- return -1.57079632675f - x / (x * x + 0.28f);
- }else {
- return x / (1f + 0.28f * x * x);
- }
- }
- public static float arctan2 (float y, float x) {
- if (x > 0) {
- return arctan (y / x);
- }else if (x < 0) {
- if (y >= 0) {
- return arctan(y / x) + 3.1415926535f;
- }
- return arctan(y / x) - 3.1415926535f;
- }
- if (y > 0) {
- return 1.57079632675f;
- }
- if (y < 0) {
- return -1.57079632675f;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement