Discuss Scratch

koda1956
Scratcher
100+ posts

Mouse movement speed

So, I was wondering, how do you get a variable to show how fast the mouse is moving? say, you are moving your mouse slow, the Variable would be a lower number, but if it is moving fast, the variable is higher… can anybody help me there?

Thanks!
amylaser
Scratcher
500+ posts

Mouse movement speed

Use two variables to constantly store the mouse's x and y position, then compare it to the mouse's current x and y position. The difference between the recorded x/y values and the mouse's current x/y values will tell you the speed of the mouse (or rather, the distance the mouse traveled).

For example:

when green flag clicked
forever
set [last x v] to (mouse x)
set [last y v] to (mouse y)
wait (0.01) secs
set [x speed v] to ((mouse x) - (last x))
set [y speed v] to ((mouse y) - (last y))
end

EDIT: as Scratch-minion showed in the next post, you can divide the distance by 0.01 secs if you want to quantify the speed in terms of pixels. ^.^

Last edited by amylaser (Nov. 2, 2016 00:33:19)

Scratch-Minion
Scratcher
1000+ posts

Mouse movement speed

Hi koda1956.

I have made a demo project for you: https://scratch.mit.edu/projects/128600654/

The mouse speed is in pixels per second.

Basically, record the mouse position, then at a later time, the speed is the distance the mouse has moved (Pythagoras) divided by the time elapsed.

I hope this helps.
(Let me know when I can delete the project).
.
The_Red_Scratcher
Scratcher
10 posts

Mouse movement speed

this is an old discussion post from 2016 and im in 2022 but i wanted to know how to cap the speed x variable to a number like 10 or smthng cuz my script to cap the numbers is smthng like this

if <<[(speed)] > [10]>> then
repeat until <[(speed)] < [10]>
change [speed] by (-1)
end
[scratchblocks]

(if it ends up looking like ascii im sorry this is my first comment on a scratch forum)

quick edit: thanks god it didnt btw the "speed" is actually the x+y variable combined

Last edited by The_Red_Scratcher (June 28, 2022 11:38:55)

TheProjectMaker2
Scratcher
3 posts

Mouse movement speed

wait i wanted that!

Powered by DjangoBB