Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/690e4c4b-5530-800d-ae72-e702400813d4
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Assistant: Excellent — let’s add smooth easing so the velocity bar decays naturally toward zero when you stop turning the crank. === Excellent — let’s add smooth easing so the velocity bar decays naturally toward zero when you stop turning the crank. This version adds a small physics-style damping effect for realism — so when you stop turning, the velocity bar “coasts” back smoothly to center. <syntaxhighlight lang="lua">-- main.lua import "CoreLibs/graphics" import "CoreLibs/timer" local gfx = playdate.graphics local W, H = 400, 240 local CX, CY = W/2, H/2 local RADIUS = 70 -- Crank tracking local lastCrank = playdate.getCrankPosition() local lastTime = playdate.getCurrentTimeMilliseconds() local crankVelocity = 0 local displayedVelocity = 0 local direction = 0 -- 1 = clockwise, -1 = counterclockwise, 0 = still -- Damping factor for easing (lower = slower return to zero) local damping = 0.15 -- Helper: draw dial tick marks local function drawDial(cx, cy, radius) for deg = 0, 330, 30 do local rad = math.rad(deg) local x1 = cx + math.cos(rad) * (radius - 8) local y1 = cy + math.sin(rad) * (radius - 8) local x2 = cx + math.cos(rad) * radius local y2 = cy + math.sin(rad) * radius gfx.drawLine(x1, y1, x2, y2) end end function playdate.update() gfx.clear() local now = playdate.getCurrentTimeMilliseconds() local crank = playdate.getCrankPosition() local deltaAngle = crank - lastCrank -- Handle wrap-around (359° → 0°) if deltaAngle > 180 then deltaAngle = deltaAngle - 360 end if deltaAngle < -180 then deltaAngle = deltaAngle + 360 end -- Time delta in seconds local dt = (now - lastTime) / 1000 if dt > 0 then crankVelocity = deltaAngle / dt -- degrees per second end -- Direction if deltaAngle > 0 then direction = 1 elseif deltaAngle < 0 then direction = -1 else direction = 0 end lastCrank = crank lastTime = now -- Ease displayed velocity toward actual local lerpRate = 0.2 displayedVelocity = displayedVelocity + (crankVelocity - displayedVelocity) * lerpRate -- Apply damping to gently return to zero if math.abs(crankVelocity) < 2 then displayedVelocity = displayedVelocity * (1 - damping) end -- Draw dial gfx.setLineWidth(2) gfx.drawCircleAtPoint(CX, CY, RADIUS) drawDial(CX, CY, RADIUS) -- Draw needle local needleAngle = math.rad(crank - 90) local nx = CX + math.cos(needleAngle) * (RADIUS - 12) local ny = CY + math.sin(needleAngle) * (RADIUS - 12) gfx.setLineWidth(6) gfx.drawLine(CX + 1, CY + 1, nx + 1, ny + 1) -- shadow gfx.setLineWidth(4) gfx.drawLine(CX, CY, nx, ny) gfx.fillCircleAtPoint(CX, CY, 6) -- Velocity bar (left side) local barX = CX - RADIUS - 40 local barY = CY - 50 local barW = 16 local barH = 100 gfx.drawRect(barX, barY, barW, barH) local maxVel = 300 -- degrees/sec range local clampedVel = math.max(-maxVel, math.min(maxVel, displayedVelocity)) local filled = (clampedVel / maxVel) * (barH / 2) gfx.fillRect(barX + 1, CY - filled, barW - 2, filled) -- Text info local angleText = string.format("Angle: %.1f°", crank) local dirText = "Direction: " .. (direction == 1 and "clockwise" or (direction == -1 and "counterclockwise" or "still")) local velText = string.format("Velocity: %.1f°/s", displayedVelocity) gfx.setFont(gfx.getSystemFont()) gfx.drawTextAligned(angleText, CX, CY + RADIUS + 10, kTextAlignment.center) gfx.drawTextAligned(dirText, CX, CY + RADIUS + 30, kTextAlignment.center) gfx.drawTextAligned(velText, CX, CY + RADIUS + 50, kTextAlignment.center) gfx.drawTextAligned("Turn the crank to see velocity and direction", CX, 10, kTextAlignment.center) playdate.drawFPS(2, 2) end </syntaxhighlight> ===== - Eased velocity bar: displayedVelocity smoothly approaches actual crank velocity. ===== * Damping: when crank stops, bar gently returns to center instead of snapping. * Smooth transitions: you’ll see momentum-like lag when reversing direction. Would you like me to add a color gradient (e.g., light gray → dark for speed intensity) or leave it monochrome for now?
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)