Pim Schreurs commited on
Commit
33be52b
·
1 Parent(s): 49547bd

Add teleport functionality

Browse files
index.html CHANGED
@@ -44,6 +44,9 @@
44
  padding: 5px;
45
  text-align: center;
46
  z-index: 10;
 
 
 
47
  pointer-events: none;
48
  }
49
 
@@ -98,7 +101,7 @@
98
  color: #0080ff;
99
  }
100
 
101
- kbd { color: orange }
102
 
103
  #container {
104
  position: fixed;
@@ -143,8 +146,8 @@
143
  <body>
144
 
145
  <div id="info">
146
- <h3>Controls:</h3>
147
- <div id="keyboard-controls">
148
  <kbd>WASD</kbd> move,
149
  <kbd>R|F</kbd> up | down,
150
  <kbd>Q|E</kbd> roll,
@@ -152,10 +155,12 @@
152
  <kbd>left|right</kbd> yaw
153
  <kbd>shift</kbd> speed up<br>
154
  <kbd>space/esc</kbd> toggle mouse controls<br>
 
155
  Scroll to increase/decrease your field of view.
156
  </div>
157
  <div id="mobile-device-controls">
158
- Point your device around you to look around. Touch the screen to move forwards. Use two fingers to go faster.
 
159
  </div>
160
  </div>
161
 
 
44
  padding: 5px;
45
  text-align: center;
46
  z-index: 10;
47
+ }
48
+
49
+ .no-pointer-events {
50
  pointer-events: none;
51
  }
52
 
 
101
  color: #0080ff;
102
  }
103
 
104
+ kbd, #teleport { color: orange }
105
 
106
  #container {
107
  position: fixed;
 
146
  <body>
147
 
148
  <div id="info">
149
+ <h3 class="no-pointer-events">Controls:</h3>
150
+ <div id="keyboard-controls" class="no-pointer-events">
151
  <kbd>WASD</kbd> move,
152
  <kbd>R|F</kbd> up | down,
153
  <kbd>Q|E</kbd> roll,
 
155
  <kbd>left|right</kbd> yaw
156
  <kbd>shift</kbd> speed up<br>
157
  <kbd>space/esc</kbd> toggle mouse controls<br>
158
+ <kbd>T</kbd> Teleport to next object<br>
159
  Scroll to increase/decrease your field of view.
160
  </div>
161
  <div id="mobile-device-controls">
162
+ <div class="no-pointer-events">Point your device around you to look around. Touch the screen to move forwards. Use two fingers to go faster.</div>
163
+ <div id="teleport">Teleport to next object</div>
164
  </div>
165
  </div>
166
 
js/Player.js CHANGED
@@ -11,13 +11,23 @@ function Player()
11
  this.galaxy = 0;
12
 
13
  this.controls = [];
 
 
14
  }
15
 
16
  Player.prototype = {
17
 
 
 
 
 
18
  lookAt: function(position)
19
  {
20
- this.eyes.lookAt(position);
 
 
 
 
21
  },
22
 
23
  handleInput: function()
@@ -107,5 +117,37 @@ Player.prototype = {
107
 
108
  // Object isn't actually part of a rendered scene, so we need to call this manually
109
  this.object.updateMatrixWorld(true);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  }
111
- };
 
 
11
  this.galaxy = 0;
12
 
13
  this.controls = [];
14
+
15
+ this.teleportTargets = [];
16
  }
17
 
18
  Player.prototype = {
19
 
20
+ addTeleportTarget: function(target) {
21
+ this.teleportTargets.push(target)
22
+ },
23
+
24
  lookAt: function(position)
25
  {
26
+ // this.object.lookAt makes it look in the exact opposite direction, for some reason
27
+ var lookAtMatrix = new THREE.Matrix4();
28
+ lookAtMatrix.lookAt(this.object.position, position, this.object.up);
29
+ this.object.quaternion.setFromRotationMatrix(lookAtMatrix);
30
+ this.object.quaternion.multiply(this.eyes.quaternion.clone().inverse());
31
  },
32
 
33
  handleInput: function()
 
117
 
118
  // Object isn't actually part of a rendered scene, so we need to call this manually
119
  this.object.updateMatrixWorld(true);
120
+ },
121
+
122
+ getClosestTeleportIndex: function() {
123
+ var minDistance = Infinity;
124
+ var result = null;
125
+ for (var i = 0; i < this.teleportTargets.length; i++) {
126
+ var distance;
127
+
128
+ if (this.teleportTargets[i].galaxy != this.galaxy) {
129
+ distance =
130
+ this.object.position.distanceTo(Simulation.wormholePositionSize) +
131
+ this.teleportTargets[i].position.distanceTo(Simulation.wormholePositionSize);
132
+ } else {
133
+ distance = this.object.position.distanceTo(this.teleportTargets[i].position);
134
+ }
135
+
136
+ if (distance < minDistance) {
137
+ minDistance = distance;
138
+ result = i;
139
+ }
140
+ }
141
+ return result;
142
+ },
143
+
144
+ teleport: function() {
145
+ var nextIndex = (this.getClosestTeleportIndex() + 1) % this.teleportTargets.length;
146
+ var teleportTarget = this.teleportTargets[nextIndex];
147
+
148
+ this.object.position.copy(teleportTarget.position);
149
+ this.lookAt(teleportTarget.lookAt);
150
+ this.galaxy = teleportTarget.galaxy;
151
  }
