Explorar o código

Added collision handling to Daydream example

Mr.doob %!s(int64=7) %!d(string=hai) anos
pai
achega
cd32a0ad23
Modificáronse 2 ficheiros con 41 adicións e 11 borrados
  1. 33 3
      examples/webvr_daydream.html
  2. 8 8
      examples/webvr_gearvr.html

+ 33 - 3
examples/webvr_daydream.html

@@ -40,6 +40,10 @@
 
 
 			var room;
 			var room;
 
 
+			var radius = 0.08;
+			var normal = new THREE.Vector3();
+			var relativeVelocity = new THREE.Vector3();
+
 			init();
 			init();
 			animate();
 			animate();
 
 
@@ -74,7 +78,7 @@
 				light.position.set( 1, 1, 1 ).normalize();
 				light.position.set( 1, 1, 1 ).normalize();
 				scene.add( light );
 				scene.add( light );
 
 
-				var geometry = new THREE.IcosahedronBufferGeometry( 0.08, 2 );
+				var geometry = new THREE.IcosahedronBufferGeometry( radius, 2 );
 
 
 				for ( var i = 0; i < 200; i ++ ) {
 				for ( var i = 0; i < 200; i ++ ) {
 
 
@@ -162,7 +166,7 @@
 
 
 				// keep cubes inside room
 				// keep cubes inside room
 
 
-				var range = 3 - 0.08;
+				var range = 3 - radius;
 
 
 				for ( var i = 0; i < room.children.length; i ++ ) {
 				for ( var i = 0; i < room.children.length; i ++ ) {
 
 
@@ -177,7 +181,7 @@
 
 
 					}
 					}
 
 
-					if ( cube.position.y < - range ) {
+					if ( cube.position.y < - range || cube.position.y > range ) {
 
 
 						cube.position.y = Math.max( cube.position.y, - range );
 						cube.position.y = Math.max( cube.position.y, - range );
 
 
@@ -194,6 +198,32 @@
 
 
 					}
 					}
 
 
+					for ( var j = i + 1; j < room.children.length; j ++ ) {
+
+						var cube2 = room.children[ j ];
+
+						normal.copy( cube.position ).sub( cube2.position );
+
+						if ( normal.length() < 2 * radius ) {
+
+							normal.multiplyScalar( 0.5 * normal.length() - radius );
+							cube.position.sub( normal );
+							cube2.position.add( normal );
+
+							normal.normalize();
+
+							relativeVelocity.copy( cube.userData.velocity ).sub( cube2.userData.velocity );
+							var dot = relativeVelocity.dot( normal );
+
+							normal = normal.multiplyScalar( dot );
+
+							cube.userData.velocity.sub( normal );
+							cube2.userData.velocity.add( normal );
+
+						}
+
+					}
+
 					cube.userData.velocity.y -= 0.00098;
 					cube.userData.velocity.y -= 0.00098;
 
 
 				}
 				}

+ 8 - 8
examples/webvr_gearvr.html

@@ -42,7 +42,7 @@
 			var controller;
 			var controller;
 
 
 			var room;
 			var room;
-			
+
 			var radius = 0.08;
 			var radius = 0.08;
 			var normal = new THREE.Vector3();
 			var normal = new THREE.Vector3();
 			var relativeVelocity = new THREE.Vector3();
 			var relativeVelocity = new THREE.Vector3();
@@ -183,7 +183,7 @@
 					cube.position.copy( controller.position ).sub( room.position );
 					cube.position.copy( controller.position ).sub( room.position );
 					cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
 					cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
 					cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
 					cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
-					cube.userData.velocity.z = ( Math.random() * 0.01 - 0.1 );
+					cube.userData.velocity.z = ( Math.random() * 0.02 - 0.1 );
 					cube.userData.velocity.applyQuaternion( controller.quaternion );
 					cube.userData.velocity.applyQuaternion( controller.quaternion );
 					room.add( cube );
 					room.add( cube );
 
 
@@ -222,15 +222,15 @@
 						cube.userData.velocity.z = - cube.userData.velocity.z;
 						cube.userData.velocity.z = - cube.userData.velocity.z;
 
 
 					}
 					}
-					
+
 					for ( var j = i + 1; j < room.children.length; j ++ ) {
 					for ( var j = i + 1; j < room.children.length; j ++ ) {
-						
+
 						var cube2 = room.children[ j ];
 						var cube2 = room.children[ j ];
-						
+
 						normal.copy( cube.position ).sub( cube2.position );
 						normal.copy( cube.position ).sub( cube2.position );
-						
+
 						if ( normal.length() < 2 * radius ) {
 						if ( normal.length() < 2 * radius ) {
-							
+
 							normal.multiplyScalar( 0.5 * normal.length() - radius );
 							normal.multiplyScalar( 0.5 * normal.length() - radius );
 							cube.position.sub( normal );
 							cube.position.sub( normal );
 							cube2.position.add( normal );
 							cube2.position.add( normal );
@@ -246,7 +246,7 @@
 							cube2.userData.velocity.add( normal );
 							cube2.userData.velocity.add( normal );
 
 
 						}
 						}
-						
+
 					}
 					}
 
 
 					cube.userData.velocity.y -= 0.00098;
 					cube.userData.velocity.y -= 0.00098;