# Input for moving

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

## 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 on other sites
Use vectors.

Here goes some pseudocode:

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 on other sites
Quote:
 Original post by blaze02Use vectors.Here goes some pseudocode:Start with vDirection = (0, 0).If the player is holding 'w': vDirection += (0, 1) //upIf the player is holding 'a': vDirection += (-1, 0) //leftIf the player is holding 's': vDirection += (0, -1) //downIf the player is holding 'd': vDirection += (1, 0) //rightNormalize 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 on other sites
Have you put a high res timer into your program and scaled movement according to it? That could cause the pauses...

##### Share on other sites
Quote:
 Original post by MegananHave 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 on other sites
Read up on the GetKeyState() function in the Windows API.

##### Share on other sites
Quote:
 Original post by MegananHave 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().

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013378
×