XNA Mouse position is off - c#

I'm working on a miniature golf game in XNA, I originally had everything in Game.cs (main), but I now want it to be more object-oriented, so I made separate class for most of my stuff.
When I had everything in Game.cs, it was working fine, now it doesn't.
What is happening is this:
When my cursor is at the top left corner of the game window, it's like X=200, Y=50.
It's supposed to be X=0, Y=0.
Even when I look for the 0, 0 position, it's way outside the game window.
Does anyone know what could be causing this?

How is that possible? Your cursor position is the mouse position. Simply draw the cursor there.
Unless you are talking about the Windows cursor. In that case, yes, the input data in XNA will not match the Windows cursor movement. They probably apply some modifiers for acceleration, etc. You have to interprete the input data yourself. In other words, draw your own cursor.

Related

How do I move an object from off screen to just off screen? (Unity2D)

I have an enemy that can constantly move quite far away when the player moves too much, so I want to move it to the just off screen so it can move back into view without appearing suddenly. It would also be nice to have them reappear in the same direction from where they left. Currently have this just to check if .isVisible was working;
if(GetComponent<Renderer>().isVisible == false)
{
Debug.Log("Off Screen");
this.transform.Translate(0, distanceFromPlayer - 5, 0);
}
Having a constant distance wouldn't work because it would either appear on screen as if it was teleporting or appear too far off screen and not make it back. I understand how Camera.WorldToScreenPoint works, just couldn't find a way to make them appear in the same direction.
Look into Camera.ViewportToWorldPoint
So if you wanted to put them outside the left of the screen, you could do
cam.ViewportToWorldPoint(new Vector3(0 - enemyScreenOffset,.5,cam.nearClipPlane));
or if you wanted the upper right side of the screen
cam.ViewportToWorldPoint(new Vector3(1 + enemyScreenOffset,1,cam.nearClipPlane));
It depends on how your game works, but you might want to change the Z component of the Vector3 to be the enemyPosition so they don't change height.

Keep the mouse cursor within the game window

How can I make it so that the mouse cursor within the game window so that the cursor won't, for example, go into the other screen if you're using dual screens? I do not want to use the Screen.lockCursor since I want to be able to use the mousepointer inside the game and not make it hidden and centered which you do with the lockCursor function.
This might be able to help you Cursor.lockState = CursorLockMode.Confined;
For more information look at the API at https://docs.unity3d.com/ScriptReference/Cursor-lockState.html

C# XNA 4.0 Fullscreen Mouse Moves Into Second Monitor

The heading just about says it all. I derive from the XNA Game class, and set the IsMouseVisible = true. I have a dual screen setup with the secondary display being extended from the primary. I set my game to fullscreen mode (GraphicsDeviceManager.IsFullScreen = true). All works fine and fullscreen mode is initialized. The only problem I have is that the mouse does not stay bound within the fullscreen game. As soon as it reaches the edge of the fullscreen game, it seamlessly moves over onto the secondary monitor. Is this expected bahaviour?
I do a Mouse.GetState() to retrieve mouse info every frame on the same thread in which my game runs.
I have even tried setting Mouse.WindowHandle = GameWindowHandle before every call to GetState, but it does not change anything.
Any ideas? Or is this expected behaviour and I need to clamp the mouse to the fullscreen area myself?
Regards,
Riaan
Yes, exactly, clamp your mouse to the screen that you want it to stay on. This is similar to an approach that I (and many others) use when I implement sprite cursors for my games. I hide the system mouse, and clamp it to the center of the screen so that it doesn't show up on other monitors.
The only downside is that you have to un-clamp your mouse if you minimize the game, etc. Otherwise it stays stuck, which can be really annoying.

How to use Pinch functionality correctly in Windows Phone 7 for zoom in/out effects from pictures?

I'm trying to make a zoom system for a C#/XNA game I'm working on. What I have is the cameras position, the cameras current zoom (stored as a float) and the GestureSample instance.
I'm grabbing both positions of the pinchs and finding their center to make that my zoom in point, then if the person tries to pinch inwards/outwards I compare the length of the distance between the two fingers before and after the pinch drag action happened to determine to zoom in or out.
This kind of works but it feels a bit floaty. I also haven't figured out how I'm going to make it zoom towards a position where the user is pinching against. I get the middle point of the pinch and try to make the camera move in that direciton as the zoom gets larger but sometimes the camera gets to that point before 100% zoom and sometimes not ever.
It's all algorithm issues, I suppose what I want to know is if there is a simple straight forward way of doing this that I don't know of?
All you need to do is give your camera a target location (ie. the "middle point" of your pinch), and an acceleration ... the camera should then, independently of the pinch gesture, move towards the target location. This way, the camera will just end up at the right spot ... and on top of that you have a new feature for your camera :-)

How to limit cursor movement in WPF based app?

I'm working with a WPF app, more specifically a Canvas with draggable elements.
Once an item is being dragged, I'd like to limit the scope of cursor movement to inside the canvas where the items are being dragged about.
The event which can start a drag is shown below
private void WidgetCanvas_PreviewHeaderLeftMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
... logic to figure out if this is a valid drag, blah blah blah ...
this.IsDragging = true;
// TODO: clip the available cursor movement to actual width of Canvas
}
On the Preview-MouseUp, I'd like to simply "unclip" the cursor movement back to it's normal state.
I'll be monitoring the movement of the mouse once I start dragging (PreviewMouseMove), so worst case, I could manually check the position of the mouse and keep it constrained to the canvas, but that seems a little ugly.
Anyone have a better way to limit the cursor boundaries?
There's no clean way to do this and the not-so-clean ways will make your mouse cursor "jitter" at the border of the clipping area.
Moreover, I'd question if this is really a good idea. The user should really own the mouse and he or she generally gets frustrated when you try to artificially limit things that he or she owns.
If you want to provide feedback when the mouse leaves your canvas, maybe you could leave the item being dragged stuck along the border while the mouse button is still down? This would tell the user that he or she has left the target area without trying to impose limitations on where the mouse can go.
Good luck!
You should be able to do it using the ClipCursor native API.

Categories