# getting unexpected trigonometric equation results

• 6th March 2021, 08:49 AM
Volnaiskra
getting unexpected trigonometric equation results
I'm using the following equation that I found on Stack Overflow. It's supposed to convert any angle (0-360˚) into a range of -90˚ to 90˚:

Code:

`asin(sin(angle * PI/180.0)) * 180.0/PI`

When I plug it into Excel, it works as expected. For example, you can see it converts 240 into -60:

https://bit.ly/3eeVkEk

But when I try and use it in Fusion, it no longer constrains the answer to between -90˚ and 90˚. In fact, it just outputs the input back at me (so 240 becomes 240):

https://bit.ly/3biWbSv

What am I doing wrong?
• 6th March 2021, 12:44 PM
MuddyMole
-90˚ to 90˚?
That's only a range of 180˚. The stackflow post is talking about -180˚ to 180˚, which makes a lot more sense, and could be solved with a simple statement using "modulus" (better for performance, as trig functions are relatively expensive).

A = ((A + 360) % 360) - 180
• 6th March 2021, 03:43 PM
MuddyMole
As for the trig formula not working - Excel uses radians, whereas CF2.5 uses degrees. The " *180 / Pi" part is how you convert between the two. If you remove that and just say: ASin(Sin( Angle )) ...it will give the same result as Excel.
• 7th March 2021, 01:07 AM
Volnaiskra
Quote:

Originally Posted by MuddyMole
-90˚ to 90˚?
That's only a range of 180˚. The stackflow post is talking about -180˚ to 180˚, which makes a lot more sense, and could be solved with a simple statement using "modulus" (better for performance, as trig functions are relatively expensive).

A = ((A + 360) % 360) - 180

The post was asking for -180˚to 180˚, but one of the answers mistakenly provided a solution for -90˚ to 90˚. The author later updated the answer to produce -180˚ to 180˚, but kept the old answer in place too...luckily for me, since -90˚ to 90˚ is what I was actually after.

If you're wondering why I would want to convert 360˚ to -90˚ to 90˚, it's for a horizontally-flying enemy that I want to tilt towards the player when it chases the player. So, regardless whether it's travelling left or right, it should either tilt down towards 90˚, or up towards -90˚. Of course, in reality, tilting downwards in Fusion requires increasing angle if travelling left or decreasing it if travelling right, but I already adjust for this elsewhere in my code. So for my purposes, I just need the equation to give me 0˚ to 90˚ if tilting down or 0 to -90˚ if tilting up. It should never go beyond abs(90) as that would no longer be a tilt but would actually change X direction. I also have code already in place elsewhere that will make the enemy change X direction if need be, so for my purposes in this section the tilt must not exceed abs(90).

I'm not concerned about performance in this instance, as enemies will only perform this check once or twice a second, and even then only if they're within close proximity to the player.

Quote:

Originally Posted by MuddyMole
As for the trig formula not working - Excel uses radians, whereas CF2.5 uses degrees. The " *180 / Pi" part is how you convert between the two. If you remove that and just say: ASin(Sin( Angle )) ...it will give the same result as Excel.

Fantastic. Thanks very much! That explains it, and it works perfectly now. And ditching the ...PI/180.0)) * 180.0/PI... part makes the equation a lot less cumbersome, which is a nice bonus.