Răsfoiți Sursa

fps example: collision detection in substeps (#21925)

* fps example: collision detection in substeps

* Update games_fps.html

Clean up.

Co-authored-by: Felix Mariotto <[email protected]>
Co-authored-by: Michael Herzog <[email protected]>
Felix Mariotto 4 ani în urmă
părinte
comite
d45c0e1b08
1 a modificat fișierele cu 13 adăugiri și 4 ștergeri
  1. 13 4
      examples/games_fps.html

+ 13 - 4
examples/games_fps.html

@@ -77,6 +77,8 @@
 			const NUM_SPHERES = 20;
 			const NUM_SPHERES = 20;
 			const SPHERE_RADIUS = 0.2;
 			const SPHERE_RADIUS = 0.2;
 
 
+			const STEPS_PER_FRAME = 5;
+
 			const sphereGeometry = new THREE.SphereGeometry( SPHERE_RADIUS, 32, 32 );
 			const sphereGeometry = new THREE.SphereGeometry( SPHERE_RADIUS, 32, 32 );
 			const sphereMaterial = new THREE.MeshStandardMaterial( { color: 0x888855, roughness: 0.8, metalness: 0.5 } );
 			const sphereMaterial = new THREE.MeshStandardMaterial( { color: 0x888855, roughness: 0.8, metalness: 0.5 } );
 
 
@@ -360,13 +362,20 @@
 
 
 			function animate() {
 			function animate() {
 
 
-				const deltaTime = Math.min( 0.1, clock.getDelta() );
+				const deltaTime = Math.min( 0.05, clock.getDelta() ) / STEPS_PER_FRAME;
+
+				// we look for collisions in substeps to mitigate the risk of
+				// an object traversing another too quickly for detection.
+
+				for ( let i = 0 ; i < STEPS_PER_FRAME ; i ++ ) {
 
 
-				controls( deltaTime );
+					controls( deltaTime );
 
 
-				updatePlayer( deltaTime );
+					updatePlayer( deltaTime );
 
 
-				updateSpheres( deltaTime );
+					updateSpheres( deltaTime );
+
+				}
 
 
 				renderer.render( scene, camera );
 				renderer.render( scene, camera );