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 staticint delta_t, current_t, previous_t;
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.
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
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
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.
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 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.