Browse Source

Merge pull request #14026 from paulmasson/webvr

Add collision handling to GearVR example
Mr.doob 7 năm trước cách đây
mục cha
commit
9d49330a58
1 tập tin đã thay đổi với 32 bổ sung2 xóa
  1. 32 2
      examples/webvr_gearvr.html

+ 32 - 2
examples/webvr_gearvr.html

@@ -42,6 +42,10 @@
 			var controller;
 
 			var room;
+			
+			var radius = 0.08;
+			var normal = new THREE.Vector3();
+			var relativeVelocity = new THREE.Vector3();
 
 			init();
 			animate();
@@ -77,7 +81,7 @@
 				light.position.set( 1, 1, 1 ).normalize();
 				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 ++ ) {
 
@@ -187,7 +191,7 @@
 
 				// keep cubes inside room
 
-				var range = 3 - 0.08;
+				var range = 3 - radius;
 
 				for ( var i = 0; i < room.children.length; i ++ ) {
 
@@ -218,6 +222,32 @@
 						cube.userData.velocity.z = - cube.userData.velocity.z;
 
 					}
+					
+					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;