Improve sensitivity of crank-based movement


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> =

local diameter = 20
local image =, diameter)
local sprite =

  gfx.fillCircleAtPoint(diameter/2, diameter/2, diameter/2)
sprite:moveTo(200, 120)

function pd.update()
  local x, y = getNewPos(pd.getCrankPosition(), 3)
  sprite:moveTo(sprite.x+x, sprite.y+y)


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

Thanks in advance!

That's just the way the math works out from sine and cosine when you use floor or ceiling functions on the result.

My guess is that you apply floor/ceiling because the position value will be applied to a sprite position on screen, so you don't want fractional values. But that doesn't mean you can't track the position fractionally.

Try allowing decimals in your x and y position variables. This will allow for finer tracking and calculation of the object position for things like angles. Just use rounding to the nearest whole number before applying them to the actual draw functions-- if you truncate the decimals on every calculation, you are losing a lot of position info, and you will get behaviors like what you are seeing.

EDIT: You can actually just pass decimal numbers directly as arguments to moveTo()-- Playdate will round the position values for you.

1 Like

Oh man that was such an easy fix!! Indeed I tried removing the floor/ceiling functions and now it's waaaay better! I didn't think I could use decimals for this, great to know :slight_smile: Thanks a lot!

1 Like