Maybe this helps. The variable continuousAngle
will track the total knob turning performed, i.e. turning the knob twice counterclockwise will get you to 720
. Then turning it three times clockwise takes you back down to -360
. Everything else should be easy to derive - limiting the minimum and maximum values, making the value wrap around, scale the value to for example 1
per turn or whatever else you want.
var lastAngle = 0;
var continuousAngle = 0;
function HandleDown(angle)
{
lastAngle = angle;
}
function HandleMove(angle)
{
// The orientation change in degrees of the knob since the last event with
// a range of [-180;+180). A positive value indicates counterclockwise, a
// negative value clockwise turning.
var change = (360 + angle - lastAngle) % 360;
if (change >= 180)
{
change -= 360;
}
// It may also be a good idea to not update continuousAngle if the absolute
// value of change is larger than say 10°, 20° or 40° because such large
// changes may indicate some kind of glitch like the user moving straight
// across the knob. But I am not sure and 20 is just a random guess.
if (Math.Abs(change) <= 20)
{
continuousAngle += change;
}
lastAngle = angle;
}
For floating point numbers the reminder can be calculated using Math.IEEEReminder
instead of the remainder operator %
. The linked page also shows how to implement this function yourself if it is not available in your language.