BabaMara - 1 year ago 93

C++ Question

I need help understanding the math behind this code. x component of the returned vec2 and the y component of the returned vec2. Can somebody explain to me what they represent. I only know this function determinate the position of the vector in sphere coordinates.

`glm::vec2 calcEulerAngles(const glm::vec3& vec)`

{

glm::vec3 v = glm::normalize(vec);

glm::vec2 result(acos(v.z), atan2(v.y, v.x));

while (result.y < 0.0)

result.y += TwoPi;

return glm::vec2(glm::degrees(result.x), glm::degrees(result.y));

}

Answer Source

It is calculating the spherical coordinates `phi`

and `theta`

for a unit vector.

The first component (`phi`

) will be the angle between the vector and the z-axis. The second component (`theta`

) will be the angle on the xy-plane. It assumes that the vector can be expressed as follows:

```
x = cos theta sin phi
y = sin theta sin phi
z = cos phi
```

If you solve this, you get the calculations in your function. Adding `2 Pi`

to `y`

just ensures that the angle is between `0`

and `2 Pi`

.