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

Input for moving

Started by bballmitch Apr 17, 2006 at 12:36 AM 5 replies 850+ views
Original Post
bballmitch
bballmitch
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).
Mitchen Games
blaze02
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.
bballmitch
bballmitch
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?

Mitchen Games
Meganan
Meganan
Have you put a high res timer into your program and scaled movement according to it? That could cause the pauses...
<-> Rate someone badly if they say something that has absolutely nothing to do with your problem; don't rate people badly because they tried to help you with their limited knowledge. <->
Sijmen
Sijmen
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.
Sneftel
Sneftel
Read up on the GetKeyState() function in the Windows API.
bballmitch
bballmitch
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().
Mitchen Games

Topic Locked

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

Sign in to reply to this topic.