# Mathf.Tan() calculation flat-out wrong .... ?!

So I have something curious here …

I have a projectile coming in at [×] degrees onto the x/z plane at a predefined location.
Because of frames and inaccuracies, I cant expect a projectile to halt at [y=0] “naturally”, so im using Raycast on fixedupdate() to calculate it. Im freezing the projectile (unchecking kinematic) when the Raycast gets a hit.

Following situation:
Im getting a hit at 26.381° and my projectile is freezing at [y=0.3415647]. So, to calculate where my projectile would be at [y=0], I calculate the cotangent of the angle, multiply by the height and add that to the fired direction. This should give me what I need:

``````float zPosDiff = (1f/Mathf.Tan(transform.rotation.x)) * transform.position.y;
``````

Except the tangent that Mathf.Tan() is calculating comes out … well … wrong, according to microsoft calculator and my pen&paper calculations.
In unity, im Debug.log()-ing the entire process and Mathf.Tan() is giving me [2.996449], while my math is giving me [0.495]. According to unity, my angle is actually around 71° - which is flat out wrong.
Subtracting my angle from 90° (which is ~63°) and going through the process gives me a wrong answer again…

This is very confusing to me - I have to be doing something wrong because I simply refuse to believe Mathf is using wrong lookup tables for these trig values …

any help would be appreciated!

Mathf.Tan requires the angle in radians, not degrees. You should also note that Transform.rotation is a quaternion, so transform.rotation.x is not even an angle.

3 Likes

How embarrassing… time for a break.

Thanks!

1 Like

lol, it’s likely a piece of an axis normal. I’m trying to visualize all this and it’s so goddamn funny.
@Seranach_1 nah, we all get tired sometimes, I burn myself out constantly. try get some sleep.

2 Likes

transform.rotation is a little confusing. It’s reasonable to assume that it corresponds to what is shown in the inspector as “Rotation”. What you see in the inspector is labeled as “Rotation”, but in the API it’s actually transform.localEularAngles.

Edit: also, I should warn you that transform.localEularAngles may not necessarily give you the same numbers that you see in the inspector because the angles are calculated on the fly and there can be more than one way to represent the same orientation with three angles.

2 Likes

Right, the x, y, z components actually are an axis. However the length of that axis is usually smaller than one (it’s the sine of half the angle that the quaternion represents).

One important thing to note is that quaternions are always relative rotations around a certain axis, just like vectors are always a relative translation along an axis. We can treat a vector like an absolute point, but that just implies we assume that (0,0,0) is the origin. So any vector is also a point (x,y,z) from the origin.

Quaternions work essentially the same way. When you use a quaternion as an absolute rotation, you essentially make a single relative rotation from the identity orientation. The absolute mind blowing fact about quaternions is that you can reach any orientation in space by doing a single rotation around a particular axis in space. So you always rotate from the identity orientation to the target orientation in a single rotation. The (x,y,z) components define this rotation axis while the w component is the cosine of half the angle you rotate around that axis. The (x,y,z) vector part of the quaternion is scaled down by the sine of half the angle, so the overall length of the 4d quaternion is always 1 since pure rotations are represented by unit quaternions. So the total length of the quaternion is always 1.0.

I’ve posted this link many times, but I still think, especially for programmers, this numberphile video is one of the best and shortest explanations how quaternions work. Make sure you watch the extra bits as well The 3b1b videos on the topic is also not bad, but I think it doesn’t help too much in understanding what it actually represents. Though every bit information may help to get a more complete picture of the thing ^^.

1 Like

@Bunny83 I fell in love with their simplicity after a lot of head-banging during my humble beginnings with 3D. It was a hard-earned realization, but once you drop that idea of them being unit in 4D and embrace the angle-axis representation, it becomes apparent that this core mathematical idea just happens to ALSO be a unit quaternion.

I think that nobody intentionally wanted them to be 4D, that’s a property that emerged on its own by trying to elegantly solve a system of rotational matrices, and I think this classification is what confuses people the most.

I mean people always struggle with angles, as if these dimensionless quantities are somehow very important, yet vectors have no angles as well, the angles are merely an emergent property that simply pops out from the law of cosines.

For people less inclined to properly visualize these abstract things, I find this video pretty spot on (especially the last part called “Mixing Orientations”), and even though it’s radically naive, the comparison is apt, and in fact, very similar to how we ended up with something called quantum chromodynamics (“Colors? Subatomic particles come in colors? Are you sure about that?” ; “Ja, natürlich, here’s the formula! Look it’s not colors but works just like RGB.”).

Nature, well, has its weird ways to sprinkle similar patterns all over the domain that we call reality.