Search code examples
pythonmouse-cursor

How do I calculate the magnitude of the velocity of my mouse cursor, in Python?


http://dl.dropbox.com/u/779859/speedCalc_puradata.JPG

I achieved it in pure data, have a look at the schematic of what I'm thinking:

  • Recieving Midi Control input from ctlin 20 and 21
  • Pipe delays whatever signal it recieves
  • Pythagoras
  • Viola, the speed of the input. The units don't matter, as long as it is absolute.

I was thinking about doing the same but in python, for the mouse cursor.

Basically, when I move my mouse, I want to see at what speed the mouse is moving. The rate of input packets is constant at 200hz.

I might have come up with a way, though I haven't tested it yet. How about collecting, say, 51 values in a list, keeping the [0] current, and [50] the oldest. Then simply doing the math on those two values?


Solution

  • What you are describing will give you the magnitude of the velocity times the length of the time interval of measurement. The actual velocity will be a vector. You can get its first coordinate as (posX - delayed_posX)/t and its second coordinate as (posY-delayed_posY)/t where t is the time interval between the measurements. Note that this satisfies Pfinal = Pstart + t V where P is our position vector. Whenever you want to know how to measure an approximation of the velocity, that's always your starting point. The smaller the time interval, the more accurate a picture of the velocity you will have.

    In response to your question about time.sleep, no it will not slow down your other code: it will stop it completely unless it runs in another thread.

    What exactly are you trying to do? It's hard to say if there's a better way unless we know where you need the data to be, when you need it to be there and how current you need it to be.