-
Notifications
You must be signed in to change notification settings - Fork 5
/
controller.js
333 lines (255 loc) · 9.9 KB
/
controller.js
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"use strict"
/* Copyright (c) 2014, Robert Buchholz <[email protected]>
The contents of this file are licensed under the GNU General Public License version 3
(see the LICENSE file in the project root for details)
*/
function getSign(pos, neg)
{
if (pos && (!neg)) return 1;
if (neg && (!pos)) return -1;
return 0;
}
var Controller = {
position: {lat: 0, lng: 0},
localPosition : { x:0, y:0, z: 1.5 }, //camera position in the local coordinate system ('z' is height)
viewAngleYaw : 0,
viewAnglePitch : 0,
getPosition : function()
{
return { "lat": Controller.position.lat, "lng": Controller.position.lng };
},
getEffectivePosition : function()
{
var metersPerDegreeLat = Helpers.getEarthCircumference() / 360;
var metersPerDegreeLng = metersPerDegreeLat * Math.cos( Controller.position.lat / 180 * Math.PI);
return {lat: Controller.position.lat + Controller.localPosition.y / metersPerDegreeLat,
lng: Controller.position.lng + Controller.localPosition.x / metersPerDegreeLng,
height: Controller.localPosition.z};
},
getLocalPosition: function()
{
return [Controller.localPosition.x, Controller.localPosition.y, Controller.localPosition.z];
},
buildQueryString: function()
{
var effPos = Controller.getEffectivePosition();
return "?lat=" + effPos.lat.toFixed(8) +
"&lng=" + effPos.lng.toFixed(8) +
"&yaw="+Controller.viewAngleYaw.toFixed(1)+
"&pitch="+Controller.viewAnglePitch.toFixed(1)+
"&height="+Controller.localPosition.z;
},
initFromQueryString: function(queryString)
{
var query = toDictionary(queryString);
if ("lat" in query && "lng" in query)
{
Controller.position = {lat:query["lat"], lng: query["lng"]};
if ("yaw" in query)
Controller.viewAngleYaw = query["yaw"];
if ("pitch" in query )
Controller.viewAnglePitch=query["pitch"];
if ("height" in query)
Controller.localPosition.z = query["height"];
}
},
onMouseDown: function(e)
{
if (e.button != 0)
return;
Controller.x = e.clientX;
Controller.y = e.clientY;
Controller.down = "mouse";
},
onMouseUp: function(e)
{
if (e.button != 0)
return;
Controller.down = null;
},
keysDown: {},
lastKeyEventProcessed: null,
turn: function(yaw, pitch)
{
Controller.viewAngleYaw = Controller.viewAngleYaw % 360;
Controller.viewAngleYaw += yaw;
Controller.viewAnglePitch += pitch;
if (Controller.viewAnglePitch > 60)
Controller.viewAnglePitch = 60;
if (Controller.viewAnglePitch < -60)
Controller.viewAnglePitch = -60;
},
move: function(dRight, dForward)
{
var arc = Controller.viewAngleYaw / 180 * Math.PI;
var forwardX = Math.sin(arc);
var forwardY = Math.cos(arc);
var rightX = Math.sin(arc + Math.PI/2.0);
var rightY = Math.cos(arc + Math.PI/2.0);
var dx = dRight*rightX + dForward*forwardX;
var dy = dRight*rightY + dForward*forwardY;
Controller.localPosition.x += dx;
Controller.localPosition.y += dy;
},
updateKeyInteraction: function()
{
var now = new Date().getTime();
//nothing to track anymore --> reset state
if (!Controller.keysStillPressed())
{
Controller.lastKeyEventProcessed = null;
return;
}
// Nothing tracked yet --> initialize state, but do nothing else.
// (there is not yet a 'dt' that we could base computations on)
if (Controller.lastKeyEventProcessed === null)
{
Controller.lastKeyEventProcessed = now;
return;
}
var dt = now - Controller.lastKeyEventProcessed;
Controller.lastKeyEventProcessed = now;
if (dt > 1000)
{
console.log("[WARN] extensive time step (%s ms)", dt);
dt = 1000;
}
var dy = dt/400 * getSign(("W" in Controller.keysDown) || ("up" in Controller.keysDown),
("S" in Controller.keysDown) || ("down" in Controller.keysDown));
var dx = dt/400 * getSign( "D" in Controller.keysDown, "A" in Controller.keysDown);
Controller.move(dx, dy);
var turnX = dt/10 * getSign( "right" in Controller.keysDown, "left" in Controller.keysDown );
var turnY = 0;
if (Controller.keysStillPressed() && Controller.down != "mouse" && Math.abs(Controller.viewAnglePitch) > 1)
turnY = (Controller.viewAnglePitch < 0 ? 1 : -1) * dt/40;
Controller.turn(turnX, turnY);
Controller.updateHistoryState();
},
x:null,
y:null,
//id of the touch event that is currently tracked as 'down'; "mouse" if the mouse is tracked, null if none
down: null,
onKeyDown: function(evt)
{
var key = null;
switch (evt.keyCode)
{
case 65: key = "A"; break;
case 68: key = "D"; break;
case 83: key = "S"; break;
case 87: key = "W"; break;
case 37: key = "left"; break;
case 38: key = "up"; break;
case 39: key = "right";break;
case 40: key = "down"; break;
}
if (key in Controller.keysDown) //is just a reoccuring event for a key that is still pressed
return;
if (key != null)
{
Controller.updateKeyInteraction();
Controller.keysDown[key] = key;
}
if (Controller.onRequestFrameRender)
Controller.onRequestFrameRender();
},
onKeyUp: function(evt)
{
switch (evt.keyCode)
{
case 65: delete Controller.keysDown["A"]; break;
case 68: delete Controller.keysDown["D"]; break;
case 83: delete Controller.keysDown["S"]; break;
case 87: delete Controller.keysDown["W"]; break;
case 37: delete Controller.keysDown["left"]; break;
case 38: delete Controller.keysDown["up"]; break;
case 39: delete Controller.keysDown["right"]; break;
case 40: delete Controller.keysDown["down"]; break;
}
Controller.updateKeyInteraction();
},
keysStillPressed: function()
{
return Object.keys(Controller.keysDown).length > 0;
},
onMouseMove: function(e)
{
//e.preventDefault();
if (Controller.down != "mouse" ) return;
var dx = e.clientX - Controller.x;
var dy = e.clientY - Controller.y;
Controller.x = e.clientX;
Controller.y = e.clientY;
Controller.turn(dx / 5.0, - dy / 5.0);
Controller.updateHistoryState();
if (Controller.onRequestFrameRender)
Controller.onRequestFrameRender();
},
getTouchData: function(touches, identifier)
{
if (identifier === null)
return null;
for (var i in touches)
{
if (touches[i].identifier == identifier)
{
return touches[i];
}
}
return null;
},
onTouchDown: function(ev)
{
ev.preventDefault();
var touch = ev.changedTouches[0];
Controller.down = touch.identifier;
Controller.x = touch.clientX;
Controller.y = touch.clientY;
//errorLog.textContent = "Touched down at (" + touch.identifier + ", " + touch.clientX + ", " + touch.clientY + ") - " + Controller.down;
},
onTouchEnd: function(ev)
{
ev.preventDefault();
Controller.down = null;
},
onTouchMove: function(ev)
{
ev.preventDefault();
//errorLog.textContent = "Touch move";
var touch = Controller.getTouchData(ev.changedTouches, Controller.down);
if (touch === null)
return;
var dx = touch.clientX - Controller.x;
var dy = touch.clientY - Controller.y;
Controller.x = touch.clientX;
Controller.y = touch.clientY;
//errorLog.textContent = "Touch move with down=" + Controller.down + ", delta =(" + dx + ", " + dy + ")";
//Controller.move(0, -dy / 100.0);
//Controller.turn(dx / 5.0, 0 );
Controller.turn(dx / 5.0, -dy/5.0 );
Controller.updateHistoryState();
if (Controller.onRequestFrameRender)
Controller.onRequestFrameRender();
},
updateTimeoutId: null,
// schedules a history state update so that the update is performed once no
// update request has been made for a second (1000ms). This keeps the history state
// reasonably up-to-date while preventing excessive history state updates (which incur
// a performance penalty at least in Firefox).
updateHistoryState : function()
{
if (Controller.updateTimeoutId)
window.clearTimeout(Controller.updateTimeoutId);
Controller.updateTimeoutId = window.setTimeout( function()
{
var url = document.URL;
if (url.indexOf("?") >= 0) // already contains a query string --> remove it
url = url.substring(0, url.indexOf("?"));
var metersPerDegreeLat = Helpers.getEarthCircumference() / 360;
var metersPerDegreeLng = metersPerDegreeLat * Math.cos( Controller.position.lat / 180 * Math.PI);
//console.log("Resolution x: %s m/°, y: %s m/°", metersPerDegreeLng, metersPerDegreeLat);
url += Controller.buildQueryString();
history.replaceState(null, document.title, url);
},1000);
}
}