2
\$\begingroup\$

I would like to simulate a point that moves in 2D. The input should be the speed of the mouse, so the new position will be computed as following

new_position = old_position + delta_time*mouse_velocity

As far as I know in GLUT there is no function to acquire the current speed of the mouse between each frame. What I've done so far to compute the delta_time as following

void Display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);
    static int delta_t, current_t, previous_t;
    current_t = glutGet(GLUT_ELAPSED_TIME);
    delta_t = current_t - previous_t;
    std::cout << delta_t << std::endl;

    previous_t = current_t;
    glutSwapBuffers();  
}

Where should I start from here? (Note: I have to get the speed of the mouse because I'm modeling a system)


Edit: Based on the above code, delta_time fluctuates so much

34
19
2
20
1
20
0
16
1
1
10
21
0
13
1
19
34
0
13
0
6
1
14

Why does this happen?

\$\endgroup\$
1
\$\begingroup\$

You're right in that you'll need the time delta between the current and previous frame. You also need to store current and previous positions of the mouse, and this happens to be pretty easy with GLUT.

int px = 0;
int py = 0;

void MouseMotion( int x, int y )
{
  int dx = x - px;
  int dy = y - py;

  // Do something with dx and dy
  // ...

  px = x;
  py = y;
}

void PassiveMouseMotion( int x, int y )
{
  px = x;
  py = y;
}

int main( void )
{
  // ...

  glutMotionFunc( MouseMotion );
  glutPassiveMotionFunc( PassiveMouseMotion );

  glutMainLoop( );
}

px and py are the x and y locations (in pixels) of the mouse. The origin should be top left of the screen with y pointing down and x pointing to the right (IIRC).

Note that changes in the mouse position (dx and dy) are only recorded in my example code when a mouse button is held down. I am sure you can modify this to your specific needs.

\$\endgroup\$
  • \$\begingroup\$ x pointing to the right and y pointing down. :) \$\endgroup\$ – Casey Feb 19 '14 at 23:40
  • \$\begingroup\$ @RandyGaul, in this case, the velocity will be the output of the system. My goal is to compute the new position based on the velocity but not the vice versa. \$\endgroup\$ – CroCo Feb 20 '14 at 2:25
  • \$\begingroup\$ Also, based on my code, this is what I'm getting, for delta_time 34 19 2 20 1 20 0 16 1 1 10 21 0 13 1 19 34 0 13 0 6 1 14 Why the delta_time fluctuates? \$\endgroup\$ – CroCo Feb 20 '14 at 2:27
  • \$\begingroup\$ @CroCo I don't know how you expect to compute the position of the mouse based on its velocity if user input decides where the mouse will be. \$\endgroup\$ – RandyGaul Feb 20 '14 at 5:13
  • \$\begingroup\$ @RandyGaul, I know it is silly since I have position and time, however, I want to go around by computing the position after calculating the velocity (which is computed by previous position). I did that though, but I'm still facing some problems to get work properly. \$\endgroup\$ – CroCo Feb 21 '14 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.