-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathvector.java
More file actions
168 lines (131 loc) · 3.32 KB
/
vector.java
File metadata and controls
168 lines (131 loc) · 3.32 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package core;
public final class vector{
//x, y, z component of the vector
public float x, y, z;
//2d position on screen (from camera point of view)
public float screenX, screenY;
//2d position on screen (from light point of view)
public float screenX_lightspace, screenY_lightspace;
public static final int Z_length = 650;
public static final int orthogonalScale = 330;
public static float old_X, old_Y, old_Z, zInverse, lengthInverse;
public static int half_width = mainThread.screen_width/2;
public static int half_height = mainThread.screen_height/2;
public static int half_width_shadowmap = mainThread.shadowmap_width/2;
//z component of the vector from light space
public float z_lightspace;
public vector(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
//calculate its 2D location on the screen
updateLocation();
}
public void add(vector v){
x+=v.x;
y+=v.y;
z+=v.z;
}
public void add(float a, float b, float c){
x+=a;
y+=b;
z+=c;
}
public void add(vector v, float scaler){
x+=v.x*scaler;
y+=v.y*scaler;
z+=v.z*scaler;
}
public void subtract(vector v){
x-=v.x;
y-=v.y;
z-=v.z;
}
//amplify each component of the vector by a number
public void scale(float d){
x*=d;
y*=d;
z*=d;
}
//normalize the vector
public void unit(){
lengthInverse = 1/getLength();
x = x*lengthInverse;
y = y*lengthInverse;
z = z*lengthInverse;
}
//find the magnitude of the vector
public float getLength(){
return (float)Math.sqrt(x*x + y*y + z*z);
}
//retrun the dot product of this vector with another vector
public float dot(vector v){
return x*v.x + y*v.y + z*v.z;
}
public void cross(vector v1, vector v2){
x = v1.y*v2.z - v1.z*v2.y;
y = v1.z*v2.x - v1.x*v2.z;
z = v1.x*v2.y - v1.y*v2.x;
}
//rotate the vector along Y axis
public void rotate_XZ(int angle){
float sin = gameData.sin[angle];
float cos = gameData.cos[angle];
old_X = x;
old_Z = z;
x = cos*old_X - sin*old_Z;
z = sin*old_X + cos*old_Z;
}
//rotate the vector along X axis
public void rotate_YZ(int angle){
float sin = gameData.sin[angle];
float cos = gameData.cos[angle];
old_Y = y;
old_Z = z;
y = cos*old_Y - sin*old_Z;
z = sin*old_Y + cos*old_Z;
}
//rotate the vector along Z axis
public void rotate_XY(int angle){
float sin = gameData.sin[angle];
float cos = gameData.cos[angle];
old_X = x;
old_Y = y;
x = cos*old_X - sin*old_Y;
y = sin*old_X + cos*old_Y;
}
//set all the component equal to the corresponding component of a given vector
public void set(vector v){
x = v.x;
y = v.y;
z = v.z;
}
public void set(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
//set all the component to 0
public void reset(){
x = 0;
y = 0;
z = 0;
}
public void updateLocation(){
//find the 2D screen location of this vector
zInverse = Z_length/z;
screenX = x*zInverse + half_width;
screenY = -y*zInverse + half_height;
}
public void updateLocationOrthognal(){
//find the 2D screen location of this vector in Orthographic projection
screenX_lightspace = x*orthogonalScale + half_width_shadowmap;
screenY_lightspace = -y*orthogonalScale + half_width_shadowmap;
}
public vector myClone(){
return new vector(x,y,z);
}
public String toString(){
return "(" + x + ", " + y + ", " + z + ")";
}
}