Sign in to follow this  

Input for moving

This topic is 4256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As of right now, I have a game where 'w','a','s', and 'd' move my character and the arrows change the view of my character. When I press 'w' and then hold 'a' as well, though, my character doesn't move diagonal. Instead, my character will move foreward, and then it will move left. Or when I hold down 'w' and then use the arrow keys, problems occur with the Input (no errors or anything, but It doesn't work properly). I've looked into some REALLY complicated stuff to solve this problem, like DirectInput. Please post the easiest way to solve this problem (even if it is DirectInput in which case please post a tutorial).

Share this post


Link to post
Share on other sites
Use vectors.

Here goes some pseudocode:

Start with vDirection = (0, 0).
If the player is holding 'w': vDirection += (0, 1) //up
If the player is holding 'a': vDirection += (-1, 0) //left
If the player is holding 's': vDirection += (0, -1) //down
If the player is holding 'd': vDirection += (1, 0) //right
Normalize vDirection.
vDirection *= (Player's max speed).
//multiply in the frame time here to get time based movement.
(Player's position) += vDirection.

This takes care of a lot of everything (I think). If you hold down A + D, you won't move. If you hold down W + D, you will move diagonally. Also, you can never move faster than your max speed. Like in GoldenEye for N64, you could strafe and move forward and run faster than just moving forward.

Edit: Also, DirectInput is only necessary if you want to use multiple mice (mouses), multiple keyboards, or if you want joystick support. Win32 will work fine for almost any game if used properly.

Share this post


Link to post
Share on other sites
Quote:
Original post by blaze02
Use vectors.

Here goes some pseudocode:

Start with vDirection = (0, 0).
If the player is holding 'w': vDirection += (0, 1) //up
If the player is holding 'a': vDirection += (-1, 0) //left
If the player is holding 's': vDirection += (0, -1) //down
If the player is holding 'd': vDirection += (1, 0) //right
Normalize vDirection.
vDirection *= (Player's max speed).
//multiply in the frame time here to get time based movement.
(Player's position) += vDirection.

This takes care of a lot of everything (I think). If you hold down A + D, you won't move. If you hold down W + D, you will move diagonally. Also, you can never move faster than your max speed. Like in GoldenEye for N64, you could strafe and move forward and run faster than just moving forward.

Edit: Also, DirectInput is only necessary if you want to use multiple mice (mouses), multiple keyboards, or if you want joystick support. Win32 will work fine for almost any game if used properly.


I already have you what you described here. My problem is that there are delays when buttons are pressed. For example, if you press 'w' you will move one increment, it will pause, and then you will start moving like you normally should. I have seen this problem in many unofficial OpenGL programs. I hope I have described well enough my "pause" problem. If not please let me know. Otherwise, can someone help me solve my problem?

Share this post


Link to post
Share on other sites
Quote:
Original post by Meganan
Have you put a high res timer into your program and scaled movement according to it? That could cause the pauses...


This is key repeat. No idea how to solve it though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Meganan
Have you put a high res timer into your program and scaled movement according to it? That could cause the pauses...


I've been thinking of doing that for other reasons, but I know thats not causing the problem.

I'll look up getKeyState().

Share this post


Link to post
Share on other sites

This topic is 4256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this