-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
190 lines (185 loc) · 7.34 KB
/
Camera.java
File metadata and controls
190 lines (185 loc) · 7.34 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
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
//import java.awt.MouseInfo;
//import java.awt.Robot;
//import java.awt.Dimension;
//import java.awt.Toolkit;
import java.awt.*;
import java.awt.image.MemoryImageSource;
public class Camera implements KeyListener{
public double xPos, yPos, zPos, xDir, yDir, xPlane, yPlane;
public boolean lookLeft, lookRight, lookUp, lookDown, moveForward, moveBack, strafeRight, strafeLeft, jump;
public final double STROLL_SPEED = .01;
public final double WALK_SPEED = .025;
public final double SPRINT_SPEED = .045;
public double MOVE_SPEED = WALK_SPEED;
public double ROTATION_SPEED = .025;
public int mouseY = MouseInfo.getPointerInfo().getLocation().y;
public int mouseX = MouseInfo.getPointerInfo().getLocation().x;
public boolean mouseEnabled = true;
private double mouseSensitivity= .005;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public Camera(double x, double y, double z, double xd, double yd, double xp, double yp) {
xPos = x;
yPos = y;
zPos = z;
xDir = xd;
yDir = yd;
xPlane = xp;
yPlane = yp;
}
public void keyPressed(KeyEvent key) {
switch(key.getKeyCode()) {
case KeyEvent.VK_LEFT:
lookLeft = true;
break;
case KeyEvent.VK_RIGHT:
lookRight = true;
break;
case KeyEvent.VK_UP:
lookUp = true;
break;
case KeyEvent.VK_DOWN:
lookDown = true;
break;
case KeyEvent.VK_W:
moveForward = true;
break;
case KeyEvent.VK_S:
moveBack = true;
break;
case KeyEvent.VK_A:
strafeLeft = true;
break;
case KeyEvent.VK_D:
strafeRight = true;
break;
case KeyEvent.VK_SPACE:
jump = true;
break;
case KeyEvent.VK_CONTROL:
MOVE_SPEED = STROLL_SPEED;
break;
case KeyEvent.VK_SHIFT:
MOVE_SPEED = SPRINT_SPEED;
break;
case KeyEvent.VK_ESCAPE:
System.exit(1);
break;
}
}
public void keyReleased(KeyEvent key) {
switch(key.getKeyCode()) {
case KeyEvent.VK_LEFT:
lookLeft = false;
break;
case KeyEvent.VK_RIGHT:
lookRight = false;
break;
case KeyEvent.VK_UP:
lookUp = false;
break;
case KeyEvent.VK_DOWN:
lookDown = false;
break;
case KeyEvent.VK_W:
moveForward = false;
break;
case KeyEvent.VK_S:
moveBack = false;
break;
case KeyEvent.VK_A:
strafeLeft = false;
break;
case KeyEvent.VK_D:
strafeRight = false;
break;
case KeyEvent.VK_SPACE:
jump = false;
break;
case KeyEvent.VK_CONTROL:
MOVE_SPEED = WALK_SPEED;
break;
case KeyEvent.VK_SHIFT:
MOVE_SPEED = WALK_SPEED;
break;
case KeyEvent.VK_PLUS:
mouseSensitivity += .005;
break;
case KeyEvent.VK_MINUS:
if(mouseSensitivity > .005)
mouseSensitivity -= .005;
break;
}
}
public void update(int[][] map) {
if(moveForward) {
if(map[(int)(xPos + xDir * MOVE_SPEED)][(int)yPos] == 0) {
xPos+=xDir*MOVE_SPEED;
}
if(map[(int)xPos][(int)(yPos + yDir * MOVE_SPEED)] ==0)
yPos+=yDir*MOVE_SPEED;
}
if(moveBack) {
if(map[(int)(xPos - xDir * MOVE_SPEED)][(int)yPos] == 0)
xPos-=xDir*MOVE_SPEED;
if(map[(int)xPos][(int)(yPos - yDir * MOVE_SPEED)]==0)
yPos-=yDir*MOVE_SPEED;
}
if(strafeLeft) {
if(map[(int)(xPos - yDir * MOVE_SPEED)][(int)yPos] == 0) {
xPos-=yDir*MOVE_SPEED;
}
if(map[(int)xPos][(int)(yPos + xDir * MOVE_SPEED)] ==0)
yPos+=xDir*MOVE_SPEED;
}
if(strafeRight) {
if(map[(int)(xPos + yDir * MOVE_SPEED)][(int)yPos] == 0)
xPos+=yDir*MOVE_SPEED;
if(map[(int)xPos][(int)(yPos - xDir * MOVE_SPEED)]==0)
yPos-=xDir*MOVE_SPEED;
}
if(lookRight) {
double oldxDir=xDir;
xDir=xDir*Math.cos(-ROTATION_SPEED) - yDir*Math.sin(-ROTATION_SPEED);
yDir=oldxDir*Math.sin(-ROTATION_SPEED) + yDir*Math.cos(-ROTATION_SPEED);
double oldxPlane = xPlane;
xPlane=xPlane*Math.cos(-ROTATION_SPEED) - yPlane*Math.sin(-ROTATION_SPEED);
yPlane=oldxPlane*Math.sin(-ROTATION_SPEED) + yPlane*Math.cos(-ROTATION_SPEED);
}
if(lookLeft) {
double oldxDir=xDir;
xDir=xDir*Math.cos(ROTATION_SPEED) - yDir*Math.sin(ROTATION_SPEED);
yDir=oldxDir*Math.sin(ROTATION_SPEED) + yDir*Math.cos(ROTATION_SPEED);
double oldxPlane = xPlane;
xPlane=xPlane*Math.cos(ROTATION_SPEED) - yPlane*Math.sin(ROTATION_SPEED);
yPlane=oldxPlane*Math.sin(ROTATION_SPEED) + yPlane*Math.cos(ROTATION_SPEED);
}
if(mouseEnabled) {
if(100 < Math.sqrt((mouseX-screenSize.width/2) * (mouseX-screenSize.width/2) + (mouseY-screenSize.height/2) * (mouseY-screenSize.height/2))) {
try{
Robot robot=new Robot(); //mouse robot
robot.mouseMove(screenSize.width/2, screenSize.height/2); // center cursor on middle of screen
} catch (AWTException e) { }
mouseX = MouseInfo.getPointerInfo().getLocation().x;
mouseY = MouseInfo.getPointerInfo().getLocation().y;
}
else{
int oldMouseX = mouseX;
mouseX = MouseInfo.getPointerInfo().getLocation().x;
int oldMouseY=mouseY;
mouseY=MouseInfo.getPointerInfo().getLocation().y;
double mouseMoveDistanceX = (mouseX-oldMouseX) * mouseSensitivity;
double mouseMoveDistanceY = (mouseY-oldMouseY) * mouseSensitivity;
double oldxDir = xDir;
xDir = xDir * Math.cos(-mouseMoveDistanceX) - yDir * Math.sin(-mouseMoveDistanceX);
yDir = oldxDir * Math.sin(-mouseMoveDistanceX) + yDir * Math.cos(-mouseMoveDistanceX);
double oldxPlane = xPlane;
xPlane = xPlane * Math.cos(-mouseMoveDistanceX) - yPlane * Math.sin(-mouseMoveDistanceX);
yPlane = oldxPlane * Math.sin(-mouseMoveDistanceX) + yPlane * Math.cos(-mouseMoveDistanceX);
}
}
}
public void keyTyped(KeyEvent arg0) {
}
}