summaryrefslogtreecommitdiff
path: root/core/src/sk/neuromancer/sphaera/rewrite/SphereUtils.java
blob: e59fef322fb619616819809f7f685979430c083f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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);
    }
}