27

So basically I'm looking for a way to calculate the x, y and z component of a vector using 2 angles as shown: enter image description here Where alpha is the 2D angle and beta is the y angle. What I've been using uptill now for 2D vectors was:

x = Math.sin(alpha);
z = Math.cos(alpha);

After searching on stackexchange math I've found this forumula doesn't really work correctly:

 x = Math.sin(alpha)*Math.cos(beta);
 z = Math.sin(alpha)*Math.sin(beta);
 y = Math.cos(beta);

Note: when approaching 90 degrees with the beta angle the x and z components should approach zero. All help would be appreciated.

Moff Kalast
  • 1,024
  • 1
  • 12
  • 22

2 Answers2

38

The proper formulas would be

x = Math.cos(alpha) * Math.cos(beta);
z = Math.sin(alpha) * Math.cos(beta);
y = Math.sin(beta);
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • 2
    Thanks, that works and makes a lot more sense. (i had to switch the x and z but that's just how my enviroment is set) – Moff Kalast May 03 '15 at 08:43
  • @MoffKalast Two angles on perpendicular planes are sufficient to define a vector in 3D space. You could calculate the angle of the projection on the third plane (in this example, XY) using the first two angles. – andrewb May 16 '16 at 10:30
  • Any idea on how to compute it, if the used x,y,z directions were relative to a plane of a model, and not relative to the coordinate-system the model sits within? –  Sep 18 '18 at 14:17
  • I took a stab at doing this in 4D, I came up with: a = Math.cos(gamma); x = Math.cos(alpha)*Math.cos(beta)*a; z = Math.sin(alpha)*Math.cos(beta)*a; y = Math.sin(beta)*a; w = Math.sin(gamma); How close was I? – Mike Mestnik Mar 28 '19 at 16:36
  • Given alpha and beta, what is the angle between red and green lines. – Przemyslaw Remin Jun 21 '23 at 13:15
0

That formula just come from the transformation of Spherical coordinates (r, theta, phi) -> (x, y, z) to Cartesian coordinates.