From 67dc751cf066ffb409aed61ecec1923aa4afec72 Mon Sep 17 00:00:00 2001 From: yaw-man Date: Fri, 20 Jan 2023 09:53:24 -0400 Subject: [PATCH] Deterministic physics! --- main.lua | 16 ++++++++++++---- marble.lua | 7 +++---- wave.lua | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/main.lua b/main.lua index c2b89d4..12dab25 100644 --- a/main.lua +++ b/main.lua @@ -22,11 +22,11 @@ state = { Reset = function() state.tick = 0 state.beat = {} - state.startTime = love.timer.getTime() state.currentBeat = 1 state.timeToSimulate = 0.0 state.longestStreak = 0 state.currentStreak = 0 + state.lastCollisionTick = 0 end, finishTime = 0.0, @@ -34,7 +34,6 @@ state = { isGameStarted = false, lastBeatScore = 0.0, currentBeat = 1, - startTime = 0.0, longestStreak = 0, currentStreak = 0, @@ -61,6 +60,7 @@ OnImpact = function( impact ) local score = BeatScore( impact.t ) --DEBUG state.lastBeatScore = score + state.lastCollisionTick = state.tick local pass = false if score > 1.0 then @@ -210,7 +210,11 @@ end OnVictory = function() - recorder.Save() + if state.isDemo then + state.isDemo = false + else + recorder.Save() + end particles:setParticleLifetime( 0, 30 ) particles:setSizes( 0.001, 0.0001, 0.0005 ) @@ -230,7 +234,7 @@ OnVictory = function() love.graphics.setCanvas( marble.Canvas() ) love.graphics.setCanvas() - local totalTime = love.timer.getTime() - state.startTime + local totalTime = state.tick / 120.0 OnImpact = function() end love.update = function( dt ) while dt > step do @@ -292,6 +296,10 @@ Draw = function() sitelenpona.Draw( text.words[state.currentBeat] ) marble.Draw() text.Draw( state.currentBeat ) + + love.graphics.print( state.tick, 400, 400, 0, 1, 1 ) + love.graphics.print( state.currentStreak, 400, 450, 0, 1, 1 ) + love.graphics.print( state.lastCollisionTick, 400, 500, 0, 1, 1 ) end diff --git a/marble.lua b/marble.lua index 06c8087..067a15d 100644 --- a/marble.lua +++ b/marble.lua @@ -22,7 +22,7 @@ function marble.GetAcceleration( ) return ddx, ddy end function marble.SetAcceleration( x, y ) ddx = x; ddy = y end function marble.Integrate( step ) - newState.t = love.timer.getTime() + newState.t = curState.t + step newState.dx = (1.0 - INERTIA) * curState.dx + INERTIA * ddx newState.dy = (1.0 - INERTIA) * curState.dy + INERTIA * ddy newState.x = curState.x + newState.dx * step * MAXSPEED @@ -75,7 +75,7 @@ function marble.OnImpact( impact, level ) xn = 0.2 * unx + x, yn = 0.2 * uny + y, vxout = x + vxout, vyout = y + vyout}]] - return marble.Integrate( math.max( impact.dt , 1 / 120 ) ) --Hmm! Maybe this should be a fixed step instead for stability's sake. + return marble.Integrate( 1 / 120.0 ) --Hmm! Maybe this should be a fixed step instead for stability's sake. end function marble.Update() @@ -87,7 +87,6 @@ function marble.Update() end - function marble.OnKey( tick ) ddx = (love.keyboard.isScancodeDown( "d" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "a" ) and 1.0 or 0.0) ddy = (love.keyboard.isScancodeDown( "w" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "s" ) and 1.0 or 0.0) @@ -126,7 +125,7 @@ function marble.Draw() --Extrapolate forward for slightly smoother rendering. local dt = love.timer.getTime() - curState.t - marble.Integrate( dt + 1 / 60.0 ) + marble.Integrate( 1 / 120.0 ) local xp, yp = transform:transformPoint( oldState.x, oldState.y ) local xc, yc = transform:transformPoint( curState.x, curState.y ) diff --git a/wave.lua b/wave.lua index 6c8f493..e6ab268 100644 --- a/wave.lua +++ b/wave.lua @@ -159,7 +159,7 @@ local function OnImpact( impact, level ) local r = cur.radii local theta = impact.th local magnitude = IMPULSESIZE * impact.speed - local dt = math.max( impact.dt, 1 / 120.0 ) + local dt = 1.0 / 120.0 for i = 0, N - 1 do r[ i + 1 ] = r[ i + 1 ] + dt * magnitude * AliasedSinc( theta, 2.0 * math.pi * i / N ) end