Monogame/C# - Mouse position flickering - c#

I am having some problem with mouse position in MonoGame and I can't really figure out what I am doing wrong. I want the game window to be at the position of my cursor, so I made this simple line:
protected override void Update(GameTime gameTime)
{
Window.Position = new Point(Mouse.GetState().X, Mouse.GetState().Y);
base.Update(gameTime);
}
But if I do this, the game window flickers between two positions. What is the problem ? Am I doing something wrong ?
Thank you!

Well it actually makes sense when you think about it. Let me make an example:
Cursor is 500,500 (in relation to Window)
Window is 300,300 (in relation to Screen)
The first time the code runs, it moves the window to 500,500.
Meaning the cursor will now have a location of 300,300 (in relation to Window).
The second time the code runs, it will detect the mouse is now at 300,300, and thus move the Window back to 300,300.
As such it runs forever.
To accomplish what I think you want, you'd need to account for the current position of the Window.
Which means you'll have to "lock" the position of the mouse (in relation to the Window), and whenever this changes, move the Window the amount that the mouse position moved. And of course move the mouse back again

Related

Mouse Global Hook - WM_MOUSEMOVE delay

Okay so there are plenty of samples for mouse global hooking.
My issue is how can I add a timer/delay for mouse movement.
What is the way of doing this? I thought maybe I should Thread.Sleep() on detecting WM_MOUSEMOVE ?
if (MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
{
Thread.Sleep(delay)
}
Thanks.
i'm afraid you cannot slow down the mouse movement in this way.
Here you are just notified of a new mouse position, the system doesn't care what you do with this notification and how long it takes to complete, or if it sleeps.
I suppose you should "record" the mouse position every "delay" time, and then every time a new WM_MOUSEMOVE event arrives (and the coordinates are different from the record ones), then you should "reset" the mouse cursor position to the saved coordinates.
of course, this until the WM_MOUSEMOVE arrives within "delay" time. Otherwise just record a new position and wait for the next event.
.NET have the Cursor.Position property that should allow you to move the mouse cursor where you want, other languages should have their analogous, but i've never tried it and i'm not sure it operates correctly in your "global" context.
anyway messing around with the cursor position is something that can make the user very upset
maybe you don't want to stop completely the cursor for "delay" time, maybe you want to linear interpolate the new position with the previous one with a <1 factor? this will slow down the mouse but with a smoother effect.

Mouse and Cat chasing game

I have to make a Windows Form Application which has to be a cat chasing a mouse. And you move the mouse with the arrow keys while that cat is going towards the mouse. It should detect when they both get in contact and show the time it took.
Both the cat and mouse should be placed randomly in the window each new game. And in higher difficulty, it should have two holes and when the mouse goes in one of them it shows on the other. Can someone help because I have no idea where to start the main part?
I've already placed pictures if a cat and a mouse in separate picture boxes and made the part with choosing difficulty.
You'd basically be combining these steps:
Implement an OnKeyDown handler to respond to key presses. Reposition the mouse image when this occurs.
Create a timer for the cat animation. At each step move the cat towards the mouse's position.
Whenever the position of one of the objects changes, check if there's a collision and handle it appropriately.
It's pretty simple to do this with a game engine as well. That would handle the animation for you. Here's how you'd get started with Unity for example:
https://msdn.microsoft.com/en-us/magazine/dn759441.aspx

Check bounds of a list of images (or the one currently being used in the list)

I have a cat and mouse game and I have the mouse randomly moving about with timers and direction change etc. When the mouse moves left, the picture box changes to another completely separate file so it looks like it has turned.
I'm now trying to check if the cat has collided with the mouse so that I can end the game.
Does anyone know how I could check the current bounds of the mouse so I can detect when its been hit? I can provide any code needed but as it is long I'll copy and paste on request.
that the question linked below was how i was planning on doing it,
public void Collsions()
{
if (Cat.Bounds.IntersectsWith(Mouse.bounds))
}
but i then realized that the MouseBounds cant fine a bound as the image changes all the time depending on its direction
MouseImage.Image = MouseImages.Images[(int)currentDirection];
so there is a list of images that change depending on the direction of the mouse,and i don't know how to check the bounds after the image is changed.
this is all the mouse code:
https://pastebin.com/aEUWBdru
this is all the cat code (so far);
https://pastebin.com/ndAbDv5c

Normalize and smooth the mouse movements?

Is there a way to smooth out the mouse movements? I want to remove all the small jitter in normal mouse moving with the hand, like you can never draw a clean line in paint because of you hand doing small jittering movements.
This might be hard to understand what I mean, but if you know zbrush they have a feature that is called lazy mouse http://www.pixologic.com/docs/index.php/Lazy_Mouse im looking for a way to recreate this inside my app. I can read the mouse position with Cursor.Position but I don't find a way to average out these numbers before they get sent to the pointer on the screen.
This is only possible if you can delay the effect of the mouse movement slightly. You record the points of the mouse movement at a certain frequency and then average them out to a line. Then use that line to draw whatever you need. You wont be able to directly set the mouse cursor to the averaged position as that would then feedback into your program as a new mouse movement.
Make sure you build it so you can tweak how long you delay the mouse movement, and how aggressive the averaging is (say by restricting the number of points it includes), and the frequency at which you record mouse movements (this is could affect cpu usage if its too frequent).
You will of course have to create some sort of abstraction for the mouse in your application and create a way for the application to get hold of it. (I would be trying to keep this as similar as possible to normal winforms/wpf so I could revert the change and just use mouse movement directly if needed).

Suppress mouse-movement

I need to freeze the windows mouse cursor on the screen, so it stays hovered over a specific UI element. While the mouse is in this frozen state, I would still like to be able to interact with the UI using a "fake" mouse pointer.
Currently, I've got a low level mouse hook that prevents WM_MOUSEMOVE messages from being passed along, effectively stopping all real mouse movement. However, when I don't pass along the updated coordinates, windows actually sends me the old coordinates in a separate WM_MOUSEMOVE message, as if to correct for the fact that the mouse didn't move.
Any idea on either how to prevent Windows from sending me these corrected coordinates, or another approach of how I can freeze the actual mouse curosr and still allow the physical mouse to control a "Fake" cursor?
You can suppress mouse cursor movement by using DirectInput exclusive mode.
You should acquire mouse input device when cursor movement must be suppressed. This will block windows cursor movement but you can get messages for your fake cursor using DirectInput API.
If you move the mouse cursor position in response to a WM_MOUSEMOVE message, to put the mouse back to where you want it, you will get another mouse move message because the mouse has been moved again (because you moved it). To stop this, don't set the mouse position if it is already in the right place.
You may also want to clip the mouse position to your window and get exclusive access to it.
Why don't you just draw an image of the cursor at the place you want it frozen, then set the real mouse cursor to be hidden (Cursor.Hide IIRC).
It's not clear to me what you are trying to do, perhaps making a tutorial for your app and you want to show mouse movements, etc. Perhaps this library will be useful:
http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx

Categories

Resources