Hot answers tagged mouse
33
The general approach to handling this in classic Win32 programming is to capture the mouse delta each frame, and then reset the mouse position to the center of the screen. You also want to make the mouse cursor invisible, obviously, as otherwise things look ugly.
You can do the same thing in XNA, you just work with a slightly different interface. So you ...
13
Based on your comment, here's the code I'm using to convert tile x,y values to on screen coordinates. Now, it doesn't take into account "3d tiles", everything is considered as being on the same plane, so if you're writing a game where that matters, this code will not work.
//this converts a map x/y coordinate into screen coordinates
//public, static ...
13
What you are seeing is not input lag.
To get the position of the cursor in Windows you can use WM_MOUSEMOVE or GetCursorPos. They both provide the position of the cursor after Windows has processed it, applying things like acceleration. This is the cursor position that Windows uses, including for drawing. And almost always what you want to use too.
This is ...
11
You're on the right track with figuring out when the mouse is transitioning from down to up and writing a handler for that. For a lot of games, it's good enough to treat a mouseUp event as a click.
Depending on your game, you're probably want to handle both events -- for example, in an RTS, you'll probably want to incorporate drag-box selecting as well as ...
10
When working with XNA in general you have to move from an event driven code paradigm to a loop driven code paradigm. Your update code loops 60 times/sec. So each time, look at the state of the mouse and if a button is down & pointer is within your rect, then branch to code you would normally place in an OnClick event.
10
The positions that you store within cursorX and cursorY hold the position of the cursor relative to top left corner of your screen monitor (since you add the ClientBounds to it). I'd guess that you used that exact position when drawing the circle, and since the Draw function takes a position relative to the top left corner of the game window, you'd be off by ...
7
The "delta time" used to be the time elapsed between two frame updates (but it can also be used in other contexts; this is usually the result of a time subtraction).
You can get the delta time in glut using the glutGet method and the GLUT_ELAPSED_TIME parameter, plus some operations. The following line returns the number of milliseconds since glutInit was ...
7
You are starting to ask very important and complicated questions. First some general wisdom... In order to reduce the load on the server you should do as much as possible on the client. And in order to eliminate cheating, you should do as much on the server. As you see, these are diametrically opposite, and you will be in a constant conflict between these ...
6
All you need to know is the relative angle between the player and the point where you are aiming at. It seems to me that you already have the angle, lets call that angle a.
Now for the bullet to point in the right direction you just rotate the bullet sprite with angle a. Now for the movement/direction. You need to convert the angle to a 2D Vector, you can ...
6
XNA only gives you just enough to build your own input classes - and you should do so considering the utility they offer.
I would recommend the mouse input class described in this blog post (source code). It detects a button being pressed, held or released, and lets you get that information either via polling or events.
On a related note, the same blog ...
4
"He who would learn to fly one day must first learn to stand and walk and run and climb and dance; one cannot fly into flying..."
You appear to want to draw a sprite that is always facing the mouse, correct? Then your first task must be to draw a sprite that is always facing something. Pick an arbitrary point and then do the math to draw the sprite so that ...
4
Is this for providing an aiming guide for your player? If that's the case, then I suppose you want to draw the reticle so that it remains at a fixed distance around the player, but always facing in the mouse's direction. Here's how I would do it, based on what you currently have:
// Distance to draw the reticle from the player
float distance = 100f;
// ...
4
How do you want this to behave? There are different ways to do this.
A simple option is to just move the object by some fixed number of world space units for each screen space unit (pixel, say) that the mouse moves.
Another option is to take the vector of mouse movement and project it onto the axis of movement through the normal projection/camera ...
3
Let's get the obvious stuff out of the way, the tool is there: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.mouse.setposition.aspx
In fullscreen it is easy to use, just do some controlled repositionings and the edge problem is gone. For some other games this could lead to the problem of a trapped mouse in windowed mode, but if you ...
3
I assume you mean that forwards and backwards are working fine (from what I can tell they should). In order to move left and right you merely need to make the Vector2 perpendicular and use almost the exact same logic:
public static class Vector2Extensions
{
public static Vector2 Perpendicular(this Vector2 vector)
{
// Swap y and x, and negate y.
...
3
Turns out you're in luck, kind of. http://superuser.com/questions/29432/using-two-mice-in-windows-7-dual-mouse-dual-cursor Because mice are just another kind of HID (human interface device) you can plug in as many as you want, but custom software is required to actually do anything with them. Beyond that link is a page to Microsoft's research called ...
3
You have somewhat answered your own question :) (everything after "so basically"). I don't have XNA here so YMMV with my code. Furthermore - it's (from what I can tell) usually a 1px border.
Using Rectangles
What you first need to do is determine the position in terms of your Viewport. What we do is create some rectangles and check if the mouse cursor is ...
3
When I was debugging mouse picking in my game, I had it set up to draw a line from my camera to where ever the picking function ended. It lets you know if the ray is going the right direction, and if the ray is ending too soon, or too late.
In your case, I'd have it draw a line between nearPoint and farPoint. Make sure those are the rays you want.
I ...
3
You should not use DirectInput. It has been completely deprecated in favor of RAWINPUT.
The answer that uses WM_MOUSEMOVE is not the same as RAWINPUT and I expect it to not be fast enough.
An example using RAWINPUT in a Win32 app is available on my blog
3
You can avoid the potentially O(N*M) containemt checks by simply calculating the tile's array index in O(1) time:
int pixelCoordToIndex(int x, int y)
{
return ((y / tileHeight) * numTilesX) + (x / tileWidth);
}
Your code is then reduced to:
System.out.println("Tile " + pixelCoordToIndex(x, y) + " clicked. X/Y val is (" + x + "," + y + ")");
...
3
The x and y you use here:
bounds.setBounds(x, y, width, height);
are the parameters x, y that are being passed into the method.
I am assuming that there is also an x and y in your Tile class. So you either need to rename the parameters or use this. in front of your variables.
public boolean inBounds(int x, int y){
Rectangle bounds = new ...
3
There is no 'best' - you need all of them.
If I'm firing a gun, I don't want to wait for the mouse to be released to trigger the weapon firing effect. And if you're dragging a box around a bunch of units, you start the drag action on mouse-down.
On the other hand, if I'm clicking a GUI item then I want to wait for the full click because that's what I'm ...
3
You're taking absolute world coordinates and feeding them to setLocalTranslation which, being a translation, sounds like it should take relative coordinates.
i.e. if your character is at (10,10) and you click (10,20), I'll bet your character will move to (20,30) - it moved 10 horizontally and 20 vertically, not to (10,20).
Instead you should find a method ...
3
In addition to using the code you have above you need to configure the axis in the input manager and name it correctly.
The accepted answer here may be of help: http://answers.unity3d.com/questions/6106/implementing-the-scrollwheel
3
While not technically a JAVA library SDL is a C library that I use extensively that does input and much more. It does however have bindings for java and can run on pretty much any platform in existence.
http://www.libsdl.org/languages.php
There is JSDL and SDLjava
3
Welp, long after I figured out the problem.
Early in my menu development I inherited the "Game" class from XNA. I forget why, I think it had to do with getting certain method to work or something. I guess I had removed the code that need the inheritance and kept the inheritance which screwed up my update method. It wanted to override it but I didn't do ...
3
While you can implement this using SDL_WarpCursor(), I've run into problems with that method on some platforms. I've had real problems with some platforms not reliably performing the WarpCursor() action, particularly when I've been calling it every frame.
Also, remember that on many platforms, the cursor is handled at a higher frequency than your app. ...
3
There are a few options, but probably the "standard" approach would be:
As soon as you detect the mouse click, transmit the command to the server (a message equivalent to "move to 40,40")
Give some immediate visual feedback (e.g. an animation at the clicked-on point)
Wait for the server to process the move command and return a confirmation of the movement
...
3
I think you may be confusing some terms. Theme Hospital is diamond shaped from what I can tell. That's the shape of the tiles, not the entire world. You can shape the entire would however you like. The shape of the world would be the same as the shape of the tiles if the world was square (meaning equal width and height), but you don't need to make it that ...
3
You can't confine the system cursor to a region with Flash. What you can do is hide the system mouse-cursor using Mouse.hide() (only works when the cursor is over your game-area), then use a custom cursor instead. There's also an event called Event.MOUSE_LEAVE which will fire whenever the mouse leaves the stage area. You can't tell where the mouse will be on ...
Only top voted, non community-wiki answers of a minimum length are eligible
