I have a sphere in 3D. At runtime i'm generating a dynamic 2048x1024 texture for it. On this texture a tiny circle is drawn, which could be anywhere. I have the x/y of this circle on the texture, and consequently the corresponding UV coordinates. Now, i'd like to interpolate where exactly on my sphere this little circle is located.
This is the code I've been using, but it always seems to be off by +/-90 degrees
// U, V are original UV obtained from dynamic texture.
_u = Math.PI * U;
_v = -2 * Math.PI * V;
_x = Math.cos(_u) * Math.sin(_v) * radius;
_y = Math.sin(_u) * Math.sin(_v) * radius;
_z = Math.cos(_v) * radius;
Thanks for any help!
According to the code you posted in the comments, here is how to generate the X, Y and Z values.
theta = 2 * PI * U;
phi = PI * V;
_x = Math.cos(theta) * Math.sin(phi) * radius;
_y = Math.sin(theta) * Math.sin(phi) * radius;
_z = -Math.cos(phi) * radius;
OLD ANSWER:
Your code is fine. Are you sure that the sphere is generated the same way, and that the poles are along the Z-axis like that? "Usually" spheres are generated with the poles along the Y-axis. Check this.
Otherwise, if you are always off by +/- 90°, play around with the sin and cos defining x and y. For example:
_x = -Math.sin(_u) * Math.sin(_v) * radius;
_y = Math.cos(_u) * Math.sin(_v) * radius;
Is a 90° rotation of the original.
There are 8 different ways you can do the sin/cos of these two components with varying signs. Without knowing how your sphere is generated, you'd have to try them all out. They go
(x = sin(u), y = cos(u));
(x = sin(u), y = -cos(u));
(x = -sin(u), y = cos(u));
(x = -sin(u), y = -cos(u));
(x = cos(u), y = sin(u));
(x = cos(u), y = -sin(u));
(x = -cos(u), y = sin(u));
(x = -cos(u), y = -sin(u));
However this is painful and hacky, I would strongly suggest trying to find out how your sphere is generated. They usually generate x, y, z positions by looping through U and V.