package sk.neuromancer.sphaera.rewrite; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector3; /** * * */ public class SphereUtils { public static void main(String[] args){ test(); } public static void test(){ Vector3 topSpherical = new Vector3(10,0,0); Vector3 topCartesian = SphereUtils.toCartesianDeg(topSpherical); System.out.println(topCartesian + " should be (0,10,0))"); Vector3 xSpherical = new Vector3(10,90,90); Vector3 xCartesian = SphereUtils.toCartesianDeg(xSpherical); System.out.println(xCartesian + "should be (10,0,0)"); Vector3 rotTopToX = xCartesian.cpy().sub(topCartesian); System.out.println(rotTopToX);//seems okay } public static float angleDeg(Vector3 cartesianA, Vector3 cartesianB){ return SphereUtils.angleRad(cartesianA, cartesianB) * MathUtils.radiansToDegrees; } public static float angleRad(Vector3 cartesianA, Vector3 cartesianB){ return (float)Math.acos(((cartesianA.dot(cartesianB))/(cartesianA.len()*cartesianB.len()))); } public static Vector3 sphericalToRad(Vector3 sphericalDeg){ return new Vector3(sphericalDeg.x, sphericalDeg.y * MathUtils.degreesToRadians, sphericalDeg.z * MathUtils.degreesToRadians); } public static Vector3 sphericalToDeg(Vector3 sphericalRad){ return new Vector3(sphericalRad.x, sphericalRad.y * MathUtils.radiansToDegrees, sphericalRad.z * MathUtils.radiansToDegrees); } public static Vector3 tangentDeg(Vector3 sphericalDeg, float azimuthDeg){ return SphereUtils.tangentRad(SphereUtils.sphericalToRad(sphericalDeg), azimuthDeg * MathUtils.degreesToRadians); } public static Vector3 tangentRad(Vector3 sphericalRad, float azimuthRad){ Vector3 tangentSpherical = SphereUtils.rotateRad(sphericalRad, (float) Math.PI / 2f, azimuthRad); return SphereUtils.toCartesianRad(tangentSpherical); } public static Vector3 rotateDegDelta(Vector3 sphericalDeg, float angleDeg, float azimuthDeg){ Vector3 newSpherical = SphereUtils.rotateDeg(sphericalDeg, angleDeg, azimuthDeg); return newSpherical.sub(sphericalDeg); } public static Vector3 rotateRadDelta(Vector3 sphericalRad, float angleRad, float azimuthRad){ Vector3 newSpherical = SphereUtils.rotateRad(sphericalRad, angleRad, azimuthRad); return newSpherical.sub(sphericalRad); } public static Vector3 rotateDegVelocity(Vector3 sphericalDeg, float angleDeg, float azimuthDeg){ Vector3 newSpherical = SphereUtils.rotateDeg(sphericalDeg, angleDeg, azimuthDeg); return SphereUtils.toCartesianDeg(newSpherical).sub(SphereUtils.toCartesianDeg(sphericalDeg)); } public static Vector3 rotateRadVelocity(Vector3 sphericalRad, float angleRad, float azimuthRad){ Vector3 newSpherical = SphereUtils.rotateRad(sphericalRad, angleRad, azimuthRad); return SphereUtils.toCartesianRad(newSpherical).sub(SphereUtils.toCartesianRad(sphericalRad)); } public static Vector3 rotateDeg(Vector3 sphericalDeg, float angleDeg, float azimuthDeg){ return new Vector3(sphericalDeg.x, (float) (sphericalDeg.y - Math.cos(azimuthDeg * MathUtils.degreesToRadians) * angleDeg), (float) (sphericalDeg.z + Math.sin(azimuthDeg * MathUtils.degreesToRadians) * angleDeg)); } public static Vector3 rotateRad(Vector3 sphericalRad, float angleRad, float azimuthRad){ return new Vector3(sphericalRad.x, (float) (sphericalRad.y - Math.cos(azimuthRad) * angleRad), (float) (sphericalRad.z + Math.sin(azimuthRad) * angleRad)); } public static Vector3 toCartesianDeg(Vector3 spherical){ return SphereUtils.toCartesianDeg(spherical.x, spherical.y, spherical.z); } public static Vector3 toCartesianRad(Vector3 spherical){ return SphereUtils.toCartesianRad(spherical.x, spherical.y, spherical.z); } public static Vector3 toCartesianDeg(float radius, float polar, float azimuth){ polar*=MathUtils.degreesToRadians; azimuth*=MathUtils.degreesToRadians; return SphereUtils.toCartesianRad(radius, polar, azimuth); } public static Vector3 toCartesianRad(float radius, float polar, float azimuth){ float x = (float) (radius * Math.sin(polar) * Math.sin(azimuth)); float y = (float) (radius * Math.cos(polar)); float z = (float) (radius * Math.sin(polar) * Math.cos(azimuth)); return new Vector3(x,y,z); } public static Vector3 toSphericalDeg(Vector3 cartesian){ return SphereUtils.toSphericalDeg(cartesian.x, cartesian.y, cartesian.z); } public static Vector3 toSphericalRad(Vector3 cartesian){ return SphereUtils.toSphericalRad(cartesian.x, cartesian.y, cartesian.z); } public static Vector3 toSphericalDeg(float x, float y, float z){ return SphereUtils.sphericalToDeg(SphereUtils.toSphericalRad(x, y, z)); } public static Vector3 toSphericalRad(float x, float y, float z){ float radius = (float) Math.sqrt(x*x + y*y + z*z); float polar = (float) Math.acos(y/radius); float azimuth = (float) Math.atan2(z, x); return new Vector3(radius, polar, azimuth); } }