-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCamera.java
235 lines (196 loc) · 8.79 KB
/
Camera.java
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
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JPanel;
public class Camera
{
//field of view of the camera.
private double fov; //strictly reffers to the horizontal fov as vertical fov is based off screen height
private Vector3 position; //position of camera in world-space
private double hAngle; //horizontal angle of camera
private double vAngle; //vertical angle of camera
private double renderPlaneDistance; //distance from the camera that the rendering plane is
private double farClipDistance; //how far away should triangles stop being rendered?
private double nearClipDistance; //how close should triangles stop being rendered?
//movement controller
private OrbitCamController orbitController = null;
//width of the render plane based off fov.
private double renderPlaneWidth;
public Camera(Vector3 positionIn, double farClipDistanceIn, double nearClipDistanceIn, double fovIn)
{
renderPlaneDistance = 10;
hAngle = 0;
vAngle = 0;
position = positionIn;
farClipDistance = farClipDistanceIn;
nearClipDistance = nearClipDistanceIn;
setFov(fovIn);
}
//sets the v and h angles to look at the specified position.
public void lookAt(Vector3 pos)
{
hAngle = (pos.x-position.x < 0)? -Math.toDegrees(Math.atan((pos.z-position.z)/(pos.x-position.x)))-90 : 90-Math.toDegrees(Math.atan((pos.z-position.z)/(pos.x-position.x)));
vAngle = Math.toDegrees(Math.atan((pos.y-position.y)/(Math.sqrt((pos.x-position.x)*(pos.x-position.x) + (pos.z-position.z)*(pos.z-position.z)))));
hAngle%=360;
vAngle%=360;
}
//camera controller which orbits a specified GameObject. panning the camera will cause it to
//circle around that object. The user can also use the scroll wheel to zoom in and out from
//the game object.
class OrbitCamController implements MouseMotionListener, MouseWheelListener, MouseListener
{
private int maxDistance = 4000; //maximum distance the camera can be from the object
private int minDistance = 500; //minimum distance
private double distance; //distacne from the object
private int maxAngle = 80; //the maximum angle the camera can go up to
private int minAngle = -80; //the minimum angle the camera can go down to.
private GameObject focusObj; //the game object that the camera is focused on.
private double sensitivity; //how fast should the camera pan?
//the previous mouse click position
private int prevX = 0;
private int prevY = 0;
//the difference in position between the camera and the object it's focusing on.
private Vector3 difference;
private Vector3 directionUnit; // the normalized vector pointing away from the focusObj
public OrbitCamController(GameObject focusObjectIn, double startDistanceIn, double sensitivityIn)
{
focusObj = focusObjectIn;
distance = startDistanceIn;
sensitivity = sensitivityIn;
directionUnit = new Vector3();
//sets up the position of the camera.
position = Vector3.add(focusObj.getTransform().getPosition(), new Vector3(0, 0, -distance));
directionUnit = Vector3.subtract(position, focusObj.getTransform().getPosition()).getNormalized();
difference = Vector3.multiply(directionUnit, startDistanceIn);
}
//changes the distance from the camera to the focusObj based on the mouse movement.
public void mouseWheelMoved(MouseWheelEvent e)
{
if (FlightSimulator.flightSim.getGamePanel().isPaused())
return;
distance = Math.max(minDistance, Math.min(distance + e.getWheelRotation()*30, maxDistance));
difference = Vector3.multiply(directionUnit, distance);
updatePosition();
}
//pans the difference vector around the focused object by changing the directionUnit vector
//and then multiplying that by the distance scalar to get the actual differece, then calls
//the update position method which sets the position of the camera based on the difference vector.
public void mouseDragged(MouseEvent e)
{
if (FlightSimulator.flightSim.getGamePanel().isPaused())
return;
directionUnit = Vector3.rotateAroundYaxis(directionUnit, (e.getX()-prevX)/(2000/sensitivity));
if (vAngle > -maxAngle && (e.getY()-prevY)/(200/sensitivity) > 0)
directionUnit = Vector3.rotateAroundYaxis(Vector3.rotateAroundXaxis(Vector3.rotateAroundYaxis(directionUnit, -hAngle*0.017453292519943295), (e.getY()-prevY)/(2000/sensitivity)), hAngle*0.017453292519943295);
else if (vAngle < -minAngle && (e.getY()-prevY)/(200/sensitivity) < 0)
directionUnit = Vector3.rotateAroundYaxis(Vector3.rotateAroundXaxis(Vector3.rotateAroundYaxis(directionUnit, -hAngle*0.017453292519943295), (e.getY()-prevY)/(2000/sensitivity)), hAngle*0.017453292519943295);
difference = Vector3.multiply(directionUnit, distance);
updatePosition();
lookAt(focusObj.getTransform().getPosition());
vAngle = Math.max(-89, Math.min(89, vAngle));
prevX = e.getX();
prevY = e.getY();
}
public void mousePressed(MouseEvent e)
{
prevX = e.getX();
prevY = e.getY();
}
//sets the sensitivty of the camera which is used for user settings
public void setSensitivity(double sens)
{
sensitivity = sens;
}
//updates the position of the camera to be around the focusObject.
//uses the difference vector calculated on other methods
public void updatePosition()
{
if (FlightSimulator.flightSim.getGamePanel().isPaused())
return;
position = Vector3.add(focusObj.getTransform().getPosition(), difference);
}
//returns the focusObj
public GameObject getFocusObj()
{
return focusObj;
}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
//sets the controls of the camera to orbit mode, with the needed values for the constructor.
public void setOrbitControls(JPanel panel, GameObject focusObject, double startDistance, double sensitivity)
{
orbitController = new OrbitCamController(focusObject, startDistance, sensitivity);
panel.addMouseListener(orbitController);
panel.addMouseMotionListener(orbitController);
panel.addMouseWheelListener(orbitController);
}
//calculates the render plane width, which is a slightly expensive method, so it is only called once.
private double calculateRenderPlaneWidth()
{
return Math.tan(fov*0.017453292519943295/2)*renderPlaneDistance*2;
}
//#region getter/setter methods
public double getFarClipDistancee()
{
return farClipDistance;
}
//returns the distance of the near clipping pane used by rendering
public double getNearClipDistance()
{
return nearClipDistance;
}
public OrbitCamController getOrbitCamController()
{
return orbitController;
}
//sets the fov but also re calculates the width of the rendering plane
//based on the new fov which is used for rendering
public void setFov(double fovIn)
{
fov = fovIn;
renderPlaneWidth = calculateRenderPlaneWidth();
}
public void setSensitivity(double sense)
{
if (orbitController != null)
orbitController.setSensitivity(sense);
}
public double getRenderPlaneDistance()
{
return renderPlaneDistance;
}
//returns the direction of the camera as a normalized vector.
public Vector3 getDirectionVector()
{
return Vector3.angleToVector(hAngle*0.017453292519943295, vAngle*0.017453292519943295);
}
//returns the horizontal orientation of the camera (yaw)
public double getHorientation()
{
return hAngle;
}
//returns the vertical orientation of the camera (pitch)
public double getVorientation()
{
return vAngle;
}
public void setFocusObj(GameObject obj)
{
orbitController.focusObj = obj;
}
public Vector3 getPosition()
{
return position;
}
public double getRenderPlaneWidth()
{
return renderPlaneWidth;
}
//#endregion
}