Sign in to follow this  

Handling Release of Movement Key with 8-Directional Mouselook

This topic is 2485 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

So I'm trying to implement a system in a Top Down Shooter where the player character is moved using the WASD keys and turned using the mouse much in the style of an FPS.

When the user presses/releases a movement key, the character is sent a message indicating the action to perform. E.g. Pressing W sends MOVE_FORWARD_BEGIN and releasing W sends MOVE_FORWARD END.

That worked by setting the character's x and y velocities to the appropriate value when pressed and setting to 0 when released. That was fine when I was just messing around, changing the character's rotated direction by hand and then recompiling and seeing what happened. Another thing is that directional movement didn't work in this system, but I was going to get that working eventually by adding when pressed and subtracting when released and figuring that out, presumably it would've been quite easy.

Now, I have to deal with something like: the player holds down W to walk forward, then rotates the character to the right without stopping. Naturally, this has the effect of changing the character's orientation while continuing to walk as though it hadn't changed at all.
To rotate the character, a ROTATE message is sent to the character object along with the angle to rotate.

Clearly, rotation being performed means that the player's velocity must then be recalculated and applied immediately somehow. However, that doesn't seem straightforward to me primarily because I need to know what messages are currently "active", which is to say what keys are pressed and haven't been released. Checking what keys are currently down would be quite sloppy since that function really shouldn't need to know anything about the exact control scheme being used.

My current system is a function that takes the message and uses it in a switch statement with blocks for each message.
Maybe I need to extend it somehow, perhaps with some kind of stack of messages recently received or is that unnecessary?
Essentially, if the character is moving forward (relative to his orientation) and is turned, it should continue by moving forward relative to its orientation. I just don't really have any way that I can see of knowing if it's moving forward at the moment.
Thoughts?

Share this post


Link to post
Share on other sites
Or maybe some kind of structure that holds the current move direction(s)?

Does the basic idea of sending a message to indicate to start moving in a direction and stop moving in a direction seem sound? Or is it a bit inflexible?

Share this post


Link to post
Share on other sites
If you wanted to continue this method you could just accept multiple "messages" per frame in some kind of buffer, and each frame have each character parse through every message it has.

For instance a character is moving forward and turning to the SE you would send the message MOVE_FORWARD_BEGIN and ROTATE.

Then when you process each message you just process every message that is available.

Personally for user movement now I just modify it directly when I receive the input like this:
if(KEYDOWN == 'w')
{
Player.Velocity += 5;
}

Just some options.

Share this post


Link to post
Share on other sites
[quote name='yewbie' timestamp='1298500923' post='4778171']
If you wanted to continue this method you could just accept multiple "messages" per frame in some kind of buffer, and each frame have each character parse through every message it has.

For instance a character is moving forward and turning to the SE you would send the message MOVE_FORWARD_BEGIN and ROTATE.
[/quote]

I think that'd just lead to the same problem I have now... yeah. The problem is that after rotation, the velocity has to change to reflect the new direction and it currently has no way of knowing what direction it's moving in.

I've had a think about it... and it seems to me that if I want to go with an event based approach where pressing a key causes movement to begin and releasing causes movement to stop, then I probably need some kind of member variable that stores the current move direction(s).
Something like bool moveDir[4];

Then on receiving ROTATE, I can just zero both velocities, look at where it's currently going and send messages corresponding to those directions. So the function would become recursive in that case I suppose.

Anyone have any thoughts on that? Sound reasonably good?

Share this post


Link to post
Share on other sites

This topic is 2485 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