152
+
153
+ };
js/Simulation.js CHANGED
@@ -127,6 +127,13 @@ var Simulation = {
127
  this.planetPositionSize.y += this.blackholePositionSize.y;
128
  this.planetPositionSize.z += this.blackholePositionSize.z;
129
 
 
 
 
 
 
 
 
130
  // Ring definition - xyz is normal going through ring. Its magnitude determines inner radius.
131
  // w component determines outer radius
132
  this.blackholeDisk = new THREE.Vector4(-12, 12, 6, 150.0);
@@ -221,12 +228,17 @@ var Simulation = {
221
  this.player = new Player;
222
  this.player.lookAt(this.wormholePositionSize);
223
 
 
 
 
 
224
  // Add keyboard controls to the player
225
  this.keyboardControls = new KeyboardControls(this.player, this.container);
226
  this.keyboardControls.movementSpeed = 1;
227
  this.keyboardControls.rollSpeed = Math.PI / 3;
228
  this.keyboardControls.autoForward = false;
229
  this.keyboardControls.dragToLook = false;
 
230
 
231
  this.player.controls.push(this.keyboardControls);
232
 
@@ -261,14 +273,16 @@ var Simulation = {
261
 
262
  window.addEventListener("keypress", keypress, false);
263
 
264
- // Disable mobile device controls until we get some indication that we're on an orientable device
265
- this.mobileDeviceControls.disconnect();
266
-
267
  var deviceListener = function(event) {
268
  if (event.alpha === null) return;
269
 
270
  self.mobileDeviceControls.connect();
271
 
 
 
 
 
 
272
  window.removeEventListener("deviceorientation", deviceListener, false);
273
 
274
  document.body.classList.add("mobile-device");
@@ -328,4 +342,4 @@ var Simulation = {
328
  this.renderer.clear();
329
  this.composer.render();
330
  },
331
- };
 
127
  this.planetPositionSize.y += this.blackholePositionSize.y;
128
  this.planetPositionSize.z += this.blackholePositionSize.z;
129
 
130
+ this.teleportTargets = [
131
+ { position: new THREE.Vector3(10, -307, 454), lookAt: this.blackholePositionSize, galaxy: 1 },
132
+ { position: new THREE.Vector3(7.2, -188, 199.6), lookAt: this.planetPositionSize, galaxy: 1 },
133
+ { position: new THREE.Vector3(12.4, 3.3, -35.1), lookAt: this.wormholePositionSize, galaxy: 1 },
134
+ { position: new THREE.Vector3(9.8, -4.6, -3.1), lookAt: this.wormholePositionSize, galaxy: 0 }
135
+ ];
136
+
137
  // Ring definition - xyz is normal going through ring. Its magnitude determines inner radius.
138
  // w component determines outer radius
139
  this.blackholeDisk = new THREE.Vector4(-12, 12, 6, 150.0);
 
228
  this.player = new Player;
229
  this.player.lookAt(this.wormholePositionSize);
230
 
231
+ for (var i = 0; i < this.teleportTargets.length; i++) {
232
+ this.player.addTeleportTarget(this.teleportTargets[i]);
233
+ }
234
+
235
  // Add keyboard controls to the player
236
  this.keyboardControls = new KeyboardControls(this.player, this.container);
237
  this.keyboardControls.movementSpeed = 1;
238
  this.keyboardControls.rollSpeed = Math.PI / 3;
239
  this.keyboardControls.autoForward = false;
240
  this.keyboardControls.dragToLook = false;
241
+ this.keyboardControls.connect();
242
 
243
  this.player.controls.push(this.keyboardControls);
244
 
 
273
 
274
  window.addEventListener("keypress", keypress, false);
275
 
 
 
 
276
  var deviceListener = function(event) {
277
  if (event.alpha === null) return;
278
 
279
  self.mobileDeviceControls.connect();
280
 
281
+ // The player will probably not be looking with their device in the right direction, so fix that
282
+ requestAnimationFrame(function () {
283
+ self.player.object.quaternion.multiply(self.player.eyes.quaternion.clone().inverse())
284
+ })
285
+
286
  window.removeEventListener("deviceorientation", deviceListener, false);
287
 
288
  document.body.classList.add("mobile-device");
 
342
  this.renderer.clear();
343
  this.composer.render();
344
  },
345
+ };
js/controls/DeviceOrientationControls.js CHANGED
@@ -85,6 +85,4 @@ THREE.DeviceOrientationControls = function ( object ) {
85
 
86
  };
87
 
88
- this.connect();
89
-
90
  };
 
85
 
86
  };
