How does this trigonometry work?

Hello, I was watching a tutorial to make the head rotate to camera direction.

I understand how it works except for how he got the angle to rotate to.

I know what math.asin does, but I always thought it required two sides, like, opposite/hypotenuse. So how does this work with just cameraDirection.x?

I have messaged the creator of the video but he has yet to reply so I decided I’d try here.

Thanks

3 Likes

You don’t need 2 sides to complete the Sin or Arc Sin function. If you have a scientific calculator, you can try this out for yourself. Basically what the code is doing is it’s getting the direction of the camera, this will determine what angle the head will point at with respect to the camera’s direction in terms of radians/degrees. A vector direction can range from -1 to 1, this also applies to the trigonometric identities which you can check out here:

If you don’t have a scientific calculator or a calculator that supports the 6 trigonometric functions, you can try it out here:

https://www.rapidtables.com/calc/math/Arcsin_Calculator.html

Note that if you input a number less than -1 and greater than 1, arcsin will not return a value, simply because it is undefined.

The values returned from arcsin will be a degree in radians

To help visualize the trigonometric functions, this video may help you:

https://www.reddit.com/r/math/comments/61wxas/helpful_visualisation_of_trigonometric_functions/

3 Likes

I think sine requires what you’ve mentioned , but this is asin,
it returns the arc sine of x, the arc sine is of only one value, it would be the inverse sine of x when x is lower than 1 and greater than -1,
for example : sin(arcsin x) = x , as in this case the inverse of sine can be defined as arcsine.

Basically the inverse function of the sine of x if y is the sine of x means x is the arcsin of y, so you would only need one value .

1 Like

As the other replies mention, math.asin takes only one argument. But you are right that it represents the inverse sine and thus that this single argument represents the ratio of the opposite and diagonal side lengths of a right (90 degree cornered) triangle.

Sin (angle) = opposite/diagonal
Asin(opposite/diagonal) = angle

However, note that in this case, the cameradirection variable holds a lookvector. A lookvector by definition is length one. So yes, technically it would be:
Math.asin(cameradirection.x/cameradirection.magnitude)

But since the latter magnitude is always 1 it may be omitted. I hope this helps alleviate the confusion, it’s indeed a bit tricky!

Kind regards,
Nonaz_jr

4 Likes

The 3 trigonomtric functions (sin, cos and tan) take an angle as an input, and output a value describing the ratio between two sides of the triangle, depending on what function you’re using.

asin, acos and atan, where the a stands for arc (arc sine), or also called the inverse trigonometric functions, mathematically denoted as sin⁻¹, cos⁻¹ and tan⁻¹, are basically the inverse of the trigonometric functions:

  • the trigonometric functions take an angle an the input, and ouput the ratio
  • the inverse trigonometric functions take the ratio as the input, and ouput the angle
print(math.sin(math.rad(30))) --prints 0.5
print(math.deg(math.asin(0.5))) --prints 30

Note that math.sin and others take the angle in radians not in degrees, that’s why we have to convert it to radians. And math.asin and others return the angle in radians as well, so we have to convert it to degrees.

math.sin and others are mainly used when you know the length of the angle and the length of one of the two sides that form the ratio correspondant to the used function, which satisfies this phrase (if the function was sin for example): opposite = sin(angle)*hypotenuse.

math.asin and others are used when you know two sides, and want to find the angle.
Like in your case, and I’m not sure about the math behind what you’re doing, but it’s just math.asin(cameraDirection.x) and not math.asin(cameraDirection.x/something) because it’s actually cameraDirection.x/1 and that 1 doesn’t really add much, it’s 1 because there is stuff going on with direction so it’s probably a unitvector that acts as the hypotenuse hence it’s just one. This does almost the same thing you’re doing, check it out to see more how to use math.asin.

6 Likes

Math.sin requires opp/hyd, math.asin figures out the theta. I.e figuring out HOW MUCH we need to rotate.

The best explanation between the relation of sin and asin, respectively the other trigonometric functions, is that we use sin to figure out lengths of unknown vectors, but with inverse trigonometric functions, we find out unknown angles.

Example:
Sin theta = opp/hyd = 0.5
theta = asin .5 → 30º (in radians)
In depth calculation: 180º = pi * radians; 1 radians = 180/pi. We know that asin(.5) is pi/6 SO

to convert radians to degrees we do 180/pi * pi/6 → 180pi/6pi (you can now cross out pi.) 180/6 = 30

Now, when we understand that a bit more in-depth on how the trigonometric functions relate. We take a look at cameraDirection. With CFrame:ToOBjectSpace, we draw a vector from the root to the camera and then we get the facing direction when we do .lookVector, we make are essentially just drawing an infinite Vector in the direction the camera is facing.

CameraDirection is now in trigonometric terms opp/hyd! So when we do asin(cameraDirection) it is the same as theta = asin(opposite/hypotenuse)

2 Likes

Okay so, I’ve figured what was tripping me up. It was that I was always looking for 2 sides but cameraDirection.x is just cameraDirection.x/1, so it was the 1 that was the other side.

All your replies are fantastic.

2 Likes

Where did you get the “1” in that equation?