Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1671 chris 1
package com.gebauz.bauzoid.math;
2
 
3
public class Vector3
4
{
5
 
6
        public float x;
7
        public float y;
8
        public float z;
9
 
10
        public Vector3()
11
        {
12
        }
13
 
14
        public Vector3(float _x, float _y, float _z)
15
        {
16
                set(_x, _y, _z);
17
        }
18
 
19
        public void set(float _x, float _y, float _z)
20
        {
21
                x = _x;
22
                y = _y;
23
                z = _z;
24
        }
25
 
26
        public Vector3 getInverse()
27
        {
28
                return new Vector3(-x, -y, -z);
29
        }
30
 
31
        public void normalize()
32
        {
33
                float len = length();
34
                if (len != 0.0f)
35
                {
36
                        x /= len;
37
                        y /= len;
38
                        z /= len;
39
                }
40
        }
41
 
42
        public float length()
43
        {
44
                float squared = squaredLength();
45
                return (float)Math.sqrt(squared);
46
        }
47
 
48
        public void setLength(float newLength)
49
        {
50
                normalize();
51
                x *= newLength;
52
                y *= newLength;
53
                z *= newLength;
54
        }
55
 
56
        public boolean isZero()
57
        {
58
                return ((x == 0.0f) && (y == 0.0f) && (z == 0.0f));
59
        }
60
 
61
        public float squaredLength()
62
        {
63
                return (x*x + y*y + z*z);
64
        }
65
 
66
        public void addVector(Vector3 v)
67
        {
68
                x += v.x;
69
                y += v.y;
70
                z += v.z;
71
        }
72
 
73
        public void subtractVector(Vector3 v)
74
        {
75
                x -= v.x;
76
                y -= v.y;
77
                z -= v.z;
78
        }
79
 
80
        public float dotProduct(Vector3 v)
81
        {
82
                return dotProduct(this, v);                            
83
        }
84
 
85
        static public float dotProduct(Vector3 a, Vector3 b)
86
        {
87
                return (a.x*b.x + a.y*b.y + a.z*b.z);
88
        }
89
 
90
        public Vector3 crossProduct(Vector3 v)
91
        {
92
                return crossProduct(this, v);          
93
        }
94
 
95
        static public Vector3 crossProduct(Vector3 a, Vector3 b)
96
        {
97
                return new Vector3(
98
                                (a.y * b.z) - (a.z * b.y),
99
                                (a.z * b.x) - (a.x * b.z),
100
                                (a.x * b.y) - (a.y * b.x));
101
        }
102
 
103
        static public float distance(Vector3 a, Vector3 b)
104
        {
105
                Vector3 c = new Vector3(b.x - a.x, b.y - a.y, b.z - a.z);
106
                return c.length();
107
        }
108
 
109
        static public Vector3 add(Vector3 a, Vector3 b)
110
        {
111
                return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
112
        }
113
 
114
        static public Vector3 subtract(Vector3 a, Vector3 b)
115
        {
116
                return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
117
        }
118
 
119
        static public float length(float x, float y, float z)
120
        {
121
                return (float)Math.sqrt(x*x + y*y + z*z);
122
        }
123
 
124
        /** Return a copy of this Vector3. */
125
        public Vector3 copy()
126
        {
127
                return new Vector3(x, y, z);
128
        }
129
 
130
        /** Returns true if the two vectors have the same values. */
131
        public boolean equals(Vector3 other)
132
        {
133
                return ((other.x == x) && (other.y == y) && (other.z == z));
134
        }
135
}
136
 
137
 
138
 
139