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?

  • \$\begingroup\$ Take a look at this: soundstep.com/blog/2011/04/08/… The function :updatePosition. It uses Atan2. \$\endgroup\$
    – Sidar
    Commented Feb 1, 2013 at 17:13
  • \$\begingroup\$ 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) \$\endgroup\$ Commented Feb 1, 2013 at 17:55
  • \$\begingroup\$ 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/… \$\endgroup\$
    – House
    Commented Feb 1, 2013 at 18:51
  • \$\begingroup\$ @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. \$\endgroup\$
    – Tetrad
    Commented Feb 1, 2013 at 19:44
  • \$\begingroup\$ @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. \$\endgroup\$
    – House
    Commented Feb 1, 2013 at 19:48

1 Answer 1


Instead of just using deltas, have you considered just linearly mapping the distance from the start of the touch to some rotation value?

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 )

I probably would do something like that first before going for angle checks and things like that since, as a user, if I want a knob to increase I'd probably just want to drag it to the right instead of having to drag around in a circle. If you went with the angle, the user might get into a situation where dragging "more" doesn't increase the knob as expected, assuming your knob rotates more than 180 degrees.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .