-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.pde
More file actions
127 lines (110 loc) · 4.82 KB
/
Camera.pde
File metadata and controls
127 lines (110 loc) · 4.82 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
// Created for CSCI 5611 by Liam Tyler
// WASD keys move the camera relative to its current orientation
// Arrow keys rotate the camera's orientation
// Holding shit boosts the move speed
class Camera
{
Camera()
{
position = new PVector( width/2, height/2, (height/1.25) / tan(PI/6) ); // initial position
theta = 0; // rotation around Y axis. Starts with forward direction as ( 0, 0, -1 )
phi = 0; // rotation around X axis. Starts with up direction as ( 0, 1, 0 )
moveSpeed = 40;
turnSpeed = 1.0; // radians/sec
boostSpeed = 10; // extra speed boost for when you press shift
// dont need to change these
shiftPressed = false;
negativeMovement = new PVector( 0, 0, 0 );
positiveMovement = new PVector( 0, 0, 0 );
negativeTurn = new PVector( 0, 0 ); // .x for theta, .y for phi
positiveTurn = new PVector( 0, 0 );
fovy = PI / 4;
aspectRatio = width / (float) height;
nearPlane = 0.1;
farPlane = 10000;
}
void Update(float dt)
{
theta += turnSpeed * ( negativeTurn.x + positiveTurn.x)*dt;
// cap the rotation about the X axis to be less than 90 degrees to avoid gimble lock
float maxAngleInRadians = 85 * PI / 180;
phi = min( maxAngleInRadians, max( -maxAngleInRadians, phi + turnSpeed * ( negativeTurn.y + positiveTurn.y ) * dt ) );
// re-orienting the angles to match the wikipedia formulas: https://en.wikipedia.org/wiki/Spherical_coordinate_system
// except that their theta and phi are named opposite
float t = theta + PI / 2;
float p = phi + PI / 2;
PVector forwardDir = new PVector( sin( p ) * cos( t ), cos( p ), -sin( p ) * sin ( t ) );
PVector upDir = new PVector( sin( phi ) * cos( t ), cos( phi ), -sin( t ) * sin( phi ) );
PVector rightDir = new PVector( cos( theta ), 0, -sin( theta ) );
PVector velocity = new PVector( negativeMovement.x + positiveMovement.x, negativeMovement.y + positiveMovement.y, negativeMovement.z + positiveMovement.z );
position.add( PVector.mult( forwardDir, moveSpeed * velocity.z * dt ) );
position.add( PVector.mult( upDir, moveSpeed * velocity.y * dt ) );
position.add( PVector.mult( rightDir, moveSpeed * velocity.x * dt ) );
aspectRatio = width / (float) height;
perspective( fovy, aspectRatio, nearPlane, farPlane );
camera( position.x, position.y, position.z,
position.x + forwardDir.x, position.y + forwardDir.y, position.z + forwardDir.z,
upDir.x, upDir.y, upDir.z );
}
// only need to change if you want difrent keys for the controls
void HandleKeyPressed()
{
if ( key == 'w' || key == 'W' ) positiveMovement.z = 1;
if ( key == 's' || key == 'S' ) negativeMovement.z = -1;
if ( key == 'a' || key == 'A' ) negativeMovement.x = -1;
if ( key == 'd' || key == 'D' ) positiveMovement.x = 1;
if ( key == 'q' || key == 'Q' ) positiveMovement.y = 1;
if ( key == 'e' || key == 'E' ) negativeMovement.y = -1;
if ( key == 'r' || key == 'R' ){
Camera defaults = new Camera();
position = defaults.position;
theta = defaults.theta;
phi = defaults.phi;
}
if ( keyCode == LEFT ) negativeTurn.x = 0.5;
if ( keyCode == RIGHT ) positiveTurn.x = -0.5;
if ( keyCode == UP ) positiveTurn.y = 0.5;
if ( keyCode == DOWN ) negativeTurn.y = -0.5;
if ( keyCode == SHIFT ) shiftPressed = true;
if (shiftPressed){
positiveMovement.mult(boostSpeed);
negativeMovement.mult(boostSpeed);
}
}
// only need to change if you want difrent keys for the controls
void HandleKeyReleased()
{
if ( key == 'w' || key == 'W' ) positiveMovement.z = 0;
if ( key == 'q' || key == 'Q' ) positiveMovement.y = 0;
if ( key == 'd' || key == 'D' ) positiveMovement.x = 0;
if ( key == 'a' || key == 'A' ) negativeMovement.x = 0;
if ( key == 's' || key == 'S' ) negativeMovement.z = 0;
if ( key == 'e' || key == 'E' ) negativeMovement.y = 0;
if ( keyCode == LEFT ) negativeTurn.x = 0;
if ( keyCode == RIGHT ) positiveTurn.x = 0;
if ( keyCode == UP ) positiveTurn.y = 0;
if ( keyCode == DOWN ) negativeTurn.y = 0;
if ( keyCode == SHIFT ){
shiftPressed = false;
positiveMovement.mult(1.0/boostSpeed);
negativeMovement.mult(1.0/boostSpeed);
}
}
// only necessary to change if you want different start position, orientation, or speeds
PVector position;
float theta;
float phi;
float moveSpeed;
float turnSpeed;
float boostSpeed;
// probably don't need / want to change any of the below variables
float fovy;
float aspectRatio;
float nearPlane;
float farPlane;
PVector negativeMovement;
PVector positiveMovement;
PVector negativeTurn;
PVector positiveTurn;
boolean shiftPressed;
};