Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (843) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 atan2  (Read 3999 times) 0 Members and 1 Guest are viewing this topic.
Roquen

JGO Kernel

Medals: 517

 « Posted 2013-03-14 09:42:02 »

Since trig and inverse trig come up time and time again, I spent a few minutes tossing together a reasonable atan2 approximation.  Making good approximations require more work that I'm willing to commit to this function.  (Because you really shouldn't need to call this very much:  says the old grump).

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62 `private static final float PI        = (float)(Math.PI);private static final float PI_OVER_2 = (float)(Math.PI/2);private static final float PI_OVER_4 = (float)(Math.PI/4);/** Return 'x' multiplied by the sign of 'y'. */// copySign isn't yet an intrinsic, so this isn't a desirable function to call.public static float mulsign(float x, float y) { return (Math.copySign(1, y) * x); }/** * arc tangent of x on [-1,1] *

* 5th order minimax approximation (minimizing abs error).  Banged out quickly, * a better version could be produced.  Error bound of ~0.0006 @ * x = +/-{0.205219, 0.59347, 0.888196, 1.0} */public static final float atan_5(float x)  {    // quality (of either abs or rel error) can be improved at same cost.    // Additionally (by using Estrin's) one could probably produce a higher    // order approximation at same cost (by reducing dependency chains). YMMV.    float x2 = x*x;    return x*(0.995354f+x2*(-0.288769f+0.079331f*x2));  }/** */public static final float atan(float y, float x){  float ay = Math.abs(y);  float xy = x*y; // this is xor of sign of x & y...probably removable.  float r  = 0;      // perform argument reduction.  probably can be reduced  if (x  < 0) { x = -x; r = -PI; }  if (ay > x) { float t = x; x = ay; ay = -t; r += PI_OVER_2; }      // perform the approximation..increasing speed or accuracy by   // trading the base approximation possible  r += atan_5(ay/x);  // xor the sign of reduced range with the input for final result..  // again all of this could probably be cleaned up with some thought.  return mulsign(r, xy);}  /**   * atan of x on [0,Inf]   */  public static final float atanp(float x)  {    // ignoring numeric issues around '1' due to the subtract,    // same error bound as core routine, stretched out over    // the interval.    return PI_OVER_4 + atan_5((x-1)/(x+1));  }    /** atan of x on [-Inf, Inf] */  public static final float atan(float x)  {    float r = atanp(Math.abs(x));        return Math.copySign(r, x);  }`

delt0r

JGO Wizard

Medals: 143
Exp: 18 years

Computers can do that?

 « Reply #1 - Posted 2013-03-14 09:51:48 »

It should be pointed out that using either complex numbers, Quaternions  and vectors, you can in fact avoid all trig that is not a constant. Well i think it should be pointed out.
[edit for Riven the grammar police.]

I have no special talents. I am only passionately curious.--Albert Einstein
Riven

« JGO Overlord »

Medals: 1340
Projects: 4
Exp: 16 years

 « Reply #2 - Posted 2013-03-14 09:55:01 »

It should be pointed out that using either complex numbers, Quaternions  and vectors.
Can you use that sentence in a sentence please?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Roquen

JGO Kernel

Medals: 517

 « Reply #3 - Posted 2013-03-14 09:58:21 »

Yeah, delt0r's comment is what I'm roughly saying.  It should be very rare to actually need to call trig and inverse trig functions in a video game.  Especially if you're using fixed rate simulation, which you should be doing for a huge list of reasons (in my opinion).
princec

« JGO Spiffy Duke »

Medals: 1033
Projects: 3
Exp: 20 years

Eh? Who? What? ... Me?

 « Reply #4 - Posted 2013-03-14 10:51:55 »

I use it all the time because I last looked at the alternative method with complex numbers over 25 years ago and have subsequently totally forgotten it all :S

Would you briefly describe how I'd find the angle of a bullet given its velocity (dx, dy)?
Or given an angle, how I'd calculate its direction?
I have a feeling this is a fundamental brain-change required on my part...

Cas

relminator
 « Reply #5 - Posted 2013-03-14 11:06:21 »

