Walking - Logic

Started by
12 comments, last by O-san 12 years, 10 months ago
The OP is right about the movement scheme he is trying to copy. Games like pokemon use that kind of movement, where you move in a grid and can turn if you tap the movement key or move if you press and hold.

My advice: Don´t measure how long the key is down, but whether it has been lifted. Subtle difference. Also, don't measure frames, measure milliseconds. For instance, I don't know if your target FPS is 60 or 30, but 6 frames would be a delay of either 0.2 or 0.1 seconds, or 200 to 100 milliseconds.

Say the key is pressed. You set the character's state to move in the desired direction. He immediatly turns in place. Then you poll for key up events. If the key is lifted before 200 milliseconds, he stops moving (facing the new direction). Else he continues to move. Then you can have a continuous movement. When they finally release the key, you finish the movement into the next tile.
Advertisement
This may be overkill for such simple case, but I have found useful to separate the control into two parts - planning and animation.

Something along the lines (rough pseudocode):


enum State { IDLE, TURN_LEFT, WALK_LEFT };

function () {
if (left_key_down)
++left_key_down_frames;
else
left_key_down_frames = 0;

if(left_key_down_frames <= 6)
player.plan (TURN_LEFT)
else if (left_key_down_frames > 0)
player.plan (WALK_LEFT)
else
player.plan(IDLE)
}

class Player {
State requested_state;
State current_state;
};

Player::plan (State request) {
// One can test the possibility of action (for example movement) here
requested_state = request;
}


Player::timeSlice (double worldTime) {
animate (worldTime);
if (animation.finished ()) {
switch (requested_state) {
case IDLE:
current_state = IDLE;
break;
case TURN_LEFT:
// Here we should test what is current state - if already facing left we do not have to turn
current_state = TURN_LEFT;
break;
case WALK_LEFT:
if (can_move_left ()) {
current_state = WALK_LEFT;
break;
} else {
current_state = IDLE;
break;
}
break;
}
startAnimation (worldTime);
}
}


Thus AI-controlled characters can share the animation code, only planning is implemented differently.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

The OP is right about the movement scheme he is trying to copy. Games like pokemon use that kind of movement, where you move in a grid and can turn if you tap the movement key or move if you press and hold.

My advice: Don´t measure how long the key is down, but whether it has been lifted. Subtle difference. Also, don't measure frames, measure milliseconds. For instance, I don't know if your target FPS is 60 or 30, but 6 frames would be a delay of either 0.2 or 0.1 seconds, or 200 to 100 milliseconds.

Say the key is pressed. You set the character's state to move in the desired direction. He immediatly turns in place. Then you poll for key up events. If the key is lifted before 200 milliseconds, he stops moving (facing the new direction). Else he continues to move. Then you can have a continuous movement. When they finally release the key, you finish the movement into the next tile.


Hmm that's exactly what I said he should do, a few posts up. :)
I am using a wanted angle and an actual angle in my game. If the wanted angle is not aligned with the actual angle the player should turn. If the angles are aligned the player should walk. Then always strive to have the actual angle aligned with the wanted using small increments (time based). Good luck!

This topic is closed to new replies.

Advertisement