-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
92 lines (86 loc) · 2.97 KB
/
Camera.java
File metadata and controls
92 lines (86 loc) · 2.97 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
public class Camera extends Vector3{
private Vector3 directionVector;
public double fov = 90;
public Vector3 up = new Vector3(0, 1, 0);
//yaw
private double hAngle;
//pitch
private double vAngle;
//clippy plains
double near = 0.01;
double far = 10000;
private double renderPlaneDistance = 50;
private double renderPlaneWidth;
private double lastMouseX = 0;
private double lastMouseY = 0;
Camera(Vector3 p, double f) {
super(p.x,p.y,p.z);
hAngle = 0;
vAngle = 0;
directionVector = Vector3.angleToVector(hAngle, vAngle);
setFov(f);
directionVector = Vector3.angleToVector(hAngle, vAngle);
}
public void setFov(double fovIn)
{
fov = Math.toRadians(fovIn);
//calculates a bespoke value based on the FOV
renderPlaneWidth = Math.tan(fov/2)*renderPlaneDistance*2;
}
public void translate(Vector3 t) {
super.add(t);
}
public void lookAt(Vector3 pos)
{
hAngle = (pos.x-super.x < 0)? -Math.atan((pos.z-super.z)/(pos.x-super.x))-Math.PI/2 : Math.PI/2-Math.atan((pos.z-super.z)/(pos.x-super.x));
vAngle = Math.atan((pos.y-super.y)/(Math.sqrt((pos.x-super.x)*(pos.x-super.x) + (pos.z-super.z)*(pos.z-super.z))));
hAngle%=Math.PI;
vAngle%=Math.PI;
directionVector = Vector3.angleToVector(hAngle, vAngle);
}
public void updateOrientation(double mouseX, double mouseY, double sensitivity) {
//double mouseDeltaX = mouseX- lastMouseX;
//double mouseDeltaY = mouseY- lastMouseY;
hAngle += mouseX * sensitivity; // Adjust the yaw based on the mouse's horizontal movement
vAngle -= mouseY * sensitivity; // Adjust the pitch based on the mouse's vertical movement
// clamp ing
if (vAngle > Math.PI / 2) {
vAngle = Math.PI / 2;
} else if (vAngle < -Math.PI / 2) {
vAngle = -Math.PI / 2;
}
directionVector = Vector3.angleToVector(hAngle, vAngle);
lastMouseX = mouseX;
lastMouseY= mouseY;
System.out.println("New Rotation! New camera rotation is ("+vAngle+", "+hAngle+")");
}
public static double dotProduct(Vector3 a, Vector3 b)
{
return a.x*b.x+a.y*b.y+a.z*b.z;
}
public static Vector3 crossProduct(Vector3 a, Vector3 b)
{
return new Vector3(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
}
public double getRenderPlaneWidth(){
return renderPlaneWidth;
}
@Override
public String toString() {
return super.toString();
}
public Vector3 getDirectionVector() {
return directionVector;
}
public double getHorientation()
{
return hAngle;
}
public double getVorientation()
{
return vAngle;
}
public double getRenderPlaneDistance() {
return renderPlaneDistance;
}
}