Skip to main content
GameDev.net gamedev.net
🔒 Locked

[XNA, mouse lag]: I found a fix for mouse lag, but it can't be the only one...

Started by iaminternets Feb 22, 2009 at 4:40 PM 13 replies 7.8k views
Original Post
iaminternets
iaminternets
Using XNA, if the IsMouseVisible property is set to true, and an image is placed at the mouse's position according to Mouse.GetState(), the image will be lagging around 80 pixels behind the correct position of the cursor (assuming the mouse is moving at a decent speed). For games which require fast, responsive mouse-movements, this is a deal-breaker. One solution I found online was to turn off vsync. Clearly, this has solution has its downsides! How do commercial games deal with this issue? [Edited by - iaminternets on February 22, 2009 11:57:21 PM]
I love the 'nets.
MJP
MJP
This is nothing specific to XNA...it has to do with the fact that with D3D whenever you submit a draw call, the results aren't reflected on-screen at the time that code runs on the CPU. Instead your CPU code might be up to 2 frames ahead of what the GPU is doing, and the commands are queued up by the driver. And even if you're CPU-bound and you're not working 1 or 2 frames ahead of the GPU, you still need to wait for the the back-buffer to be flipped and presented onto the front-buffer. Ultimately you're always going to be at least frame behind the cursor, hence the lag.

If you want to try something fancy, you could always use the Raw Input API's to get unprocessed mouse input data. Be aware though that since the data won't have any processing applied (for ballistics), it will feel "weird" if you just it to move a cursor.
iaminternets
iaminternets
Thank you; I've rated you up.

Is there no other way aside from using the Raw Input API? How do modern Windows games deal with this issue?
I love the 'nets.
cyansoft
cyansoft
Why couldn't you simply set IsMouseVisible to false and draw your own mouse cursor?
iaminternets
iaminternets
Quote:
Original post by cyansoft
Why couldn't you simply set IsMouseVisible to false and draw your own mouse cursor?


That's exactly what I'm doing (I turned on the Windows cursor to see how bad the lag was).

Something interesting: When I turn off vsync, it appears the image is still lagging a frame or two behind the windows cursor (about 2-5 pixels as opposed to 50-100 pixels), though that level of lag is probably unavoidable.

I should have mentioned this sooner, but IsFixedTimeStep is set to false.
I love the 'nets.
wingbird101
wingbird101
I think this thread at the xna community site might help you:
http://forums.xna.com/forums/t/15035.aspx

I saw a more comprehensive thread somewhere else on their forums a while back with actual code and examples, but I couldn't find it.
cyansoft
cyansoft
How accurate are the mouse coordinates if you turn vsync back on and set IsFixedTimeStep to true?
Sc4Freak
Sc4Freak
Most commercial games that use a cursor wouldn't use a software-drawn cursor like you're using, because of the issues you mentioned. Usually they'd use a hardware cursor with a custom image applied to it. This allows for much better responsiveness.
iaminternets
iaminternets
wingbird101:

From what I read, it seems I'd have to create XNA from within a Windows Form. If I'm going to do that, how much harder would it be to simply use SlimDX (which would give me much greater control over sound)?

cyansoft:

If I turn both vsync and fixed-length frames on, the cursor seems to be delayed the same number of pixels (about 50-100), but it also stutters.
I love the 'nets.
iaminternets
iaminternets
Have any XNA-users found a work-around?
I love the 'nets.
Moe
Moe
Never really noticed this in my own stuff. What exactly is it that you are trying to do that is so time/pixel critical?
iaminternets
iaminternets
I'm working on a fast-paced real-time strategy game.
I love the 'nets.
Opt7ons
Opt7ons
After looking around a bit I found this solution:

using System;using System.Drawing;using System.Windows.Forms;........../// <summary>/// This is the main class for the game/// </summary>public class Main : Microsoft.Xna.Framework.Game{    public Main()    {        Bitmap cur = new Bitmap(@"C:\Documents and Settings\Opt7ons\Desktop\cur.bmp", true);        Graphics g = Graphics.FromImage(cur);        IntPtr ptr = cur.GetHicon();        Cursor c = new Cursor(ptr);        Form.FromHandle(this.Window.Handle).Cursor = c;        this.IsMouseVisible = true;    }}


From: http://forums.xna.com/forums/p/5131/26901.aspx
Sc4Freak
Sc4Freak
Yeah, that's using a hardware cursor. You should also be able to have animated cursors if that's supported by .NET - I think animated cursors have the extension .ani.
iaminternets
iaminternets
From first glance, that certainly seems to be it!

I'll play around with it more after work. Thank you very much for finding this! :)
I love the 'nets.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.