I use it all the time because I last looked at the alternative method with complex numbers over 25 years ago and have subsequently totally forgotten it all :S

Would you briefly describe how I'd find the angle of a bullet given its velocity (dx, dy)?
Or given an angle, how I'd calculate its direction?
I have a feeling this is a fundamental brain-change required on my part...

Cas

This.

BTW, You can directly calculate the vector components between two points via a normalized vector but that would mean an sqrt() and I would assume that's slower than a simple atan2().

A dot product should also work, but that would still use an acos().

delt0r

JGO Wizard

Medals: 143
Exp: 18 years

Computers can do that?

 « Reply #6 - Posted 2013-03-14 11:43:04 »

You don't find the angle because you don't need it. Just a vector in the direction of travel. So point at etc are all just x,y coordinates its easy to find the vector pointing in that direction. Now assume x is imaginary (or is it y) and complex multiplication is adding angles. Angles you never need to know.  Normalization is *much* faster than trig and esp atan2. Sqrt are pretty fast in comparison to trig functions. Done right you can avoid to much normalization too.

Really trig functions are pretty expensive and inverse trig even more so.

I have no special talents. I am only passionately curious.--Albert Einstein
relminator
 « Reply #7 - Posted 2013-03-14 12:03:44 »

You don't find the angle because you don't need it. Just a vector in the direction of travel. So point at etc are all just x,y coordinates its easy to find the vector pointing in that direction. Now assume x is imaginary (or is it y) and complex multiplication is adding angles. Angles you never need to know.  Normalization is *much* faster than trig and esp atan2. Sqrt are pretty fast in comparison to trig functions. Done right you can avoid to much normalization too.

Really trig functions are pretty expensive and inverse trig even more so.

In 3D yes, because you could just align your objects in an orthogonal matrix.  And a normalized direction vector would be faster and simpler because in 3D, spherical coordinates need 2 angles (azimuth and elevation) and 5 trig functions to convert to rectangular coordinates.  But in a 2D game, I still could not find a better way to align my rotated sprites without the use of *atan2().

*Excepting bezier splines, catmull-rom splines, etc. which you can get the component vectors by finding the differential.
delt0r

JGO Wizard

Medals: 143
Exp: 18 years

Computers can do that?

 « Reply #8 - Posted 2013-03-14 12:11:29 »

I have code that does it. Its easier in 2D. I could share it in the code section?

I have no special talents. I am only passionately curious.--Albert Einstein
relminator
 « Reply #9 - Posted 2013-03-14 12:53:57 »

I have code that does it. Its easier in 2D. I could share it in the code section?

Sure.  I would love to see it. ;*)
Roquen

JGO Kernel

Medals: 517

 « Reply #10 - Posted 2013-03-14 16:45:58 »

Because I'm nuts, I stared a wiki page: Complex number cookbook.  Go ask questions.  Or better yet...add content!
delt0r

JGO Wizard

Medals: 143
Exp: 18 years

Computers can do that?

 « Reply #11 - Posted 2013-03-14 16:47:39 »

Just added a new shared code post with my complex number thingy. http://www.java-gaming.org/topics/complex-angles-why-you-don-t-need-trig/28992/view.html

I have no special talents. I am only passionately curious.--Albert Einstein
Roquen

JGO Kernel

Medals: 517

 « Reply #12 - Posted 2013-03-20 15:10:57 »

Added some notes and single parameter methods atanp & atan.
Pages: [1]
 ignore  |  Print

 EgonOlsen (37 views) 2018-06-10 19:43:48 EgonOlsen (20 views) 2018-06-10 19:43:44 EgonOlsen (39 views) 2018-06-10 19:43:20 DesertCoockie (186 views) 2018-05-13 18:23:11 nelsongames (122 views) 2018-04-24 18:15:36 nelsongames (121 views) 2018-04-24 18:14:32 ivj94 (861 views) 2018-03-24 14:47:39 ivj94 (122 views) 2018-03-24 14:46:31 ivj94 (758 views) 2018-03-24 14:43:53 Solater (139 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org