Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (595) Games in Android Showcase (168) games submitted by our members Games in WIP (646) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 atan2  (Read 1823 times) 0 Members and 1 Guest are viewing this topic.
Roquen
 « 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 Knight

Medals: 33
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
« League of Dukes »

« JGO Overlord »

Medals: 1001
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
 « 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: 545
Projects: 3
Exp: 16 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 Knight

Medals: 33
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 Knight

Medals: 33
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
 « 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 Knight

Medals: 33
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
 « Reply #12 - Posted 2013-03-20 15:10:57 »

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

You cannot reply to this message, because it is very, very old.

 deepthought (47 views) 2015-06-30 15:39:44 deepthought (50 views) 2015-06-30 15:39:09 deepthought (62 views) 2015-06-30 15:36:52 Za\'Anzabar (34 views) 2015-06-29 05:44:54 TritonDreyja (46 views) 2015-06-24 17:10:40 CopyableCougar4 (46 views) 2015-06-23 00:34:45 BurntPizza (51 views) 2015-06-21 20:36:46 cookiecompiler (93 views) 2015-06-11 15:42:53 cookiecompiler (55 views) 2015-06-11 15:41:14 NegativeZero (79 views) 2015-06-11 09:49:18
 princec 30x wessles 22x BurntPizza 19x CopyableCougar4 19x opiop65 17x Riven 16x nsigma 15x ags1 14x EgonOlsen 14x theagentd 12x KaiHH 12x sunburn 11x SauronWatchesYou 11x KevinWorkman 11x Archive 11x NegativeZero 10x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 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