# [PD] Beginners query: Calculating mouse speed

Frank Barknecht fbar at footils.org
Thu Nov 29 15:49:21 CET 2007

```Hallo,
Rebecca Schatz hat gesagt: // Rebecca Schatz wrote:

> I have a really simple question: Does anyone know how to make a patch that
> will give me the speed the mouse is moving?  I'm on windows and am using
> gemmouse to get the position.

A handy idiom to get the difference between a value's current and its
previous position is a cross-wired trigger-substraction construct like
this:

[t f f]
\ /
x
/ \
[-  ]

If you connect for example a slider on top of that, you can see how
fast the slider was moved. With Gem you can use the values coming out
of gemmouse inserted into this idiom. You can also drive the whole
thing with a [metro] or from the [gemhead] while storing the mouse
coordinate into a [float]'s right inlet like:

[gemhead] <- or use a [metro] here
|
[b]   [gemmouse]
|     |
[float]
|
[t f f]
X
[- ]
|
[s SPEED-X]

Duplicate this for the y coordinate, if you want. For absolute speed
in both directions you can add the squares of "SPEED-X" and "SPEED-Y"
and [sqrt] it. That's like in Pythagoras from school: a^2 + b^2 = c^2

But when using this with [gemmouse] make you you match the
[gemmouse] coordinates with the [gemwin] coordinates accordingly.