2023-01-13 20:05:19 +00:00
|
|
|
--Character controller. Renders point particle.
|
|
|
|
local love = love
|
2023-01-15 20:46:32 +00:00
|
|
|
local marble = {}
|
2023-01-13 21:34:01 +00:00
|
|
|
local oldBuffer = love.graphics.newCanvas()
|
|
|
|
local newBuffer = love.graphics.newCanvas()
|
2023-01-14 13:48:52 +00:00
|
|
|
local oldState, curState, newState
|
|
|
|
|
2023-01-16 02:38:15 +00:00
|
|
|
local INERTIA = 0.01
|
|
|
|
local MAXSPEED = 6
|
2023-01-14 13:48:52 +00:00
|
|
|
local ddx, ddy = 0.0, 0.0
|
|
|
|
|
|
|
|
local function State( )
|
|
|
|
return { t = 0, x = 0, y = 0, dx = 0, dy = 0 }
|
|
|
|
end
|
2023-01-13 20:05:19 +00:00
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
function marble.Current() return curState end
|
|
|
|
|
|
|
|
function marble.Next() return newState end
|
2023-01-14 13:48:52 +00:00
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
function marble.GetAcceleration( ) return ddx, ddy end
|
|
|
|
|
2023-01-18 22:51:03 +00:00
|
|
|
function marble.SetAcceleration( x, y ) ddx = x; ddy = y end
|
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
function marble.Integrate( step )
|
2023-01-14 13:48:52 +00:00
|
|
|
newState.t = love.timer.getTime()
|
2023-01-16 02:38:15 +00:00
|
|
|
newState.dx = (1.0 - INERTIA) * curState.dx + INERTIA * ddx
|
|
|
|
newState.dy = (1.0 - INERTIA) * curState.dy + INERTIA * ddy
|
2023-01-14 13:48:52 +00:00
|
|
|
newState.x = curState.x + newState.dx * step * MAXSPEED
|
|
|
|
newState.y = curState.y + newState.dy * step * MAXSPEED
|
|
|
|
end
|
2023-01-13 20:05:19 +00:00
|
|
|
|
2023-01-16 02:38:15 +00:00
|
|
|
function marble.OnImpact( impact, level )
|
2023-01-14 13:48:52 +00:00
|
|
|
--Adjust current trajectory according to collision.
|
2023-01-14 14:54:32 +00:00
|
|
|
if not impact.dt then return end
|
2023-01-14 18:47:46 +00:00
|
|
|
|
2023-01-16 04:38:47 +00:00
|
|
|
INERTIA = 0.03 - 0.02 * math.sqrt( level / 120.0 )
|
2023-01-16 02:38:15 +00:00
|
|
|
MAXSPEED = 2.0 + 4.0 * ( level / 120.0 )
|
|
|
|
|
2023-01-14 18:47:46 +00:00
|
|
|
local x, y = impact.r * math.cos( impact.th ), impact.r * math.sin( impact.th )
|
|
|
|
local vx, vy = newState.dx, newState.dy --Velocity of particle going into collision.
|
|
|
|
local unx, uny = math.cos( impact.normal ), math.sin( impact.normal ) --Outward-facing normal of wave.
|
|
|
|
local uvx, uvy --Unit vector velocity of particle.
|
|
|
|
local speed = math.sqrt( vx * vx + vy * vy )
|
|
|
|
if speed < 0.01 then
|
|
|
|
--If the marble is motionless, there is no angular velocity wrt 0,
|
|
|
|
--so the wave is headed directly inward.
|
|
|
|
--We handle the collision as if the marble is headed directly outward.
|
|
|
|
uvx, uvy = unx, uny
|
|
|
|
vx, vy = uvx, uvy
|
|
|
|
else
|
|
|
|
uvx, uvy = vx / speed , vy / speed
|
|
|
|
end
|
2023-01-16 02:38:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-01-14 18:47:46 +00:00
|
|
|
--Get signed angle between normal and incoming velocity (both unit vectors)
|
|
|
|
local dot = unx * uvy - uny * uvx
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-14 18:47:46 +00:00
|
|
|
--Fudge factor: apply an impulse inward so that you don't stick or slide on the wave.
|
|
|
|
local inward = ( dot > 0 ) and dot or -dot
|
|
|
|
inward = inward * inward * inward
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-14 18:47:46 +00:00
|
|
|
--Calculate the rotation matrix:
|
|
|
|
--counterclockwise rotation by 2 * pi - 2 * arccos( n dot v )
|
|
|
|
local c, s = 1 - 2 * dot * dot, - 2 * dot * math.sqrt( 1 - dot * dot )
|
|
|
|
--Apply:
|
|
|
|
local vxout, vyout =
|
|
|
|
inward * (- math.cos(impact.th) ) - (1.0 - inward) * ( vx * c - vy * s ),
|
|
|
|
inward * (- math.sin(impact.th) ) - (1.0 - inward) * ( x * s + vy * c )
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-14 18:47:46 +00:00
|
|
|
curState.x, curState.y = x, y
|
2023-01-15 13:39:58 +00:00
|
|
|
curState.dx, curState.dy = vxout, vyout
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
--[[debugRenderImpact = { xi = x, yi = y, xf = x - vx, yf = y - vy,
|
2023-01-14 18:47:46 +00:00
|
|
|
xn = 0.2 * unx + x, yn = 0.2 * uny + y,
|
2023-01-15 20:46:32 +00:00
|
|
|
vxout = x + vxout, vyout = y + vyout}]]
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
return marble.Integrate( math.max( impact.dt , 1 / 120 ) ) --Hmm! Maybe this should be a fixed step instead for stability's sake.
|
2023-01-14 12:43:54 +00:00
|
|
|
end
|
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
function marble.Update()
|
2023-01-14 13:48:52 +00:00
|
|
|
--Roll the log.
|
|
|
|
for k, v in pairs( oldState ) do
|
|
|
|
oldState[k] = curState[k]
|
|
|
|
curState[k] = newState[k]
|
|
|
|
end
|
2023-01-14 12:43:54 +00:00
|
|
|
end
|
|
|
|
|
2023-01-13 20:05:19 +00:00
|
|
|
|
|
|
|
|
2023-01-18 22:51:03 +00:00
|
|
|
function marble.OnKey( tick )
|
2023-01-13 20:05:19 +00:00
|
|
|
ddx = (love.keyboard.isScancodeDown( "d" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "a" ) and 1.0 or 0.0)
|
2023-01-14 03:46:39 +00:00
|
|
|
ddy = (love.keyboard.isScancodeDown( "w" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "s" ) and 1.0 or 0.0)
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-18 22:51:03 +00:00
|
|
|
--print( tick, ddx, ddy )
|
2023-01-13 20:05:19 +00:00
|
|
|
local n = math.sqrt( ddx * ddx + ddy * ddy )
|
|
|
|
if n < 0.001 then return end
|
|
|
|
ddx, ddy = ddx / n, ddy / n
|
|
|
|
end
|
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
function marble.OnVictory()
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-16 01:47:14 +00:00
|
|
|
--[[marble.Draw = function()
|
2023-01-15 20:46:32 +00:00
|
|
|
|
|
|
|
--Extrapolate forward for slightly smoother rendering.
|
|
|
|
local dt = love.timer.getTime() - curState.t
|
|
|
|
marble.Integrate( dt + 1 / 60.0 )
|
|
|
|
|
|
|
|
local xp, yp = transform:transformPoint( oldState.x, oldState.y )
|
|
|
|
local xc, yc = transform:transformPoint( curState.x, curState.y )
|
|
|
|
local xn, yn = transform:transformPoint( newState.x, newState.y )
|
|
|
|
|
|
|
|
love.graphics.setColor( 91 / 255, 206 / 255, 250 / 255, 1.0 ) --Trans blue.
|
|
|
|
love.graphics.setLineJoin( "bevel" )
|
|
|
|
love.graphics.setLineStyle( "smooth" )
|
|
|
|
love.graphics.setLineWidth( 8.0 )
|
|
|
|
love.graphics.line( xp, yp, xc, yc, xn, yn)
|
|
|
|
love.graphics.circle( "fill", xn, yn, 4.0 )
|
|
|
|
love.graphics.setCanvas()
|
|
|
|
|
|
|
|
|
2023-01-16 01:47:14 +00:00
|
|
|
end]]
|
2023-01-15 20:46:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function marble.Draw()
|
2023-01-14 13:59:20 +00:00
|
|
|
|
|
|
|
--Extrapolate forward for slightly smoother rendering.
|
|
|
|
local dt = love.timer.getTime() - curState.t
|
2023-01-15 20:46:32 +00:00
|
|
|
marble.Integrate( dt + 1 / 60.0 )
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-14 13:48:52 +00:00
|
|
|
local xp, yp = transform:transformPoint( oldState.x, oldState.y )
|
|
|
|
local xc, yc = transform:transformPoint( curState.x, curState.y )
|
|
|
|
local xn, yn = transform:transformPoint( newState.x, newState.y )
|
2023-01-14 03:46:39 +00:00
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
love.graphics.setCanvas( newBuffer )
|
2023-01-14 03:46:39 +00:00
|
|
|
love.graphics.setColor( 1.0, 1.0, 1.0 , 0.99 ) -- White.
|
2023-01-13 21:34:01 +00:00
|
|
|
love.graphics.draw( oldBuffer ) --Time-dependent fade: overlay canvas over itself.
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
--Render latest segment in trail.
|
2023-01-14 03:46:39 +00:00
|
|
|
love.graphics.setColor( 245 / 255, 169 / 255, 184 / 255, 1.0 ) --Trans pink.
|
|
|
|
--love.graphics.line( xi, yi, xf, yf )
|
2023-01-14 13:48:52 +00:00
|
|
|
--Segment stuff:
|
|
|
|
love.graphics.setLineJoin( "bevel" )
|
|
|
|
love.graphics.setLineStyle( "smooth" )
|
|
|
|
love.graphics.setLineWidth( 8.0 )
|
2023-01-14 13:59:20 +00:00
|
|
|
love.graphics.line( xp, yp, xc, yc, xn, yn)
|
|
|
|
love.graphics.circle( "fill", xn, yn, 4.0 )
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
love.graphics.setCanvas( oldBuffer )
|
|
|
|
love.graphics.clear( 1.0, 1.0, 1.0, 0.0 )
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
--Render circle directly to screen.
|
|
|
|
love.graphics.setCanvas()
|
|
|
|
love.graphics.draw( newBuffer )
|
2023-01-14 03:46:39 +00:00
|
|
|
love.graphics.setColor( 1, 1, 1, 1.0 ) --White.
|
2023-01-14 14:54:32 +00:00
|
|
|
love.graphics.setLineWidth( 1.0 )
|
2023-01-15 13:39:58 +00:00
|
|
|
--love.graphics.circle( "line", xn, yn, 4 )
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
oldBuffer, newBuffer = newBuffer, oldBuffer
|
|
|
|
|
2023-01-13 20:05:19 +00:00
|
|
|
end
|
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
marble._Draw = marble.Draw
|
|
|
|
|
|
|
|
function marble.Impact( impact )
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-13 20:05:19 +00:00
|
|
|
end
|
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
--Window resize.
|
2023-01-15 20:46:32 +00:00
|
|
|
function marble.Resize()
|
2023-01-14 03:46:39 +00:00
|
|
|
newBuffer = love.graphics.newCanvas()
|
|
|
|
--TODO: render oldBuffer to new newBuffer, but scaled down.
|
|
|
|
oldBuffer = love.graphics.newCanvas()
|
2023-01-16 02:38:15 +00:00
|
|
|
|
2023-01-14 03:46:39 +00:00
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
end
|
|
|
|
|
2023-01-16 02:38:15 +00:00
|
|
|
function marble.Reset()
|
|
|
|
|
|
|
|
INERTIA = 0.05
|
|
|
|
MAXSPEED = 2.0
|
|
|
|
|
2023-01-18 22:51:03 +00:00
|
|
|
ddx, ddy = 0, 0
|
2023-01-14 13:48:52 +00:00
|
|
|
oldState, curState, newState = State(), State(), State()
|
2023-01-15 20:46:32 +00:00
|
|
|
marble.Resize()
|
|
|
|
marble.Draw = marble._Draw
|
|
|
|
end
|
|
|
|
|
|
|
|
function marble.Canvas()
|
|
|
|
return newBuffer
|
2023-01-13 20:05:19 +00:00
|
|
|
end
|
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
|
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
marble.Reset()
|
2023-01-13 20:05:19 +00:00
|
|
|
|
2023-01-15 20:46:32 +00:00
|
|
|
return marble
|