XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Framerate independent mouse cursor

Last post 8/21/2007 9:04 PM by Jody McAdams. 7 replies.
  • 6/8/2007 6:51 PM

    Framerate independent mouse cursor

    Moar questions!

    In my game I use the mouse cursor to aim. I've looked around for ways to show a custom mouse cursor in-game, but all I can find is the following method:

    • Keep track of the mouse's position during Update calls
    • Draw a cursor at its position during Draw calls

    The problem with this is that it limits the mouse's responsiveness to the framerate, which causes it to feel sluggish. The effect can easily be seen by turning on the IsMouseVisible property so that the game shows the default hardware mousecursor as well. If I move the cursor around, at 60fps, my homemade cursor seems to lag behind the windows cursor, following it around across the screen.

    So what I've done is hook up an event to the mouse's movement. As a test I removed the position tracking from the Update method and put it in my event instead, which worked nicely:

    void hook_MouseMove(object sender, MouseHookEventArgs e)
    {
    Vector2 windowposition;

    windowposition = new Vector2(this.Window.ClientBounds.Left, Window.ClientBounds.Top);

    mousePosition.X = e.X;
    mousePosition.Y = e.Y;
    mousePosition -= windowposition;
    }

    Now I'd also like to draw the mouse cursor on the window/screen at this point. Ideally, I'd plot the cursor directly to the screen instead of the backbuffer, using damage repair so as to not leave a messy trail.

    Unfortunately I am at a loss at how to do this, or if it is even possible within the framework. So my question is how do I do a quick plotting of my mousecursor to the screen, or is there another way to add a fast and responsive custom mousecursor image to my game?

    Edit:

    After some more experimenting, it seems that the SynchronizeWithVerticalRetrace is causing me grief. The game will draw the screen, then wait for the next retrace to copy it all to the front buffer - and this minimal wait time is causing the cursor to lag. At the moment I'm using the workaround of just turning off retrace synching, which works as long as my framerate stays high enough to avoid "tearing".

    Perhaps there's some way to respond to the retrace or the game's final draw-to-front-buffer step where I can quickly plot in the cursor somehow.. :)

  • 6/12/2007 12:56 PM In reply to

    Re: Framerate independent mouse cursor

    Noone has any ideas what causes this, or has run into the same problem? I've added a second cursor with a built-in 3 frames of lag, and the difference is about the same as the difference between the "instant" custom cursor and the actual cursor.

    Three frames worth of lag in the mouse response time is pretty significant, especially for mouse reliant games like realtime strategies.

  • 6/12/2007 3:29 PM In reply to

    Re: Framerate independent mouse cursor

    vsync == evil

    Are you setting IsFixedTimeStep to false in your game class? By default it is true and the Game class updates 60 times a second, perhaps this combined with your previous use of vsync caused the lag. It's just a guess, but easy enough to test.
  • 6/12/2007 6:21 PM In reply to

    Re: Framerate independent mouse cursor

    Drivers may triple-buffer ahead of time, up to three frames' worth of latency. That might be what you're seeing, if your rendering runs fast enough that the driver is ahead.

    You can try adding a call to sleep 10 milliseconds before presenting your frame and see if it makes things better. Or turn off vsync.
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 8/21/2007 6:04 AM In reply to

    Re: Framerate independent mouse cursor

    Do this.  Calculate where the mouse should be by storing the vector direction of the mouse and moving it to the "correct" position.  You'll probably need to keep a variable that is updated occasionally that stores the amount of time to update a frame and use this to move the mouse along it's movement vector.  Experiment around with this idea and you may have what you want.

    In short, add a vector plus a time amount to the current position of the mouse and use this as the draw position.

    Puttin' my foot up on that rail since 2008.
  • 8/21/2007 6:45 AM In reply to

    Re: Framerate independent mouse cursor

    Here is a link to a Custom Mouse pointer game component I put together, this might help with what you need. For those who are interested I have also done this using the GamePad Controller to move the mouse pointer.
  • 8/21/2007 8:30 AM In reply to

    Re: Framerate independent mouse cursor

    MegaJiXiang: that's a neat idea, extrapolating the mouse's position. Like accounting for lag in a multiplayer game, right? I'm not sure how well this will work with something as delicate as mouse movement, but it's worth a try. How would I get accurate information on timing though? I need to know the "lag" between drawing the mouse and actually displaying it on the screen to be able to predict the position.

    Glenn: I might be mistaken but I don't think your component addresses the issue I'm facing.

  • 8/21/2007 9:04 PM In reply to

    Re: Framerate independent mouse cursor

    Harald Maassen:

    MegaJiXiang: that's a neat idea, extrapolating the mouse's position. Like accounting for lag in a multiplayer game, right? I'm not sure how well this will work with something as delicate as mouse movement, but it's worth a try. How would I get accurate information on timing though? I need to know the "lag" between drawing the mouse and actually displaying it on the screen to be able to predict the position.

    Glenn: I might be mistaken but I don't think your component addresses the issue I'm facing.

    Ok here goes... keep in mind I am stuck in China on a crappy computer with no XNA at the moment.  I think you just need to use the time since the last update.  Drawing adds to the time since last update and you can consider moving the mouse to be an instant operation.  Try messing around with using the time since last update as the scalar to the mouse movement vector.  If I was doing this I'd just mess around with this idea to get it working and if it doesn't work, just bust out the paper and pencil.  Paper and pencil is the best advice I can give to everyone doing math related things in this forum.  This advice comes from my teacher who ingrained it in all our brains for all eternity.

    Puttin' my foot up on that rail since 2008.
Page 1 of 1 (8 items) Previous Next