Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  atan2  (Read 1341 times)
0 Members and 1 Guest are viewing this topic.
Offline 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]
 * <p>
 * 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);
  }
Offline delt0r

JGO Knight


Medals: 27
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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« 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? persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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).
Offline princec

JGO Kernel


Medals: 378
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 Smiley

Offline 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 Smiley

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().

Offline delt0r

JGO Knight


Medals: 27
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
Offline 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.
Offline delt0r

JGO Knight


Medals: 27
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
Offline 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. ;*)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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!
Offline delt0r

JGO Knight


Medals: 27
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
Offline 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

UprightPath (20 views)
2014-09-20 20:14:06

BurntPizza (26 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (95 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!