@@ -813,6 +813,54 @@ function updateCursorVisibility() {
813813 }
814814}
815815
816+ // ===========================================
817+ // BACKSPIN LOGIC - Reusable functions for spin effects
818+ // ===========================================
819+
820+ // Start a backspin effect with random axis
821+ function startBackspin ( ) {
822+ gameState . player . spinning = true ;
823+ gameState . player . spinProgress = 0 ;
824+
825+ // Generate a random spin axis (normalized)
826+ gameState . player . spinAxis
827+ . set (
828+ ( Math . random ( ) - 0.5 ) * 2 ,
829+ ( Math . random ( ) - 0.5 ) * 2 ,
830+ ( Math . random ( ) - 0.5 ) * 2
831+ )
832+ . normalize ( ) ;
833+
834+ // Play a higher pitched sound for trick landing
835+ createJumpSound ( ) ;
836+ }
837+
838+ // Update backspin animation (call this in the game loop)
839+ function updateBackspin ( ) {
840+ if ( ! gameState . player . spinning || ! copilotModel ) return ;
841+
842+ gameState . player . spinProgress += gameState . player . spinSpeed ;
843+
844+ // Apply easing function for smooth spin animation
845+ // Using smoothstep for ease-in-out effect: 3t² - 2t³
846+ const t = Math . min ( gameState . player . spinProgress , 1 ) ;
847+ const easedProgress = t * t * ( 3 - 2 * t ) ;
848+
849+ // Create rotation around the random axis with easing
850+ const spinAngle = easedProgress * Math . PI * 2 ; // Full 360 degree spin with easing
851+
852+ // Apply rotation around the random axis
853+ copilotModel . setRotationFromAxisAngle ( gameState . player . spinAxis , spinAngle ) ;
854+
855+ // Stop spinning after one full rotation
856+ if ( gameState . player . spinProgress >= 1 ) {
857+ gameState . player . spinning = false ;
858+ gameState . player . spinProgress = 0 ;
859+ // Reset rotation to identity
860+ copilotModel . rotation . set ( 0 , 0 , 0 ) ;
861+ }
862+ }
863+
816864// ===========================================
817865// GAME CONFIGURATION - Easy to change flags
818866// ===========================================
@@ -1322,20 +1370,7 @@ function checkPlatformCollision() {
13221370
13231371 // Start spin effect with random axis (only 20% chance) - play special sound
13241372 if ( Math . random ( ) < 0.2 ) {
1325- gameState . player . spinning = true ;
1326- gameState . player . spinProgress = 0 ;
1327-
1328- // Generate a random spin axis (normalized)
1329- gameState . player . spinAxis
1330- . set (
1331- ( Math . random ( ) - 0.5 ) * 2 ,
1332- ( Math . random ( ) - 0.5 ) * 2 ,
1333- ( Math . random ( ) - 0.5 ) * 2
1334- )
1335- . normalize ( ) ;
1336-
1337- // Play a higher pitched sound for trick landing
1338- createJumpSound ( ) ;
1373+ startBackspin ( ) ;
13391374 }
13401375
13411376 return ; // Exit after hitting any cube
@@ -1796,28 +1831,10 @@ function updateGame() {
17961831 copilotModel . position . y = gameState . player . position . y ;
17971832
17981833 // Handle spinning animation
1799- if ( gameState . player . spinning ) {
1800- gameState . player . spinProgress += gameState . player . spinSpeed ;
1801-
1802- // Apply easing function for smooth spin animation
1803- // Using smoothstep for ease-in-out effect: 3t² - 2t³
1804- const t = Math . min ( gameState . player . spinProgress , 1 ) ;
1805- const easedProgress = t * t * ( 3 - 2 * t ) ;
1834+ updateBackspin ( ) ;
18061835
1807- // Create rotation around the random axis with easing
1808- const spinAngle = easedProgress * Math . PI * 2 ; // Full 360 degree spin with easing
1809-
1810- // Apply rotation around the random axis
1811- copilotModel . setRotationFromAxisAngle ( gameState . player . spinAxis , spinAngle ) ;
1812-
1813- // Stop spinning after one full rotation
1814- if ( gameState . player . spinProgress >= 1 ) {
1815- gameState . player . spinning = false ;
1816- gameState . player . spinProgress = 0 ;
1817- // Reset rotation to identity
1818- copilotModel . rotation . set ( 0 , 0 , 0 ) ;
1819- }
1820- } else {
1836+ // Handle non-spinning model animation
1837+ if ( ! gameState . player . spinning ) {
18211838 // Make the head face the direction of movement (only when not spinning)
18221839 if ( Math . abs ( gameState . player . velocity . x ) > 0.01 ) {
18231840 // Smoothly rotate the head based on movement direction - increased rotation for more dramatic effect
0 commit comments