- Home /
Infinite circular slider / rotary dial input
I want to implement an input that consists of a circle that the user would rotate like a dial to increment/decrement an associated number. I've been able to find a few posts about how to make a dial visually rotate when used like this, and I think I could figure out how to get a number from the resulting angle if the dial was limited to < 360 degrees of spin, but I want the user to be able to keep rotating the dial indefinitely to continue raising or lowering the associated number. I'm not entirely sure how to approach this, I'd appreciate any help to get started.
Thank you!
Answer by elenzil · Oct 05, 2016 at 09:17 PM
float prevDegrees = 0;
float cumulativeValue = 0;
// assuming the "degrees" is guaranteed in the the range [-180, 180].
void onNewDegrees(float degrees) {
float diff = degrees - prevDegrees;
if (diff > 180f) {
diff -= 360f;
}
else if (diff < -180f) {
diff += 360f;
}
cumulativeValue += diff;
prevDegrees = degrees;
}
actually, this may work even if degrees is in the range [0, 360]. not sure.
Yes, this does work with any range. The subtraction actually removes any offset that both values might have as they cancel out. The delta must never actually be larger than 180 or smaller than -180 as in that case the direction can not be deter$$anonymous$$ed / would be inverted. However at a decent framerate the user usually can't rotate fast enough to produce a delta larger than +-180.
the only case i worry about is if the user runs their finger right through the center of the dial. in that scenario the delta might be more than +/- 180º. but it might be fair to call that user-error. ;)
actually i guess a really robust "knob" widget would have a maximum amount of change per frame, so if you go through the center it would only turn say a max of 30º per frame or something. i've never seen that implemented, tho.
This worked perfectly, just note that you need to reset the prevDegrees to the initial angle whenever the user touches the control or the value controlled by the dial can jump around a bit. Thanks much!
cool - glad that worked for you! yes, as you point out, "prevDegrees" should be reset on the beginning of input.
Your answer
Follow this Question
Related Questions
cant slide GUI slider 0 Answers
'MouseLook.sensitivityX' is inaccessible due to its protection level. 2 Answers
Use gui slider to change text field and vice versa? 1 Answer
Hover state doesn't work on slider with custom styles 0 Answers
How to change how meany decimals are in a GUI sound slider 1 Answer