Cartesian / Spherical Conversions with alternate Z Vector as 'Up'

When using Z as the up axis it's not possible to use the current FastMath.cartesianToSpherical and FastMath.sphericalToCartesian methods.



The modifications are strait forward to make it work. All it needs is a switch b/t y and z cartesian coordinates. For those who need it…




public static Vector3f sphericalToCartesianZUp(Vector3f sphereCoords,

Or you can use the already included FastMath methods sphericalToCartesianZ and cartesianZToSpherical.  :)  I notice the second method is slightly different than yours (when storing, y and z is swapped) but we use these at work sucessfully.

That's really odd, because when I do use those (figuring that's what they were for) they didn't work for me… might be worth a little investigation.

Yeah, especially since the sphere->cart method looks the same to me… so at least that should work for you.

Just as a double check I tried it again.



java.lang.ArithmeticException: This matrix cannot be inverted
   at com.jme.math.Matrix4f.invert(Matrix4f.java:1086)
   at com.jme.renderer.AbstractCamera.getWorldCoordinates(AbstractCamera.java:939)
   at com.jme.system.DisplaySystem.getWorldCoordinates(DisplaySystem.java:782)
   at com.jme.system.DisplaySystem.getWorldCoordinates(DisplaySystem.java:765)
   at org.resonus.aa.comps.ui.AAInputHandler$12.onMove(AAInputHandler.java:339)
   at com.jme.input.lwjgl.LWJGLMouseInput.update(LWJGLMouseInput.java:200)
   at com.jme.input.InputSystem.update(InputSystem.java:67)
   at com.jme.app.BaseGame.start(BaseGame.java:63)
   at org.resonus.aa.Main.main(Main.java:234)



Got that when zooming.

That's when I did a quick search and replace, replacing all my sphericalToCartesianZUp with FastMath.sphericalToCartesianZ and my cartesianToSphericalZUp with FastMath.cartesianZToSpherical.

:?

Hmm, well please alter cartesianZToSpherical to use the swapped y and z as in your method and see if it then works for you.  If so, check it in.  Looking through the code, it seems we're not using that method anyhow, instead we swap y and z and pass that in. 

Note that if you're going to keep right-handed coordinates, you should flip the sign of the new y axis (y' = -z, z' = y).