Player Input System

Started by
6 comments, last by Code Sage 9 years, 3 months ago

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.

Advertisement
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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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

- Jason Astle-Adams

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:

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-keyboard-and-mouse-r2439

BTW, I'm a huge fan of do-it-yourself in Java. :-)

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

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

This topic is closed to new replies.

Advertisement