Hot answers tagged

34

Although you are targeting desktops, there will be players on (gaming) laptops and for some of them, it will be an inconvenience to get a mouse before being able to play your game. It would sound like a good thing to me, if you were to support alternative control schemes or customizable controls. This is not a very "sciency" answer - I've just run into this ...


31

To simulate a time lag, use a circular buffer to store the last N frames' mouse positions. Store the current mouse position each frame. In your control calculations, use the oldest mouse position from the buffer instead of the current mouse position.


22

It really depends what you mean by "assume". Are you making this assumption at the point of designing your gameplay mechanics? Or at the point of deciding whether or not to implement fully customizable key bindings? You could mean "I assume real gamers have a 3 button mouse, therefore I don't need to offer the option to rebind bayonet-thrust to a keyboard ...


13

Coroutines are fun: using UnityEngine; using System.Collections; public class DoubleClickTest : MonoBehaviour { private float doubleClickTimeLimit = 0.25f; protected void Start() { StartCoroutine(InputListener()); } // Update is called once per frame private IEnumerator InputListener() { while(enabled) { //Run as long as this is activ ...


12

You can implement IPointerEnter and IPointerExit interfaces and keep boolean for 'over state': using System; using UnityEngine; using UnityEngine.EventSystems; public class TestOver : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { public bool isOver = false; public void OnPointerEnter(PointerEventData eventData) { Debug....


11

v1.7.0+ Pixmap pm = new Pixmap(Gdx.files.internal("cursorImage.png")); Gdx.graphics.setCursor(Gdx.graphics.newCursor(pm, 0, 0)); pm.dispose(); Before v1.7.0 Pixmap pm = new Pixmap(Gdx.files.internal("cursorImage.png")); Gdx.input.setCursorImage(pm, 0, 0); pm.dispose(); The hotspot parameters represent the "tip" of the cursor. For example, the operating ...


10

The solution is SDL_SetRelativeMouseMode. How can I have missed that.


10

Many laptops lack a middle button, especially those with a trackpad, and you need special software to emulate it. Mac laptops have only one button. Right-click is pretty easy (two finger click) and not uncommon in Mac games, and the two finger drag to scroll isn't bad, but only in slower paced games. However, there is no concept of a middle click in the ...


7

First you need to calculate the vector pointing from your player to the current mouse position. This can be done by subtracting the player's position with the mouse's position: mouse_x, mouse_y = pygame.mouse.get_pos() rel_x, rel_y = mouse_x - self.x, mouse_y - self.y Then calculate the angle: angle = math.atan2(rel_y, rel_x) This will calculate the ...


5

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 matrices....


5

A matrix simply applies some transformation on the coordinates of the object in order to send them to another coordinate system. In this case, the camera matrix converts world coordinates to screen coordinates. You can revert the process (that is, convert screen coordinates back to world coordinates) by transforming the screen coordinates with the inverse of ...


5

Accordingly to http://legacy.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html you can make this like this: while (Keyboard.next()) { bool pressed = Keyboard.getEventKeyState(); int key = Keyboard.getEventKey(); if(pressed) processKeyPress(key); else processKeyRelease(key); } For more detail, you can google "Buffered input vs Unbuffered input"...


5

Some people use a trackball (some trackballs have no middle mouse button), trackballs generally are just as good pointing devices for players as ordinary mouses. The trackball I linked has no middle mouse button, and (at least on Windows) has an unusable scrollbar; despite these shortcomings I managed to finish quite few action games using it ;).


5

I can't read English. But UniRx may be able to help you. https://github.com/neuecc/UniRx#introduction void Start () { var clickStream = Observable.EveryUpdate().Where(_ => Input.GetMouseButtonDown(0)); var bufferStream = clickStream.Buffer (clickStream.Throttle (TimeSpan.FromMilliseconds (250))).Publish ().RefCount (); bufferStream.Where (...


4

I think the best way to solve this is by using a neural network that has been trained extensively on a lot of data. Draw your own shapes, make your friends draw them etc. It would then be able to guess which one the user tries to draw even if it's not perfect. More info on how to model a neural network to recognize hand drawn shapes can be found here


4

You're averaging the total travel of the mouse on a frame. The travel of the mouse is proportional to the speed the player is moving the mouse and the duration over which you measure the movement. Assuming the player has been moving the mouse at approximately the same speed for several frames in a row, if some of those frames were shorter (less time between ...


3

The computer graphics term for what you're trying to do is picking. The issue with picking from an HTML canvas is that the canvas doesn't understand anything about sprites -- all it knows about is pixels. In more general terms, canvas is a purely graphical representation -- it stores no semantic information. It has no idea what you wanted those pixels to ...


3

SDL events are events. They are fired in response to the user doing something, not fired continuously. The mouse down event is only called when the button is first pressed. The key down events are only called when a key is pressed down. They keydown events may get repeatedly called if you have key repeat enabled (which exists to easily simulate the ...


3

As per this MSDN article: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.mousestate.x.aspx, The mouse location is based on the top left of the game window. So, if the mouse escapes the window, the mouse location will be negative. Also, some windows counts a pixel or two to the left of the screen, so this is not an uncommon behavior. ...


3

For SDL2 there seems to also be the function SDL_SetWindowGrab if you want to enable and disable it. SDL_SetWindowGrab(SDL_Window* window, SDL_bool grabbed)


3

While Trevor's updated answer does reflect the current state of SDL2, you can also trap the mouse by creating your SDL_Window with the SDL_WINDOW_INPUT_GRABBED set. Be aware that I've run into some strange issues on Linux (presumably due to some X windowing quirk) where you cannot ever exit the window unless SDL_QUIT is called (even if another OS event ...


3

Yes, you should use WM_INPUT for things like camera movement. Why? Because it doesn't matter what the mouse's factory DPI is. Pretty much all games give the user a "mouse sensitivity" setting, where they can tweak a custom DPI value (they don't need to know it's the DPI!) to fit their preferences. When it comes to GUI, you should still use the ...


3

If you're using win32 then you can handle mouse and keyboard events using RawInput. Info can be found on MSDN. You would handle windows messages for raw input devices in the window process. For example, if handling the input for a keyboard and mouse, register those devices in the WM_CREATE case of your application window process like so: switch( uMsg ){ ...


3

This is usually solved by changing glfwSetInputMode with GLFW_CURSOR from GLFW_CURSOR_HIDDEN to GLFW_CURSOR_DISABLED. The behavior differences are due to the differences in OS APIs that GLFW uses and how they interact with the windowing system. In the documentation, it says that DISABLED should be used for 3D camera controls, and HIDDEN should be used when ...


3

When you resize your game resolution you need to update every single rectangle in your game (every button, etc) and multiply it by a proper scale, so that the "hitboxes" would work, which would be a lot of work. So, a smarter way of achieving the same result is simply by multiplying your mouse position by a Vector2 scale = new Vector2(originalWidth / width, ...


3

The velocity is the difference between the new position and the last position. velocity = newPos - oldPos The vector direction is the normalized velocity. direction = velocity.normalized Rigidbody should update velocity each frame, even if you are using MovePosition(), however, if you need to know what the velocity will be before the object is actually ...


3

Translate mouse input to rotation on the client-side. Then send the new view direction to the server, not raw mouse input. Usually there is more than one way to change the players view direction (keybindings, for example). When you just handle direction updates you don't have to handle this as a separate case on the server (not a particularly important ...


3

InputStream stream = new FileInputStream("C:\\path\\to\\image.png"); BufferedImage image = ImageIO.read(stream); int width = image.getWidth(); int height = image.getHeight(); int[] pixels = new int[width * height]; image.getRGB(0, 0, width, height, pixels, 0, width); // convert image to RGBA format ByteBuffer buffer = ...


3

Unity 4.6 UI doesn't really like those OnMouse events, so we can't use OnMouseOver() for example. (Those are just for Colliders or GUIElements.) Instead, we use Pointer properties from Selectable under EventSystem. You can add the following script on your Canvas, or individually on UI transforms, or just attach EventListeners to them. using UnityEngine; ...


3

I ended up using a coroutine, because (from what I understand) the delay will be constant regardless of the performance of the device running the game. This is my code: private IEnumerator DelayedInput() { Vector3 a = Input.acceleration; Vector2 m = Input.mousePosition; yield return new WaitForSeconds(delay); accelerometer = Vector3.Lerp(...


Only top voted, non community-wiki answers of a minimum length are eligible