Mouse Speed?

I want to acquire mouse speed, so I need to calculate the delta time between each reading first. I want to know if what I'm doing correct.
I'm using OpenGL and glut.

I have the following variables
static int delta_t, current_t, previous_t;

In Rendering function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Display()
{
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);
    
    current_t = glutGet(GLUT_ELAPSED_TIME);
    delta_t = current_t - previous_t;
    std::cout << delta_t << std::endl;

    previous_t = current_t;
    
    glutSwapBuffers();  
}


These what I'm getting
34
19
2
20
1
20
0
16
1
1
10
21
0
13
1
19
34
0
13
0
6
1
14


why do the values fluctuate?
Last edited on
The code looks OK. about fluctuations - I agree that seems strange, but it could not decide what could be the reason. you can simply print out "current_t" to make sure that time looks proper and contains such non equal intervals...

There could be small chance that timer is not increased steadily though I do not believe it - this was an issue with older computers and specific kind of timer increasing once per 18.2ms... Heard nothing of the kind nowadays.
Are you moving the mouse with constant speed? If the mouse stays steady do you get constant 0?
i m not familiar with openGL and glut but i can give you advices.

to get mouse speed

a constant time distance like aSec = 1000;

delta_t value(like you used)

the distance mouse made in delta_t formula
radical((cur_mouse.x - prev_mouse.x)² + (cur_mouse.y - prev_mouse.y)²)
for example : radical((7 - 4)² + (11 - 7)²) is equal to 5 and use double for more precision

and finally this is equal to mouse speed
mouseMove_inDeltaT * (aSec / delta_t)

and i might have made mistakes in formulas, need to be checked

also a note: speed is sth done in a constant delta time. for example 60 km / h

and srry if i made word mistakes. eng is not my native language
Last edited on
@rodiongork,

current_time
120
143
144
166
185
185
198
199
215
216


I'm getting same time in some readings.

@Mats,
I wanna first compute the delta time then calculating the speed of the mouse since I know the position of the mouse at a sampled rate of time.

@Ceset,
Thanks for being helpful. I want to know precisely how fast does the computer draws frames?
I wanna first compute the delta time then calculating the speed of the mouse since I know the position of the mouse at a sampled rate of time.

Your code doesn't show you getting the position of the mouse when you get the time, so how accurate can that be?


I want to know precisely how fast does the computer draws frames?

It varies. The mouse info should be updated independently of frames being drawn.
Are you moving the mouse with constant speed?
to get mouse speed
Your code doesn't show you getting the position of the mouse when you get the time, so how accurate can that be?

According to the question he's asking why his delta time values fluctuate so much. Why is everyone trying to solve the second part of the problem.

Any way, what else is happening in your render loop that could be taking up time? Try removing everything other than the Display call, if that's what you have already then try drawing something (a few quads or tris) and compare the delta times.
@CroCo
like @cire said, it varies. so you need to check the time between two draws to be sure.

@Lachlan Easton
well it might be. but i m not familiar with any of the libraries and i did not get what the code does actually and why the values fluctate. so i just give an advice which is all i can. sryy if it disturbed you
Last edited on
This is the code to compute the X velocity of the Mouse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Display()
{
    
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1.0f, 0.0f, 0.0f);
    static float Xpre;
    
    
    
    current_t = glutGet(GLUT_ELAPSED_TIME);
    delta_t = current_t - previous_t;
    Vx = ((float)X_mouse - Xpre) / delta_t;
    std::cout << Vx << std::endl;
    Xpre = X_mouse;
    
    glBegin(GL_LINES);
    glVertex3f(0.0, 0.0, -1.0);
    glVertex3f(10.0, 10.0, -1.0);
    glEnd();
    previous_t = current_t;
    
	glutSwapBuffers();  
}


I'm still getting zeros even though I'm moving the mouse.
i m guessing a few reasons that is because

*the loop is too fast that you cant even move at enough speed to make a change
*output is not double or float but int so it seems 0

note : a loop can run at 35m at a second which is max i could achieved at my humble laptop
Last edited on
closed account (S6k9GNh0)
SDL and GLFW gives much better means as to grabbing mouse "speed". For instance, SDL can provide mouse distance relative to previous event push which I believe is exactly what you're wanting (since the event queue is pushed exactly once per frame or more). SFML might as well, not sure.
Last edited on
I've solved the problem by doing the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Display()
{
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);
    static double  delta_t;
    
    clock_t t;
    t = clock();
    
    glBegin(GL_LINES);
    glVertex3f(0.0, 0.0, -1.0);
    glVertex3f(10.0, 10.0, -1.0);
    glEnd();
    
    glutSwapBuffers();
    
    t = clock() - t;
    delta_t = 1000.0*(double)t/CLOCKS_PER_SEC;
    std::cout << delta_t << std::endl;
    
}


This what I got in millisecond.
0.061
0.051
0.049
0.052
0.05
0.046
0.044
0.043


But I got another problem. It seems that the speed of drawing the frames is faster than getting the movement of the mouse, so if I want to get a good result, I have to move the mouse very very fast.
what you need to do is a lead-filter.

What you do, is you calculate the (dMousePosition / dTime) where the time is the real-time sample from the OS. Then you either apply a low-pass filter over that signal or apply a moving average over the past X steps. You'll see fewer jumps in your drawing.

If you calculate the acceleration (dSpeed / dTime) then you can also predict what the speed will be in those frames where you are not getting updates from the mouse. This should eliminate any residual jitter.

The moving average will also help to correct when you go from a predicted value to an actual value so you don't get a jump when an actual value jumps in.
So, what about the following approach. It seems it's working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void Display()
{
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);
    static double Xpre, previous_t;
    double current_t, delta_t, Vx, diffX;

    glBegin(GL_LINES);
    glVertex3f(0.0, 0.0, -1.0);
    glVertex3f(10.0, 10.0, -1.0);
    glEnd();
    

    current_t = static_cast<double>(glutGet(GLUT_ELAPSED_TIME));
    delta_t = (current_t - previous_t)/CLOCKS_PER_SEC;
      diffX = (X_mouse - Xpre);
     
    if ( diffX != 0)
    {
        if (delta_t == 0) delta_t = 1;
        Vx = diffX / delta_t;
        std::cout << Vx/1000.0 << std::endl; 
    }
    
    glutSwapBuffers();
    previous_t = current_t;
    Xpre = X_mouse;
}


the velocity in millisecond.
-272.727
-181.818
-181.818
-100
-90.9091
-90.9091
-90.9091
-181.818
-90.9091
-117.647
-181.818
-181.818
-83.3333


Any suggestions about this approach?
Last edited on
Topic archived. No new replies allowed.