Heya! I'm trying to move a sprite based on the position of the crank, but the movement isn't as fine-grained as I'd like. For example, the sprite will move straight to the right if the angle of getCrankPosition
is between 71 and 119. Is there any way for the movement to be more responsive to the crank angle?
Here's a snippet of my code:
local pd <const> = playdate
local gfx <const> = pd.graphics
local diameter = 20
local image = gfx.image.new(diameter, diameter)
local sprite = gfx.sprite.new(image)
gfx.pushContext(image)
gfx.fillCircleAtPoint(diameter/2, diameter/2, diameter/2)
gfx.popContext()
sprite:moveTo(200, 120)
sprite:add()
function pd.update()
local x, y = getNewPos(pd.getCrankPosition(), 3)
sprite:moveTo(sprite.x+x, sprite.y+y)
gfx.sprite.update()
end
function getNewPos(angle, speed)
local cos = math.cos(math.rad(angle - 90))
local sin = math.sin(math.rad(angle - 90))
local cosFn = cos < 0 and math.ceil or math.floor
local sinFn = sin < 0 and math.ceil or math.floor
local x = cosFn(cos * speed)
local y = sinFn(sin * speed)
return x, y
end
Thanks in advance!