woensdag 21 maart 2012

Character Control


Dear readers,

Yesterday as I was sitting in the train on my way home, I sat next to our artist-intern Sander. As I didn't really know what to write for my blog yet I asked him what he would like to know in programming. He asked me how characters are controlled and how when a character is in the air it 'knows' when it's back on the ground again. So that's what I'll be writing about today :) Thanks Sander!


Controlling a character can be done in several ways and one way might work better than the other depending on what we need. I'll shortly describe three ways of doing it:
1. Through the setting of the character model position. Simply put: when the user presses the forward key we change the model position that corresponds to the character moving forward. Depending on the model we might play an animation for this as well.
2. Through the playing of animations. The problem with the first way is that it is difficult to get the animation and movement visually coherent. For example: the character may be moving faster than the legs are moving. We can match this, but due to animation blending it'll never be exactly right (luckily most of the times you won't notice it too much). Instead we can make the character move by playing an animation. In this case the artist actually moves the model as he creates the animation ensuring that movement and animation match. So if we want to move the character forward we play animation 'WalkForward' or if we want to make the character strafe we play animation 'Strafe', etc.
3. By using physics: creating a rigidbody and attaching the character model to this rigidbody. A rigidbody is a physics object and the advantage of using this way is that we don't have to check for collisions ourselves, the physics engine takes care of it for us. In the first ways I had not talked about this, but as soon as you have a character in a world you'll most likely have to deal with obstacles (which the model cannot go through) and uneven surfaces such as slopes. To make the character move in this way, we'll apply forces on the rigidbody and as the rigidbody moves so will the character model as it is linked to the rigidbody. Animations can be played as well but just as in the first way it will be difficult to get the movement and animation to match visually.


As for a character being in the air and detecting when it's back on the ground again. You could keep it really simply and keep your entire world flat (no slopes) so you'd know the ground is always at y = 0 (for example). In this way a character knows it's back on the ground again when it's y-position is 0 again (assuming the pivot point of the model is at the bottom). You could also start playing a landing animation when the model's y-position is at 0.3. Of course a flat world is a rather boring world. If you have physics we could also use the collision events. Whenever the physics engine gives us a collision event between the character and the ground we know we have touched the ground. In this way however we might be too late with our landing animation: we might want to play the animation before we actually hit the ground. For this we can cast rays: cast a ray from the character downwards and check if that ray hits the ground and what the distance between the hit point on the ground and the character is. If smaller than a certain distance, start playing the animation. Of course there's a lot more to this all (which you'll discover when you start working on such a project), but these are the basics.

-- Stijn

3 opmerkingen:

  1. So for every difference in walking and running speeds, you plan on using separate animations?

    BeantwoordenVerwijderen
  2. Dear Robert,

    No, I personally wouldn't, but it's possible. Instead I'd change the rate (speed) at which the animation is played.

    -- Stijn

    BeantwoordenVerwijderen
  3. Why is the weapon in the top image different, while the soldiers look the same in both images?

    BeantwoordenVerwijderen