Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Hot answers tagged

85

First of all, XNA also uses C# too so it's the same programming language. And although the underlying API might have some differences from DirectX, that has nothing to do with jumping, so the same tutorials or answers should apply here. Also, there are numerous ways to implement this, depending on a lot of factors. What I'll describe below is just one of the ...


41

Yes, this is normal for a real-time game to try to use 100% CPU to perform as fast and good as it can. So that player sees as much frames per second or as good physics simulation or anything else as his PC can provide. In your case - No, this looks like an inefficient design, to take a thread and make it poll for events in a loop (while true do ...


37

Yes, when you want your game to run on PC, you should think from an early design stage on how to best utilize the mouse. The same applies to the input features of all other platforms you consider targeting. One of the main differences between a good port and a bad port to a different platform is how much effort you invested into accommodating the different ...


35

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.


28

You can't. At least, not as a game developer. As a gamer, you can purchase more expensive keyboards with "anti-ghosting" features, but otherwise the limitation is part of the hardware itself, so there's nothing you can do in software to solve it. Check out this demo page to see how keyboard ghosting works, plus a demo: http://www.microsoft.com/...


24

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


22

Split this into several layers. At the lowest layer you have raw input events from the OS. SDL keyboard input, mouse input, joystick input, etc. You might have several platforms (SDL is a least-common-denominator lacking several input forms, for instance, which you might later care about). You can abstract these with a very low-level custom event type, ...


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


21

There is no one perfect mapping that gives you a platform specific abstraction, because obviously most of the identifiers that make sense for a 360 controller are wrong for a PlayStation controller (A instead of X, B instead of Circle). And of course a Wii controller is another thing altogether. The most effective way I've found to deal with this is to use ...


19

What you're trying to do is simple to solve without having to resort to doing any timing on the input (in fact I recommend you not to do so in this case - timing is more useful for scenarios such as limiting the firing rate of a spaceship). If you want for only the initial press of the key to be registered, the way it's usually done is: Keep track of ...


19

Keyboards have hardware limitations on how many keys can be pressed and recognized at the same time. Your code is not the problem here, it's most likely the design of the keyboard.


15

If you stop calling it "pushing forward" on the joystick/mouse, and start calling it "pushing up" (which is the way that most players think of it -- particularly the ones who don't play flight simulators), then the "invert Y axis" name makes complete sense, since pushing up causes the player to look down. Edit: The fundamental issue here is people's mental ...


14

Distance-Based You will basically need to take two things into consideration: a threshold (inversely a deadzone) and the dominant vector. Given an enum like the following: public enum Direction { None, Up, Down, Left, Right } You would find the dominant direction (if the deadzone isn't in play) and use that. // You could make this user-configurable. ...


14

What is often used is an intermediate Intent System which abstracts the input and keeps track of the context and relevant gamestates. The Intent system will stop transmitting inputs when the simulation is paused for example. It also handles the mapping between controller events and intents (move in direction, run, shoot, reload...). This way your other ...


13

void Main::processInput() { Uint8* keystate = SDL_GetKeyState(NULL); //continuous-response keys if(keystate[SDLK_LEFT]) { } if(keystate[SDLK_RIGHT]) { } if(keystate[SDLK_UP]) { } if(keystate[SDLK_DOWN]) { } //single-hit keys, mouse, and other general SDL events (eg. windowing) while(...


13

Recognising arbitrary shape vectors is hard! A simpler technique is to use a grid and count the edges crossed by the stroke drawn by the user. This list of edges is used as a "signature" which is compared against a dictionary of signatures of pre-defined shapes. Algorithm Sub-divide the input area into a grid (eg: 3x3 with infinite edges), and give each ...


12

Since asked by the thread starter, I elaborate on event managers. I think this is a good way to handle input in a game. An event manager is a global class which allows to both register callback functions to keys and to fire off those callbacks. The event manager stores registered functions in a private list grouped by their key. Each time a key gets fired, ...


11

An SDL_KEYDOWN event is only sent when the key is first pressed. You will receive an SDL_KEYUP event when it's released. You'll want to handle moving in code which gets called every frame, not in response to an event. Inside Avatar::handle_input, you'll instead want to set variables to tell you whether each key is up or down, and update those variables as ...


11

Keyboards have a key matrix, where the buttons have been arranged into something roughly square, with the keyswitches each being tied to one row and one column. The keyboard activates each row and then reads the columns. If you sketch this out you will see that some button combinations must activate "phantom" keys. You can fix this with one diode per button (...


10

I wrote a series of articles about building a platform game from the ground up using modern technologies and it includes how I handled the simple physics: http://www.wildbunny.co.uk/blog/2011/12/14/how-to-make-a-2d-platform-game-part-2-collision-detection/ However if you want something more high-tech it's entirely possible to approach this using full a ...


10

Did you search in the interactive fiction community? They still write parsers and some try to push the envelope by implementing new techniques such as natural language processing. See for example this link for articles describing approaches used: http://ifwiki.org/index.php/Past_raif_topics:_Development:_part_2#Parsing


10

The XNA Keys struct is not marked with the Flags Attribute which means that it's not legal to | a load of keys together like that. Keys keyCombo = Keys.Left | Keys.A; Console.WriteLine(keyCombo); Will print NumPad5, which is clearly not what you want here! You've never been able to do this in any version of XNA, I think the book is just wrong here :O ...


10

How about you use a Queue<T>? There are 4 significant members of Queue<T> you can use: Queue.Enqueue(T item) - put your item at the end of the queue. T Queue.Peek() - look at the first item without removing it. T Queue.Dequeue() - get and remove the first item. Queue.Count - check how many items are in queue. Sample usage Here's what could ...


10

This is actually really simple. All you have to do is add another ClickListener which listens for Right Clicks (the default only listens to left clicks). To do this all you have to do is this: someButton.addListener(new ClickListener(Buttons.RIGHT) { @Override public void clicked(InputEvent event, float x, float y) { //do whatever } }...


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


9

The term you want is 'natural language processing', or NLP. However, bear in mind that formal methods are designed to try and understand real world texts, whereas you only usually need something that works for a limited subset of your natural language. Typically you can start out with a simple grammar and vocabulary, then write a parser for it. A grammar ...


9

Your problem is the fact that you're only looking at KEYDOWN events. What you need to do is toggle a boolean value when a key is pressed or released. Something like this would work: # event loop for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: # check for key ...


9

Moving the joystick in a complete circle gives you the upper bounds of the joystick range, but the at rest (i.e. no input) position of the joystick can't be accurately determined from those values. You could assume that that rest position is the exact center of the available range but that is only true in ideal circumstances. Over time controllers wear in ...


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