Currently, I am working on a 3-D scene that requires me to convert between polar and Cartesian coordinates as needed. To assist with this task, I have developed a function for conversion. However, I am encountering a recurring issue where one of the angles frequently results in NaN. The function I am utilizing is outlined below:
toPolar: function(x,y,z){
var sqrd = (x*x)+(y*y)+(z*z)
var radius = Math.pow(sqrd,.5)
var theta = Math.acos(z/radius)
var phi = Math.asin(y/x)
var toReturn={
r:radius,
t:theta,
p:phi
}
return toReturn
}
The problematic angle is Phi, which often returns NaN. Despite my efforts to pinpoint the source of this issue, it appears to occur unpredictably throughout the scene.
You can observe the problem here:
In the top left corner of the display, you will find the camera's polar and Cartesian coordinates. Occasionally, the final section of the polar coordinates (Phi) displays as NaN.
I suspect there may be an error in my mathematical calculations, as my proficiency in math is limited. It is possible that the problem lies in how I am using Math.asin...
Thank you for your attention, and please let me know if further information is required!
-Isaac