I am currently making a game in Java from scratch and I have a certain way of handling player input, but I don't feel like it is as efficient as it could be. Anyone feel like sharing how they handle Player Input or how they believe it should be handle in the most efficient matter possible. I realize there are probably a million ways to do it, but I'd just like to hear from the community and see what they have come up with.
Player Input System
http://www.google.com/search?q=site%3Agamedev.net+Spiro+input&oq=site%3Agamedev.net+Spiro+input&sourceid=chrome&es_sm=93&ie=UTF-8&gws_rd=ssl
L. Spiro
It’s not about efficiency, it is about correctness, usability, and response.
http://www.google.com/search?q=site%3Agamedev.net+Spiro+input&oq=site%3Agamedev.net+Spiro+input&sourceid=chrome&es_sm=93&ie=UTF-8&gws_rd=ssl
L. Spiro
Thanks for the response I am currently giving it a read.
I use a state based singleton class for keyboard, gamepad and mouse input. The states are polled at the start for every frame.
Important is that i record the previous state - to differentiate between key down (Continues) and key press (Single key).
In my player class i can then do something like that in its update method (Jump example):
if (Input.isKeyPressed(Input.KEY_UP)) {
if (!jumpRequested && onGround) {
// Jump key is pressed initially
jumpRequested = true;
jumpForce = maxJumpForce;
} else {
// Jump key is hold down
jumpForce -= stepJumpForce;
if (jumpForce <= 0f) {
jumpForce = 0f;
}
}
} else {
if (jumpRequested) {
// Jump key is released
jumpRequested = false;
}
}
// Apply force impulse when there is some jump force left
if (jumpRequested && jumpForce > 0f) {
Vec2 f = new Vec2(0f, bodyMass * jumpForce);
Vec2 c = body.getPosition();
body.applyLinearImpulse(f, c, true);
}
You might take a look at the article "Designing a Robust Input Handling System for Games" and the accompanying demo (linked at the bottom of the article).
I second L. Spiro, but I suggest that you take a look here too; if you implement in the way I described, you'll not find any problems.
Also, only after you get correct inputs synchronized with the game time that you start re-mapping inputs.
I wrote up something about this for Java:
BTW, I'm a huge fan of do-it-yourself in Java. :-)