summaryrefslogtreecommitdiff
path: root/core/src/sk/neuromancer/sphaera/rewrite/DirectedSphere.java
blob: 885553c57ea50467172d7065ca8df9650e1aca59 (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
package sk.neuromancer.sphaera.rewrite;

import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.math.Vector3;

import sk.neuromancer.sphaera.interf.Moving;

public class DirectedSphere extends Sphere implements Moving {
    
    private Vector3 direction;
    
    public DirectedSphere() {
        super();
        this.direction = Vector3.Zero.cpy();
    }

    public DirectedSphere(float radius) {
        super(radius);
    }

    public DirectedSphere(float radius, Material... mat) {
        super(radius, mat);
    }

    public DirectedSphere(float x, float y, float z, float radius,
            Material... mat) {
        super(x, y, z, radius, mat);
    }
    
    public DirectedSphere(Sphere parent, float a, float b, float radius,
            Material... mat) {
        super(parent, a, b, radius, mat);
    }

    public DirectedSphere(Sphere parent, float a, float b, float radius, Vector3 direction,
            Material... mat) {
        super(parent, a, b, radius, mat);
        this.direction = direction;
    }
    
    public void randomDirection(){
        Vector3 relative = this.getRelativePosition();
        Vector3 random = new Vector3().setToRandomDirection();
        Vector3 circleNormal = relative.cpy().crs(random);
        float angle = SphereUtils.angleDeg(random, relative);
        random.rotate(circleNormal, angle);
        this.direction = random;
    }

    @Override
    public void forward(float angle) {
        Vector3 oldRelative = this.getRelativePosition();
        Vector3 circleNormal = oldRelative.cpy().crs(this.direction);
        Vector3 newRelative = oldRelative.cpy().rotate(circleNormal, angle);
        this.setVelocity(newRelative.cpy().sub(oldRelative));
    }

    @Override
    public void back(float angle) {
        this.forward(-angle);
    }

    @Override
    public void rotateAzimuth(float angle) {
        this.direction.rotate(this.getRelativePosition(), angle);
    }

    @Override
    public void rotateLeft(float angle) {
        rotateAzimuth(angle);
    }

    @Override
    public void rotateRight(float angle) {
        rotateAzimuth(-angle);
    }
    
    @Override
    public void move(float howMuch) {
        if(this.getVelocity().len() == 0)
            return;
        Vector3 oldRelative = this.getRelativePosition();
        super.move(howMuch);
        Vector3 newRelative = this.getRelativePosition();
        this.updateDirection(oldRelative, newRelative);
    }

    private void updateDirection(Vector3 oldPos, Vector3 newPos) {
        Vector3 circleNormal = oldPos.cpy().crs(newPos);
        float angle = SphereUtils.angleDeg(oldPos, newPos);
        this.direction = circleNormal.cpy().crs(newPos);
    }
    
    public void setDirection(float x, float y, float z){
        this.direction = new Vector3(x,y,z);
    }

    public void setDirection(Vector3 direction){
        this.direction = direction;
    }

    public Vector3 getDirection(){
        return this.direction.cpy();
    }
}