Skip to main content
GameDev.net gamedev.net
🔒 Locked

Player Input System

Started by Code Sage Jan 15, 2015 at 4:25 AM 6 replies 5.1k views
Original Post
Code Sage
Code Sage

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.

Finalspace
Finalspace

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);
}
Irlan Robson
Irlan Robson

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.

Code Sage
Code Sage
Thanks for all these great replies guys. Very informative and helpful.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.