The relative angle is the speed if you calibrate it correctly. (Velocity is the derivative of speed, if you want to be speeding up.) Here is an example Lua function that converts the relative angle to a final speed for a specific range of motion. Note that this calculation can be inlined. It does not need to live in a function.
function relative_angle_to_speed(relative_angle, speed, range_of_motion)
return relative_angle * speed / range_of_motion
end
Say you want the speed to be 2 (pixels or game world units or whatever) for every 90 degrees the crank is turned. In that case, you would call the function as follows.
local ra, ara = playdate.getCrankChange() -- degrees
local base_speed = 2 -- pixels or game world units or whatever
local range_of_motion = 90 -- degrees
local speed = relative_angle_to_speed(ra, base_speed, range_of_motion)
If you want to do anything else, you will need to figure out what your specification is and translate it into code. It is easy enough to add a counter or use the accelerated change return value (ara
in the code above). Having said that, you need a clear idea of what your specification is before it can be coded, even if values get tweaked along the way.
Disclaimer: I tested the Lua in an online editor, so I am not 100% sure the Playdate SDK call is correct. The rest of the code is verified as working.
EDIT: It could be fun to have powerups/enemy effects that modify base_speed
/range_of_motion
. Also, note that these values are split out to make the problem easier to think about, but the terms are inversely equivalent.
calibration_constant = base_speed / range_of_motion
speed = relative_angle * calibration_constant
-- therefore
base_speed = range_of_motion * calibration_constant
range_of_motion = base_speed / calibration_constant
-- base_speed*2 or range_of_motion/2 to double the final speed
You could precalculate the calibration_constant
and apply powerups to it, but it might just be easier to think in terms of speed up and speed down modifiers.
local ra, ara = playdate.getCrankChange() -- degrees
local speed_modifier = 2 -- unitless, double speed
local base_speed = 2 -- pixels or game world units or whatever
local range_of_motion = 90 -- degrees
local speed = relative_angle_to_speed(ra, speed_modifier * base_speed, range_of_motion)