Getting the X, Y, and Z angles of a 3D vector

I’m really not a strong mathematical person, so this doesn’t come easy for me.

I wanted to challenge myself by making my player’s arm look towards where I target with my mouse. However, I really don’t know how to determine the individual rotations of the vector.

I started with first getting the direction I want to use (which I assume is what I’d need)

local direction = (mousePos - shoulderPos).Unit

But then I really have no clue how to determine each angle of that vector.

Thanks for any advice.

I don’t need any physical code, rather just some explanation about how to understand and go about this math

Here’s a really nice illustration of SOH CAH TOA:

image

For this example, we’re going to pretend the hypotenuse is the arm (arm extends from the top left to the bottom right). This example only shows how to get the angle of the x-axis.

It’s fairly simple, really. You can get the Z position in direction (this is the opposite side), and the Y position in direction (this is the adjacent side). Because we’re trying to find the angle based on opposite (opp) and adjacent sides, we’re going to use the tangent:

image

It would then look like this:

math.tan(x) = opp / adj

Let’s put everything on one side:

local xAngle = math.deg(math.atan(opp / adj)))
--> atan is just arctan, which is the inverse of tan
--> math.atan returns in radians. We want degrees. math.deg converts radians to degrees

And there we go! You got your X angle. Now, you just have to repeat this to get the Yand Z angles.

1 Like