-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
371 lines (318 loc) · 10.9 KB
/
Camera.java
File metadata and controls
371 lines (318 loc) · 10.9 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Camera {
// stores location, rotation and FOV data
private float x, y, z, rot, pitch, FOV;
private int height;
// used for camera physics
private int camSpeed = 2;
// stores velocity on x, y plane
private float velocity = 0;
// stores z velocity
private float velocityUp = 0;
// stores the current number of jumps since touching ground
private float numJumps = 0;
// rotation applied to x, y velocity
private float movementRot = 0;
// floor height
private float floorHeight = 0;
// used to store the next coordinates for the camera
private float newX, newY;
public Camera(int x, int y, int z, float rot, int FOV) {
// sets all variables to inputed stating coordinates
this.x = x;
this.y = y;
this.z = z;
this.height = 50;
this.rot = (float) Math.toRadians(rot);
this.FOV = (float) Math.toRadians(FOV);
this.pitch = 0;
// if possible tries to read camera save data
try {
readSave();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// reads in save data to apply to camera loc and rot
private void readSave() {
try {
// accesses file location
File saveFile = new File("SaveData/Camera.csv");
Scanner input = new Scanner(saveFile);
// clears first line
input.nextLine();
// takes data and splits it
String[] data = input.nextLine().split(",");
// reads through data and sets variables
this.x = Float.parseFloat(data[0]);
this.y = Float.parseFloat(data[1]);
this.z = Float.parseFloat(data[2]);
this.rot = Float.parseFloat(data[3]);
this.pitch = Float.parseFloat(data[4]);
// closes scanner
input.close();
} catch (FileNotFoundException e) {
// if read in fails camera will use previously set data
e.printStackTrace();
}
}
// used before program closes to save data
public void saveData() {
// create a PrintWriter from the file
PrintWriter output = null;
try{
output = new PrintWriter("SaveData/Camera.csv");
}catch(Exception e){
e.printStackTrace();
}
// adds the header line
output.println("x,y,z,rot,pitch");
// writes out camera location data
output.println(this.x + "," + this.y + "," + this.z + "," + this.rot + "," + this.pitch);
// closes the PrintWriter
output.close();
}
// used to adjust velocities and rotations that move the player
public void moveForward() {
// sets the camera speed to the camSpeed
if (this.velocity < camSpeed) {
this.velocity = camSpeed;
}
// points the movement vector forward
this.movementRot = 0;
}
public void moveBackward() {
// sets the camera speed to the camSpeed
if (this.velocity < camSpeed) {
this.velocity = camSpeed;
}
// points movement vectore backward
this.movementRot = (float) Math.PI;
}
public void moveLeft() {
// sets the camera speed to the camSpeed
if (this.velocity < camSpeed) {
this.velocity = camSpeed;
}
// if moving forward
if (this.movementRot == 0) {
// turn vector 45 degrees left
this.movementRot = (float) (-Math.PI/4);
// if moving backward
} else if (this.movementRot == Math.PI) {
// turn vector 45 degrees left
this.movementRot = (float) (-3*Math.PI/4);
// if not moving forward or backward
} else {
// set vector left
this.movementRot = (float) (-Math.PI/2);
}
}
public void moveRight() {
// sets the camera speed to the camSpeed
if (this.velocity < camSpeed) {
this.velocity = camSpeed;
}
// if moving forward
if (this.movementRot == 0) {
// turn vector 45 degrees right
this.movementRot = (float) (Math.PI/4);
// if moving backward
} else if (this.movementRot == Math.PI) {
// turn vector 45 degrees right
this.movementRot = (float) (3*Math.PI/4);
// if not moving forward or backward
} else {
// set vector left
this.movementRot = (float) (Math.PI/2);
}
}
public void sprint() {
// doubles velocity
this.velocity = this.camSpeed*2;
}
// moves camera view
public void moveRot(double deltaRot) {
// converts to radians
this.rot += Math.toRadians(deltaRot);
// keeps rotation between 0 and 2PI
if (this.rot > 2*Math.PI) {
this.rot -= 2*Math.PI;
} else if (this.rot < 0) {
this.rot += 2*Math.PI;
}
}
public void movePitch(double deltaPitch) {
// converts to radians
this.pitch += Math.toRadians(deltaPitch);
// makes sure you can only look up/down 90 degrees
// keeps the rotation between PI/2 to -PI/2
if (this.pitch > (Math.PI/2)) {
this.pitch = (float) (Math.PI/2);
} else if (this.pitch < -(Math.PI/2)) {
this.pitch = (float) -(Math.PI/2);
}
}
// makes the player jump
public void jump() {
// if player is on the ground
if (this.numJumps < 2) {
// adds to the velocity upward
this.velocityUp += 4;
// increases the number of jumps
this.numJumps++;
}
}
// resets the player position
public void resetLocation () {
this.x = 100;
this.y = -250;
this.z = 250;
this.rot = 0;
this.newX = 100;
this.newY = -250;
this.numJumps = 0;
this.velocity = 0;
}
// uses current velocities to generate the new coordinates
// prior to collision detection
public void calculateNewCordinates() {
this.newX = (float) (this.x + Math.cos(this.movementRot+this.rot) * camSpeed * velocity);
this.newY = (float) (this.y + Math.sin(this.movementRot+this.rot) * camSpeed * velocity);
}
// updates all location variables using velocities
public void update() {
// updates x, y, z
this.x = this.newX;
this.y = this.newY;
this.z += this.velocityUp;
if (this.z < -250 + this.height) {
resetLocation();
}
// if the velocity is positive
if (this.velocity > 0) {
// decelerate
this.velocity -= 0.1;
// if negative
} else {
// set to 0
this.velocity = 0;
}
// if player is in the air
if (this.z > this.floorHeight+this.height) {
// accelerate downward
this.velocityUp -= 0.30;
// if player is below ground
} else {
// set player to ground height
this.z = this.floorHeight+this.height;
// stop movement upward/downward
this.velocityUp = 0;
// sets number of jumps in air to 0
this.numJumps = 0;
}
}
// given a floor it keeps the camera withing its bounding box
public void checkCollisions(Floor floor) {
if (floor == null) {
return;
}
ArrayList <Wall> walls = floor.getWalls();
for (Wall w : walls) {
if (this.z - this.height + 20 > w.getMinZ() && this.z - this.height + 20 < w.getMaxZ()) {
checkLineCollisions(w.getPoint1(), w.getPoint2(), w.getNormal());
}
}
}
// checks axis aligned collisions with floors
private void checkAACollisions() {
}
private void checkLineCollisions(Vertex2D v1, Vertex2D v2, Normal n) {
double x1 = v1.getX();
double y1 = v1.getY();
double x2 = v2.getX();
double y2 = v2.getY();
float length = (float) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double theta;
if (x1 == x2) {
if (y1 <= y2) {
theta = Math.PI/2;
} else {
theta = -Math.PI/2;
}
} else {
theta = Math.atan2((y2-y1),(x2-x1));
}
if (theta == -Math.PI/2 || theta == Math.PI/2 || theta == 0 || theta == Math.PI || theta == -Math.PI) {
double x = this.newX - x1;
double y = this.newY - y1;
double normalY;
this.newX = (float) (x*Math.cos(theta) + y*Math.sin(theta));
this.newY = (float) (y*Math.cos(theta) - x*Math.sin(theta));
normalY = (float) (n.getY()*Math.cos(theta) - n.getX()*Math.sin(theta));
x = this.newX;
y = this.newY;
if (normalY > 0) {
if (this.newX > -20 && this.newX < length + 20) {
if (y < 25 && y > 0) {
y = 25;
}
}
} else if (normalY < 0) {
if (this.newX > -20 && this.newX < length + 20) {
if (y > -25 && y < 0) {
y = - 25;
}
}
}
this.newX = (float) (x*Math.cos(-theta) + y*Math.sin(-theta));
this.newY = (float) (y*Math.cos(-theta) - x*Math.sin(-theta));
this.newX += x1;
this.newY += y1;
}
}
// stops the player
public void stop() {
this.velocity = 0;
}
// setters and getters
public void setFloorHeight(float floorHeight) {
this.floorHeight = floorHeight;
}
public void setFOV( int FOV) {
this.FOV = FOV;
}
public float getX() {
return this.x;
}
public float getY() {
return this.y;
}
public float getZ() {
return this.z;
}
public float getNewX() {
return this.newX;
}
public float getNewY() {
return this.newY;
}
public float getRot() {
return this.rot;
}
public float getPitch() {
return this.pitch;
}
public float getFOV() {
return this.FOV;
}
// converts camera to 3D Vertex
public Vertex3D toVertex() {
return new Vertex3D(this.x, this.y, this.z);
}
}