# How to get the acceleration of the mouse's movement or other user imputs (e.g. trackball)

7 views (last 30 days)
francesco turco on 3 Oct 2021
Commented: francesco turco on 8 Oct 2021
Dears,
I am working on a alertness task to detect brief lapses of vigilance. Briefly, an object is continuosly moving away from the centre of the screen (or figure), while the user is asked to continuosly keep it back to the centre of the screen. The creators of the task provided an equation of the motion that gives the position of the object in each time frame when two centrifugal forces and the user imput force are known.
I am triyng to code the user imput force, and I managed to get it from the position of the mouse for each time frame (each cycle loop), as described below:
dt = 0.1; % time step for animation
t = 0:dt:20 ; % time length of the animation
figure
C=ones(length(t),2);
A=ones(length(t),2); % A=accelaration
xpos=ones(1,length(t));
ypos=ones(1,length(t));
for i=2:length(t)
C(i,:)=get(0, 'PointerLocation');
pause(0.05)
end
for ii=2:length(t)
A(ii,1)=(C(ii,1)-C(ii-1,1))/dt;
A(ii,2)=(C(ii,2)-C(ii-1,2))/dt;%
end
I doubt this is the best solution:
1) it ceases to work when the mouse reach the border of the screen, and the user must move it back
2) I wonder if there will be problems of time-accuracy
Thank you very much for helping me!
All the best,
Francesco
francesco turco on 3 Oct 2021
The wider question (maybe of interest also for other matlab users) would be "Is it possible to get the movement/postion of the mouse outside the screen borders?"

Rishabh Singh on 7 Oct 2021
According to my understanding, you wish to calculate the acceleration of the mouse pointer.
Possible way around for this would be using a 3-point instead of 2.
For acceleration calculation, and , .
So firstly, calculate the velocity and then acceleration.
To improve the accuracy of your calculation, consider
• decreasing value of ‘dt’ variable.
• Avoid any print or display statement in the ‘for’ loop, as they increase execution time.
• Use tic-toc to calculate time to execute the for loop. For example,
for i=2:length(t)
t_actual = toc;
time_gap(i+1) = t_actual;
pointer_location(i,:) =get(0, 'PointerLocation');
tic;
pause(dt)
end
Can you elaborate what you mean my location of pointer outside the screen?. According to me, the cursor/pointer it bound to stay within the screen, and as “get(0, ‘PointerLocation’)” gives pointer location as in pixel coordinates, so it’s not possible to get location outside the screen.
Hope this helps!
francesco turco on 7 Oct 2021
Dear Rishabh Singh,
Thank you very much for your help.
My aim is to code a task that requires a subject use the mouse movement to keep a dot centered within
a bullseye ring on a computer screen. The dot is escaping the centre of the bullseye ring, moved by a centrifugal force (Fc). The user opposes a force (Fu) to keep the dot centered, by moving the mouse.
To compute the Fu I managed to compute the acceleration of the of the mouse pointer movement, as described above. Unfortunately, this is not the best solution. Indeed, the mouse pointer is bound to stay within the screen.
Is there a way to get the acceleration of the mouse (not the mouse pointer), irrespective to the position of the mouse pointer?
Thank you again!
francesco turco on 8 Oct 2021
E.G. The dot is moving rightward, the user moves the mouse leftward to keep the dot close to the center of the screen. When the mouse pointer reaches the bound of the screen, the user movement leftward ceases to be effective.

### Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!