# How to emulate knob turning, based on x/y movement?

I'm doing a game where one component is that you turn a knob. I'm having trouble getting good "knob behavior".

I know the current touch/mouse X,Y location, and I keep track of the previous X,Y location, so I can determine if I'm going left or right and/or up or down.

It works, but it's crude. I suspect I have to take an average of any given direction over a period of time (say, last quarter second) to eliminate jitter because people don't move uniformly in the same movement every moment.

I can't really use touchdown/release because you should be able to put the touch down, then move left/right up/down continuously without release, and the knob would turn, just as if you had a free-spinning audio control or video shuttle (say).

Any thoughts in this direction on how to even it out?

• Take a look at this: soundstep.com/blog/2011/04/08/… The function :updatePosition. It uses Atan2. Feb 1, 2013 at 17:13
• I'd definitely recommend using the angles to find a rotation for the knob which like Sidar said best done with Atan2. You will still need to remove some of the shakyness of the control (which can probably done by only allowing the turn after a certain deltaAngle value is reached) Feb 1, 2013 at 17:55
• Try to be a bit more generic in your questions. Emulating a door knob is not a common activity. Rotation based on touch is more common and has been asked before: gamedev.stackexchange.com/questions/29799/… Feb 1, 2013 at 18:51
• @Byte56 the OP doesn't mention "door" in a question. When I hear "knob" I'm thinking like a control knob on a switch board and he wants to convert mouse movement to a hand twist gesture. Feb 1, 2013 at 19:44
• @Tetrad True enough. But OP doesn't mention mouse movement either ;). It's using touch to cause rotation, no? If it's rotating a sprite or controlling a knob, it's taking a change in position and converting it to a rotation value. Seems like they're the same to me shrug. Feb 1, 2013 at 19:48

So let's say you start at position (x,y) and your knob is currently rotated to point to angle theta. As long as your finger is held down, you update the current angle of the knob. At position (x+z,y+w) you figure that you moved z+w distance (using manhattan distance so you can go negative), which you then multiply by some scalar value r to give you how much you should rotate the knob. Say 1 radian of rotation equals 100 pixels movement (so r = 100 in this case). So current rotation = theta + ((w+z) / r )