-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.pde
146 lines (127 loc) · 3.34 KB
/
camera.pde
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
// this file is complicated
// Camera Location
float CamX = 459.33347;
float CamY = -36.666653;
float CamZ = -372.99988;
// Camera View location
float tx = 349.33322;
float ty = -76.66664;
float tz = -271.99988;
// Camera Angle
float angle;
// Camera Deltas
float xComp,zComp,yComp;
//Movement Initialization
int camSpeed = 15;
boolean camUp = false;
boolean camDown = false;
boolean camLeft = false;
boolean camRight = false;
boolean camFly = false;
boolean camFall = false;
void setCamera() // set the camera
{
camera(CamX, CamY, CamZ, tx, ty, tz, 0, 1, 0);
ambientLight(255, 255, 255, CamX, CamY, CamZ);
}
void lockCam(int pos) // predefined camera viewpoints
{
if (pos == 1)
{
CamX = 459.33347;
CamY = -36.666653;
CamZ = -372.99988;
tx = 349.33322;
ty = -76.66664;
tz = -271.99988;
}
if (pos == 2)
{
CamX = -590.06555;
CamY = 103.33334;
CamZ = -710.5321;
tx = -472.06555;
ty = 63.33332;
tz = -620.5322;
}
if (pos == 3)
{
CamX = 907.5336;
CamY = -156.66676;
CamZ = 110.799355;
tx = 785.5333;
ty = -196.6667;
tz = -6.200346;
}
}
void updateCamera() // update the camera rotation
{
if (!CameraLock) // as long as the camera is not locked
{
int deltaX = mouseX - width/2; // difference of the mouse to the center of the screen
// println("camX: ", CamX, "camY: ", CamY, "camZ: ", CamZ, "TX: ", tx,"TY ", ty,"TZ", tz);
if(abs(deltaX) > 100){ // in the center the view doesn't move
xComp = tx - CamX;
zComp = tz - CamZ;
yComp = ty - CamY;
angle = correctAngle(xComp,zComp); // set the angle
//Looking 'forwards'
if ((angle >= 0 && angle < 45) || (angle > 315 && angle < 360))
tx += deltaX/25;
//Looking 'left'
else if (angle > 45 && angle < 135)
tz += deltaX/25;
//Looking 'back'
else if (angle > 135 && angle < 225)
tx -= deltaX/25;
//Looking 'right'
else if (angle > 225 && angle < 315)
tz -= deltaX/25;
}
}
}
void moveCamera() // WASD movements
{
if(camUp){ // move camera Forwards
CamZ += zComp/camSpeed;
tz+= zComp/camSpeed;
CamX += xComp/camSpeed;
tx+= xComp/camSpeed;
}
else if(camDown){ // move camera Backwards
CamZ -= zComp/camSpeed;
tz-= zComp/camSpeed;
CamX -= xComp/camSpeed;
tx-= xComp/camSpeed;
}
if (camLeft){ // move camera Left
CamZ += xComp/camSpeed;
tz+= xComp/camSpeed;
CamX -= zComp/camSpeed;
tx-= zComp/camSpeed;
}
if (camRight){ // move camera Right
CamZ -= xComp/camSpeed;
tz-= xComp/camSpeed;
CamX += zComp/camSpeed;
tx+= zComp/camSpeed;
}
if (camFly){ // move camera vertically up
CamY += yComp/int(camSpeed*0.80);
ty += yComp/int(camSpeed*0.80);
}
if (camFall){ // move camera vertically down
CamY -= yComp/int(camSpeed*0.80);
ty -= yComp/int(camSpeed*0.80);
}
}
public float correctAngle(float xc, float zc){
float newAngle = -degrees(atan(xc/zc));
if (xComp > 0 && zComp > 0)
newAngle = (90 + newAngle)+90;
else if (xComp < 0 && zComp > 0)
newAngle = newAngle + 180;
else if (xComp < 0 && zComp < 0)
newAngle = (90+ newAngle) + 270;
return newAngle;
}