| |
|
Shobobo99
|
  |
| Joined: 30 Dec 2008 |
| Total Posts: 5754 |
|
|
| 05 Feb 2012 12:54 PM |
The tangent of two sides is opposite side over adjacent side, and assuming x is the adjacent side, it would be:
math.atan(y/x) |
|
|
| Report Abuse |
|
|
| |
|
sdfgw
|
  |
 |
| Joined: 08 Jan 2009 |
| Total Posts: 41681 |
|
|
| 05 Feb 2012 02:40 PM |
Sounds like you're looking for
math.atan2(x, y) |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 05 Feb 2012 02:45 PM |
@sdfgw is correct. `math.atan2(x, y)` does what you want.
Remember, floating point numbers are not infinitely accurate! |
|
|
| Report Abuse |
|
|
| |
|
sdfgw
|
  |
 |
| Joined: 08 Jan 2009 |
| Total Posts: 41681 |
|
|
| 05 Feb 2012 02:55 PM |
The fact that there's a function designed especially to solve your problem proves you're not the first person to struggle with turning a gradient back into an angle
darned quadrants |
|
|
| Report Abuse |
|
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 05 Feb 2012 03:08 PM |
"math.atan2(x, y)"
The arguments are actually ordered y, x since you use math.atan as math.atan(y/x): math.atan2(y, x) |
|
|
| Report Abuse |
|
|
sdfgw
|
  |
 |
| Joined: 08 Jan 2009 |
| Total Posts: 41681 |
|
| |
|
|
| 05 Feb 2012 03:19 PM |
<-- Knows nothing about an arctangent (is that right?). <-- Knew to use that. But what exactly does arc-tangent 2 do? How does it work? tan(y/x) is simply opposite/adjacent (as denoted by SOHCAHTOA). Therefore, the arctan of y/x is the inverse tangent... which would give the angle. Now I understand :D It just gets the quadrant (or just positive/negative) and changes the angle to an appropriate value. So, math.atan(-3/-2) would return ~56 degrees. But math.atan2(-3, -2) would return ~(56 + 180) so about 236 (or ~-124). Am I right?
But this would imply that you can get the x and y values from the slope. Didn't we have several huge discussions about getting fractions from decimals? |
|
|
| Report Abuse |
|
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 05 Feb 2012 04:03 PM |
"But this would imply that you can get the x and y values from the slope. Didn't we have several huge discussions about getting fractions from decimals?"
If you only have slope, you should probably just use math.atan (since the data about which quadrant the angle is in has already been lost). You could use math.atan2(slope,1) if you wanted to though. Those discussions were about getting a ratio of two integers from a decimal - you don't need the integer requirement. |
|
|
| Report Abuse |
|
|
| |
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 06 Feb 2012 12:12 AM |
| A better question is why you want to find the angle. Having angles isn't useful for very much in programming generally. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 06 Feb 2012 12:30 AM |
^ Yes!
Lately I've been a bit frustrated on the over-(ab)use of trig functions and things kept in angle form when unit vectors and such are such a better choice. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 06 Feb 2012 12:33 AM |
The funny thing is, is that usually users will note that a game has cool 'This' or "That', but the actual application of the nice scripting won't really change how much the player likes the game.
It's more of a game play thing then a Oh look, it's a AI! |
|
|
| Report Abuse |
|
|
HotThoth
|
  |
 |
| Joined: 24 Aug 2010 |
| Total Posts: 1176 |
|
|
| 06 Feb 2012 12:02 PM |
| I usually only use angles for when I'm rotating, or else when I want to see if something is within a cone [like if an enemy can "see" you, you can just test if its lookVector is within say 60 degrees from the vector from its head to you]. And for the second, using :Dot is the best way to go. |
|
|
| Report Abuse |
|
|
HotThoth
|
  |
 |
| Joined: 24 Aug 2010 |
| Total Posts: 1176 |
|
|
| 06 Feb 2012 12:06 PM |
| And yes, as hinted at earlier, those arc-functions are not defined over the entire (0, 2-pi) range. Sin(3pi/4) = sin(pi/4) = sqrt(2)/2, so what should arcsin(sqrt(2)/2) return? It can't return 3pi/4 AND pi/4. That's why with just arctan, you may need additional maths to get your final answer, but with arctan2, it's built in to the function, since you give it two parameters so it knows what quadrant you're in. |
|
|
| Report Abuse |
|
|
| |
|
ChefSolo
|
  |
| Joined: 23 Oct 2011 |
| Total Posts: 676 |
|
| |
|
|
| 07 Feb 2012 01:31 AM |
"Those discussions were about getting a ratio of two integers from a decimal - you don't need the integer requirement." Yes, I know. I was just referring to getting the quadrant (which is impossible D:).
I love the over-abuse of trig functions recently. It confirms that I know what I'm doing even though I never use trig. :D |
|
|
| Report Abuse |
|
|