87
 
 
 
88
  };
js/controls/KeyboardControls.js CHANGED
@@ -1,6 +1,7 @@
1
  function KeyboardControls(player, element)
2
  {
3
  this.player = player;
 
4
 
5
  element.setAttribute('tabindex', -1);
6
 
@@ -96,6 +97,10 @@ function KeyboardControls(player, element)
96
  case 69: // E
97
  self.moveState.rollRight = 1;
98
  break;
 
 
 
 
99
  }
100
 
101
  updateMovementVector();
@@ -316,6 +321,4 @@ function KeyboardControls(player, element)
316
  this.player.velocity.copy(this.moveVector).multiplyScalar(moveMult).applyQuaternion(this.player.eyes.getWorldQuaternion());
317
  this.player.eyeAngularVelocity.copy(this.rotationVector).multiplyScalar(rotMult);
318
  };
319
-
320
- this.connect();
321
  }
 
1
  function KeyboardControls(player, element)
2
  {
3
  this.player = player;
4
+ this.teleport = this.player.teleport.bind(this.player);
5
 
6
  element.setAttribute('tabindex', -1);
7
 
 
97
  case 69: // E
98
  self.moveState.rollRight = 1;
99
  break;
100
+
101
+ case 84: // T
102
+ self.teleport();
103
+ break;
104
  }
105
 
106
  updateMovementVector();
 
321
  this.player.velocity.copy(this.moveVector).multiplyScalar(moveMult).applyQuaternion(this.player.eyes.getWorldQuaternion());
322
  this.player.eyeAngularVelocity.copy(this.rotationVector).multiplyScalar(rotMult);
323
  };
 
 
324
  }
js/controls/MobileDeviceControls.js CHANGED
@@ -8,6 +8,13 @@ function MobileDeviceControls(player, element)
8
 
9
  var self = this;
10
 
 
 
 
 
 
 
 
11
  function onTouchEvent(event)
12
  {
13
  event.preventDefault();
@@ -48,6 +55,12 @@ function MobileDeviceControls(player, element)
48
  element.addEventListener( 'touchmove', onTouchEvent, false );
49
  element.addEventListener( 'touchend', onTouchEvent, false );
50
 
 
 
 
 
 
 
51
  this.enabled = true;
52
  };
53
 
@@ -58,6 +71,12 @@ function MobileDeviceControls(player, element)
58
  element.removeEventListener( 'touchmove', onTouchEvent, false );
59
  element.removeEventListener( 'touchend', onTouchEvent, false );
60
 
 
 
 
 
 
 
61
  this.enabled = false;
62
  };
63
 
@@ -67,6 +86,4 @@ function MobileDeviceControls(player, element)
67
  this.player.velocity.copy(this.velocity).applyQuaternion(this.player.eyes.getWorldQuaternion());
68
  this.deviceOrientationControls.update();
69
  };
70
-
71
- this.connect();
72
  }
 
8
 
9
  var self = this;
10
 
11
+ function teleport(event)
12
+ {
13
+ event.preventDefault();
14
+
15
+ player.teleport();
16
+ }
17
+
18
  function onTouchEvent(event)
19
  {
20
  event.preventDefault();
 
55
  element.addEventListener( 'touchmove', onTouchEvent, false );
56
  element.addEventListener( 'touchend', onTouchEvent, false );
57
 
58
+ document.querySelector( '#teleport' ).addEventListener(
59
+ 'touchstart',
60
+ teleport,
61
+ false
62
+ );
63
+
64
  this.enabled = true;
65
  };
66
 
 
71
  element.removeEventListener( 'touchmove', onTouchEvent, false );
72
  element.removeEventListener( 'touchend', onTouchEvent, false );
73
 
74
+ document.querySelector( '#teleport' ).removeEventListener(
75
+ 'touchstart',
76
+ teleport,
77
+ false
78
+ );
79
+
80
  this.enabled = false;
81
  };
82
 
 
86
  this.player.velocity.copy(this.velocity).applyQuaternion(this.player.eyes.getWorldQuaternion());
87
  this.deviceOrientationControls.update();
88
  };
 
 
89
  }