Skip to content

Commit cd32a0a

Browse files
committed
Added collision handling to Daydream example
1 parent 9d49330 commit cd32a0a

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

examples/webvr_daydream.html

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141
var room;
4242

43+
var radius = 0.08;
44+
var normal = new THREE.Vector3();
45+
var relativeVelocity = new THREE.Vector3();
46+
4347
init();
4448
animate();
4549

@@ -74,7 +78,7 @@
7478
light.position.set( 1, 1, 1 ).normalize();
7579
scene.add( light );
7680

77-
var geometry = new THREE.IcosahedronBufferGeometry( 0.08, 2 );
81+
var geometry = new THREE.IcosahedronBufferGeometry( radius, 2 );
7882

7983
for ( var i = 0; i < 200; i ++ ) {
8084

@@ -162,7 +166,7 @@
162166

163167
// keep cubes inside room
164168

165-
var range = 3 - 0.08;
169+
var range = 3 - radius;
166170

167171
for ( var i = 0; i < room.children.length; i ++ ) {
168172

@@ -177,7 +181,7 @@
177181

178182
}
179183

180-
if ( cube.position.y < - range ) {
184+
if ( cube.position.y < - range || cube.position.y > range ) {
181185

182186
cube.position.y = Math.max( cube.position.y, - range );
183187

@@ -194,6 +198,32 @@
194198

195199
}
196200

201+
for ( var j = i + 1; j < room.children.length; j ++ ) {
202+
203+
var cube2 = room.children[ j ];
204+
205+
normal.copy( cube.position ).sub( cube2.position );
206+
207+
if ( normal.length() < 2 * radius ) {
208+
209+
normal.multiplyScalar( 0.5 * normal.length() - radius );
210+
cube.position.sub( normal );
211+
cube2.position.add( normal );
212+
213+
normal.normalize();
214+
215+
relativeVelocity.copy( cube.userData.velocity ).sub( cube2.userData.velocity );
216+
var dot = relativeVelocity.dot( normal );
217+
218+
normal = normal.multiplyScalar( dot );
219+
220+
cube.userData.velocity.sub( normal );
221+
cube2.userData.velocity.add( normal );
222+
223+
}
224+
225+
}
226+
197227
cube.userData.velocity.y -= 0.00098;
198228

199229
}

examples/webvr_gearvr.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
var controller;
4343

4444
var room;
45-
45+
4646
var radius = 0.08;
4747
var normal = new THREE.Vector3();
4848
var relativeVelocity = new THREE.Vector3();
@@ -183,7 +183,7 @@
183183
cube.position.copy( controller.position ).sub( room.position );
184184
cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
185185
cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
186-
cube.userData.velocity.z = ( Math.random() * 0.01 - 0.1 );
186+
cube.userData.velocity.z = ( Math.random() * 0.02 - 0.1 );
187187
cube.userData.velocity.applyQuaternion( controller.quaternion );
188188
room.add( cube );
189189

@@ -222,15 +222,15 @@
222222
cube.userData.velocity.z = - cube.userData.velocity.z;
223223

224224
}
225-
225+
226226
for ( var j = i + 1; j < room.children.length; j ++ ) {
227-
227+
228228
var cube2 = room.children[ j ];
229-
229+
230230
normal.copy( cube.position ).sub( cube2.position );
231-
231+
232232
if ( normal.length() < 2 * radius ) {
233-
233+
234234
normal.multiplyScalar( 0.5 * normal.length() - radius );
235235
cube.position.sub( normal );
236236
cube2.position.add( normal );
@@ -246,7 +246,7 @@
246246
cube2.userData.velocity.add( normal );
247247

248248
}
249-
249+
250250
}
251251

252252
cube.userData.velocity.y -= 0.00098;

0 commit comments

Comments
 (0)