|
10 | 10 |
|
11 | 11 | public class FirstPersonView { |
12 | 12 |
|
13 | | - private boolean enabled; |
14 | | - private boolean left; |
15 | | - private boolean right; |
16 | | - private boolean forward; |
17 | | - private boolean back; |
18 | | - private boolean up; |
19 | | - private boolean down; |
20 | | - private float pitch = Mathf.PI; |
21 | | - private float yaw = 0; |
22 | | - private Vector3f eye = new Vector3f(-1000, 0, 1000); |
23 | | - private float speed = 10; |
24 | | - private PApplet context; |
25 | | - |
26 | | - public FirstPersonView(PApplet context) { |
27 | | - this.context = context; |
28 | | - context.registerMethod("pre", this); |
29 | | - context.registerMethod("keyEvent", this); |
30 | | - } |
31 | | - |
32 | | - public void pre() { |
33 | | - if (!enabled) |
34 | | - return; |
35 | | - yaw = Mathf.map(context.mouseX, 0, context.width, Mathf.PI, -Mathf.PI); |
36 | | - pitch = Mathf.map(context.mouseY, 0, context.height, -Mathf.PI, Mathf.PI); |
| 13 | + private boolean enabled; |
| 14 | + |
| 15 | + private boolean left; |
| 16 | + |
| 17 | + private boolean right; |
| 18 | + |
| 19 | + private boolean forward; |
| 20 | + |
| 21 | + private boolean back; |
| 22 | + |
| 23 | + private boolean up; |
| 24 | + |
| 25 | + private boolean down; |
| 26 | + |
| 27 | + private float pitch = Mathf.PI; |
| 28 | + |
| 29 | + private float yaw = 0; |
| 30 | + |
| 31 | + private Vector3f eye = new Vector3f(-1000, 0, 1000); |
| 32 | + |
| 33 | + private float speed = 10; |
| 34 | + |
| 35 | + private PApplet context; |
| 36 | + |
| 37 | + public FirstPersonView(PApplet context) { |
| 38 | + this.context = context; |
| 39 | + context.registerMethod("pre", this); |
| 40 | + context.registerMethod("keyEvent", this); |
| 41 | + } |
| 42 | + |
| 43 | + public void pre() { |
| 44 | + if (!enabled) |
| 45 | + return; |
| 46 | + yaw = Mathf.map(context.mouseX, 0, context.width, Mathf.PI, -Mathf.PI); |
| 47 | + pitch = Mathf |
| 48 | + .map(context.mouseY, 0, context.height, -Mathf.PI, Mathf.PI); |
37 | 49 |
|
38 | 50 | // if (pitch > 89) |
39 | 51 | // pitch = 89; |
40 | 52 | // if (pitch < -89) |
41 | 53 | // pitch = -89; |
42 | 54 |
|
43 | | - Vector3f front = new Vector3f(); |
44 | | - float x = Mathf.cos(Mathf.toRadians(yaw)) * Mathf.cos(Mathf.toRadians(pitch)); |
45 | | - float y = Mathf.sin(Mathf.toRadians(pitch)); |
46 | | - float z = Mathf.cos(Mathf.toRadians(yaw)) * Mathf.cos(Mathf.toRadians(pitch)); |
47 | | - front.set(x, y, z); |
| 55 | + Vector3f front = new Vector3f(); |
| 56 | + float x = Mathf.cos(Mathf.toRadians(yaw)) |
| 57 | + * Mathf.cos(Mathf.toRadians(pitch)); |
| 58 | + float y = Mathf.sin(Mathf.toRadians(pitch)); |
| 59 | + float z = Mathf.cos(Mathf.toRadians(yaw)) |
| 60 | + * Mathf.cos(Mathf.toRadians(pitch)); |
| 61 | + front.set(x, y, z); |
48 | 62 |
|
49 | | - Vector3f velocity = new Vector3f(); |
| 63 | + Vector3f velocity = new Vector3f(); |
50 | 64 |
|
51 | | - if (left) { |
52 | | - velocity.addLocal(-1, 0, 0); |
53 | | - } |
| 65 | + if (left) { |
| 66 | + velocity.addLocal(-1, 0, 0); |
| 67 | + } |
54 | 68 |
|
55 | | - if (right) { |
56 | | - velocity.addLocal(1, 0, 0); |
57 | | - } |
| 69 | + if (right) { |
| 70 | + velocity.addLocal(1, 0, 0); |
| 71 | + } |
58 | 72 |
|
59 | | - if (back) { |
60 | | - velocity.addLocal(0, 0, 1); |
61 | | - } |
| 73 | + if (back) { |
| 74 | + velocity.addLocal(0, 0, 1); |
| 75 | + } |
62 | 76 |
|
63 | | - if (forward) { |
64 | | - velocity.addLocal(0, 0, -1); |
65 | | - } |
| 77 | + if (forward) { |
| 78 | + velocity.addLocal(0, 0, -1); |
| 79 | + } |
66 | 80 |
|
67 | | - velocity.multLocal(getRotationMatrix(yaw)); |
| 81 | + velocity.multLocal(getRotationMatrix(yaw)); |
68 | 82 |
|
69 | | - eye.addLocal(velocity.mult(speed)); |
70 | | - eye.setY(-300); |
71 | | - } |
| 83 | + eye.addLocal(velocity.mult(speed)); |
| 84 | + eye.setY(-300); |
| 85 | + } |
72 | 86 |
|
73 | | - public void apply() { |
74 | | - Matrix4f m = Matrix4f.fpsViewRH(eye, pitch, yaw).transpose(); |
75 | | - PMatrix matrix = context.getMatrix(); |
76 | | - matrix.set(m.getValues()); |
77 | | - context.setMatrix(matrix); |
78 | | - } |
| 87 | + public void apply() { |
| 88 | + Matrix4f m = Matrix4f.fpsViewRH(eye, pitch, yaw).transpose(); |
| 89 | + PMatrix matrix = context.getMatrix(); |
| 90 | + matrix.set(m.getValues()); |
| 91 | + context.setMatrix(matrix); |
| 92 | + } |
79 | 93 |
|
80 | | - public void keyEvent(KeyEvent key) { |
81 | | - if (key.getAction() == KeyEvent.PRESS) |
82 | | - onKeyPressed(key.getKey()); |
83 | | - if (key.getAction() == KeyEvent.RELEASE) |
84 | | - onKeyReleased(key.getKey()); |
85 | | - } |
| 94 | + public void keyEvent(KeyEvent key) { |
| 95 | + if (key.getAction() == KeyEvent.PRESS) |
| 96 | + onKeyPressed(key.getKey()); |
| 97 | + if (key.getAction() == KeyEvent.RELEASE) |
| 98 | + onKeyReleased(key.getKey()); |
| 99 | + } |
86 | 100 |
|
87 | | - public void onKeyPressed(char key) { |
88 | | - if (key == 'w' || key == 'W') |
89 | | - forward = true; |
| 101 | + public void onKeyPressed(char key) { |
| 102 | + if (key == 'w' || key == 'W') |
| 103 | + forward = true; |
90 | 104 |
|
91 | | - if (key == 's' || key == 'S') |
92 | | - back = true; |
| 105 | + if (key == 's' || key == 'S') |
| 106 | + back = true; |
93 | 107 |
|
94 | | - if (key == 'a' || key == 'A') |
95 | | - left = true; |
| 108 | + if (key == 'a' || key == 'A') |
| 109 | + left = true; |
96 | 110 |
|
97 | | - if (key == 'd' || key == 'D') |
98 | | - right = true; |
| 111 | + if (key == 'd' || key == 'D') |
| 112 | + right = true; |
99 | 113 |
|
100 | | - if (key == ' ') |
101 | | - up = true; |
| 114 | + if (key == ' ') |
| 115 | + up = true; |
102 | 116 |
|
103 | | - if (key == 'c' || key == 'C') |
104 | | - down = true; |
105 | | - } |
| 117 | + if (key == 'c' || key == 'C') |
| 118 | + down = true; |
| 119 | + } |
106 | 120 |
|
107 | | - public void onKeyReleased(char key) { |
108 | | - if (key == 'w' || key == 'W') |
109 | | - forward = false; |
| 121 | + public void onKeyReleased(char key) { |
| 122 | + if (key == 'w' || key == 'W') |
| 123 | + forward = false; |
110 | 124 |
|
111 | | - if (key == 's' || key == 'S') |
112 | | - back = false; |
| 125 | + if (key == 's' || key == 'S') |
| 126 | + back = false; |
113 | 127 |
|
114 | | - if (key == 'a' || key == 'A') |
115 | | - left = false; |
| 128 | + if (key == 'a' || key == 'A') |
| 129 | + left = false; |
116 | 130 |
|
117 | | - if (key == 'd' || key == 'D') |
118 | | - right = false; |
| 131 | + if (key == 'd' || key == 'D') |
| 132 | + right = false; |
119 | 133 |
|
120 | | - if (key == ' ') |
121 | | - up = false; |
| 134 | + if (key == ' ') |
| 135 | + up = false; |
122 | 136 |
|
123 | | - if (key == 'c' || key == 'C') |
124 | | - down = false; |
125 | | - } |
| 137 | + if (key == 'c' || key == 'C') |
| 138 | + down = false; |
| 139 | + } |
126 | 140 |
|
127 | | - public Matrix3f getRotationMatrix(float angle) { |
128 | | - Matrix3f m = new Matrix3f(Mathf.cos(angle), 0, Mathf.sin(angle), 0, 1, 0, -Mathf.sin(angle), 0, |
129 | | - Mathf.cos(angle)); |
130 | | - return m; |
131 | | - } |
| 141 | + public Matrix3f getRotationMatrix(float angle) { |
| 142 | + Matrix3f m = new Matrix3f( |
| 143 | + Mathf.cos(angle), 0, Mathf.sin(angle), 0, 1, 0, |
| 144 | + -Mathf.sin(angle), 0, Mathf.cos(angle) |
| 145 | + ); |
| 146 | + return m; |
| 147 | + } |
132 | 148 |
|
133 | | - public boolean isEnabled() { |
134 | | - return enabled; |
135 | | - } |
| 149 | + public boolean isEnabled() { |
| 150 | + return enabled; |
| 151 | + } |
136 | 152 |
|
137 | | - public void setEnabled(boolean enabled) { |
138 | | - this.enabled = enabled; |
139 | | - } |
| 153 | + public void setEnabled(boolean enabled) { |
| 154 | + this.enabled = enabled; |
| 155 | + } |
140 | 156 |
|
141 | 157 | } |
0 commit comments