Deterministic physics!

This commit is contained in:
yaw-man 2023-01-20 09:53:24 -04:00
parent 34f76c0b7c
commit 67dc751cf0
3 changed files with 16 additions and 9 deletions

View File

@ -22,11 +22,11 @@ state = {
Reset = function() Reset = function()
state.tick = 0 state.tick = 0
state.beat = {} state.beat = {}
state.startTime = love.timer.getTime()
state.currentBeat = 1 state.currentBeat = 1
state.timeToSimulate = 0.0 state.timeToSimulate = 0.0
state.longestStreak = 0 state.longestStreak = 0
state.currentStreak = 0 state.currentStreak = 0
state.lastCollisionTick = 0
end, end,
finishTime = 0.0, finishTime = 0.0,
@ -34,7 +34,6 @@ state = {
isGameStarted = false, isGameStarted = false,
lastBeatScore = 0.0, lastBeatScore = 0.0,
currentBeat = 1, currentBeat = 1,
startTime = 0.0,
longestStreak = 0, longestStreak = 0,
currentStreak = 0, currentStreak = 0,
@ -61,6 +60,7 @@ OnImpact = function( impact )
local score = BeatScore( impact.t ) local score = BeatScore( impact.t )
--DEBUG --DEBUG
state.lastBeatScore = score state.lastBeatScore = score
state.lastCollisionTick = state.tick
local pass = false local pass = false
if score > 1.0 then if score > 1.0 then
@ -210,7 +210,11 @@ end
OnVictory = function() OnVictory = function()
if state.isDemo then
state.isDemo = false
else
recorder.Save() recorder.Save()
end
particles:setParticleLifetime( 0, 30 ) particles:setParticleLifetime( 0, 30 )
particles:setSizes( 0.001, 0.0001, 0.0005 ) particles:setSizes( 0.001, 0.0001, 0.0005 )
@ -230,7 +234,7 @@ OnVictory = function()
love.graphics.setCanvas( marble.Canvas() ) love.graphics.setCanvas( marble.Canvas() )
love.graphics.setCanvas() love.graphics.setCanvas()
local totalTime = love.timer.getTime() - state.startTime local totalTime = state.tick / 120.0
OnImpact = function() end OnImpact = function() end
love.update = function( dt ) love.update = function( dt )
while dt > step do while dt > step do
@ -292,6 +296,10 @@ Draw = function()
sitelenpona.Draw( text.words[state.currentBeat] ) sitelenpona.Draw( text.words[state.currentBeat] )
marble.Draw() marble.Draw()
text.Draw( state.currentBeat ) 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 end

View File

@ -22,7 +22,7 @@ function marble.GetAcceleration( ) return ddx, ddy end
function marble.SetAcceleration( x, y ) ddx = x; ddy = y end function marble.SetAcceleration( x, y ) ddx = x; ddy = y end
function marble.Integrate( step ) function marble.Integrate( step )
newState.t = love.timer.getTime() newState.t = curState.t + step
newState.dx = (1.0 - INERTIA) * curState.dx + INERTIA * ddx newState.dx = (1.0 - INERTIA) * curState.dx + INERTIA * ddx
newState.dy = (1.0 - INERTIA) * curState.dy + INERTIA * ddy newState.dy = (1.0 - INERTIA) * curState.dy + INERTIA * ddy
newState.x = curState.x + newState.dx * step * MAXSPEED 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, xn = 0.2 * unx + x, yn = 0.2 * uny + y,
vxout = x + vxout, vyout = y + vyout}]] 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 end
function marble.Update() function marble.Update()
@ -87,7 +87,6 @@ function marble.Update()
end end
function marble.OnKey( tick ) 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) 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) 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. --Extrapolate forward for slightly smoother rendering.
local dt = love.timer.getTime() - curState.t 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 xp, yp = transform:transformPoint( oldState.x, oldState.y )
local xc, yc = transform:transformPoint( curState.x, curState.y ) local xc, yc = transform:transformPoint( curState.x, curState.y )

View File

@ -159,7 +159,7 @@ local function OnImpact( impact, level )
local r = cur.radii local r = cur.radii
local theta = impact.th local theta = impact.th
local magnitude = IMPULSESIZE * impact.speed 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 for i = 0, N - 1 do
r[ i + 1 ] = r[ i + 1 ] + dt * magnitude * AliasedSinc( theta, 2.0 * math.pi * i / N ) r[ i + 1 ] = r[ i + 1 ] + dt * magnitude * AliasedSinc( theta, 2.0 * math.pi * i / N )
end end