Repository navigation
Expand file tree
/
Copy pathCamera.java
More file actions
159 lines (103 loc) · 4.1 KB
/
Copy pathCamera.java
File metadata and controls
159 lines (103 loc) · 4.1 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
// Class that represents a Camera object
import org.lwjgl.input.Keyboard;
public class Camera {
public static final Vector3F yAxis = new Vector3F(0, 1, 0);
private Vector3F position;
// Orientation details: (Note that forward and up will always need to be perpendicular to each other so cross
// product will be very handy here)
private Vector3F forward;
private Vector3F up;
public Camera() { // "Default" Camera, like the initial view.
this(new Vector3F(0, 0, 0), new Vector3F(0, 0, 1), new Vector3F(0, 1, 0));
}
public Camera(Vector3F position, Vector3F forward, Vector3F up) {
this.position = position;
this.forward = forward.normalized();
this.up = up.normalized();
// up = up.normalized();
// forward = forward.normalized();
}
public void move(Vector3F directionToMoveIn, float amount) {
position = position.add(directionToMoveIn.mult(amount));
}
boolean mouseInViewMode = false;
public void userMove() {
Vector2F centerPosition = new Vector2F(GameWindow.getWidth()/2, GameWindow.getHeight()/2);
if (Input.getKey(Keyboard.KEY_ESCAPE)){
Input.setCursorVisible(true);
mouseInViewMode = false;
}
if (Input.getMouse(0)){
Input.setCursorVisible(false);
Input.setMousePosition(centerPosition);
mouseInViewMode = true;
}
if (mouseInViewMode) {
// Mouse.getDWheel();
float translationAmount = (float) (10 * Time.getDelta());
if (Input.getKey(Keyboard.KEY_W)) {
move(getForward(), translationAmount);
}
if (Input.getKey(Keyboard.KEY_S)) {
move(getForward(), -translationAmount);
}
if (Input.getKey(Keyboard.KEY_A)) {
move(getLeft(), translationAmount);
}
if (Input.getKey(Keyboard.KEY_D)) {
move(getRight(), translationAmount);
}
if (Input.getKey(Keyboard.KEY_R)) {
this.position = new Vector3F(0, 0, 0);
this.forward = new Vector3F(0, 0, 1);
this.up = new Vector3F(0, 1, 0);
}
Vector2F deltaPos = Input.getMousePosition().sub(centerPosition);
boolean rotY = deltaPos.getX() != 0;
boolean rotX = deltaPos.getY() != 0;
if (rotY) {
rotateY(deltaPos.getX() * 0.1f);
}
if (rotX) {
rotateX(-deltaPos.getY() * 0.1f);
}
if (rotY || rotX) {
Input.setMousePosition(centerPosition);
}
}
}
public Vector3F getLeft() { // Cross product of up and forward gives the left vector by definition.
return forward.crossProd(up).normalized();
}
public Vector3F getRight() { // This is essentially multiplying the result of getLeft() by -1
return up.crossProd(forward).normalized();
}
public void rotateX(float angle) { // Rotating Camera around x-axis
Vector3F xAxis = yAxis.crossProd(forward).normalized();
forward = forward.rotate(angle, xAxis).normalized();
up = forward.crossProd(xAxis).normalized();
}
public void rotateY(float angle) {
Vector3F xAxis = yAxis.crossProd(forward).normalized();
forward = forward.rotate(angle, yAxis).normalized();
up = forward.crossProd(xAxis).normalized();
}
public Vector3F getPosition() {
return position;
}
public void setPosition(Vector3F position) {
this.position = position;
}
public Vector3F getForward() {
return forward;
}
public void setForward(Vector3F forward) {
this.forward = forward;
}
public Vector3F getUp() {
return up;
}
public void setUp(Vector3F up) {
this.up = up;
}
}