# Matching feet to terrain - Part 2

986 views

##### (This is a follow up to my blog entry here: https://www.gamedev.net/blogs/entry/2264506-matching-feet-to-terrain-using-ik/ )

This is a quick one just to show progress with getting 4 legged creatures matched to the terrain. While it was quite quick to get 2 legged creatures matching their feet to terrain using IK, 4 legged creatures has proved to be a whole other ball game entirely.

The few examples I've seen of 4 legged IK on the web have been using full body iterative solutions, so maybe I should be going this way. However using what I already had working I've so far been staying with the simple analytical solution for the legs, and using some maths and forward kinematics for the rest of the body.

First thing I did was try changing the pitch of the torso. I have been using a rotation based around the pelvis, not sure if this is the best approach but it seems to look rightish. Of course in real life you do not always hit a slope head on, so I wanted to be able to roll the torso too.

As well as this there is a twist in the spine so the body can follow the terrain better. A stiff spine looks very rigid (but is simpler). Then it is a case of lining up the legs to compensate for gravity (this isn't always switched on) and doing the IK legs to the terrain under each foot.

I've had quite a lot of problems with feet not being long enough to reach the ground, particularly with the short legged crocodile. You sometimes have to move the belly up to not hit the ground, but then the legs are too short to hit the ground!

As well as the central body there is also a forward kinematic system for moving the head and the tail.

It has been very difficult making things generic (fixing one thing on the crododile would break the elephant and vice versa) but I am getting there. There are also other creatures which are semi 4 legged (the monkey and chimp) but those are easier for the system to calculate. There are still some bugs of feet going through ground, and glitches, but it doesn't have to be perfect.

Very nice! How much "setup by artist" does it need? I guess that you are trying to make it as generic as possible to be out of the box? I also want to get down to implement it one day.

The elephant seems to have very low traction with the ground, almost as if its walking on ice. Do you have a method to deal with that?

Both the elephant and the croc (but more so the elephant) seem to be sliding a little - the leg movement isn't looking too bad otherwise though!

This is tricky stuff to get right, lots of fiddly little adjustments!

I'm glad you guys were interested, my implementation is very comically bad at the moment , but in the game the models are so small on screen I doubt players will notice. If they were relatively bigger I'd spend a lot longer on getting this right, in fact I will probably come back to it, but I feel I've wasted a couple of weeks on tweaking this already!

The sliding isn't actually as bad as in these videos, I should have said, in both cases I had scaled up the models just for the videos so the animation speed doesn't match the movement speed. In the game the match is much better as the animation speed is driven off how far the animals moves, I will try to do a later video soon with all in game.

Also the base animation for the croc is a very bad test anyway, I basically just wiggled the legs in blender!  Obviously also these are low poly and the skin weightings need some work too, I didn't envisage the joints moving so far when I made the models.

I have seen there is middleware available for doing this with quadrupeds, IKinema and Bik. I suspect they use a better iterative method and I will try this if I have time. But even with iterative I'm sure it took a lot of tweaking to get right. And I am slightly worried it will use more performance as it has to work on mobiles.

I could write much more on how it works (or fails to work lol) if people were interested.

One major change I would make if possible is instead of calculating the solution each frame from the original animation, I would calculate the solution from the previous frame solution, and limit the change in rotations (or even use damping), to prevent physically incorrect snapping. Also as a performance optimization and to further smooth it might be an idea to calculate the solution at a fixed lower tick rate (say 30fps or matching the underlying animation tick rate) and interpolate frames.

Here is example of setup for the most complex animal, crocodile. The others are simpler, especially 2 legged. All this is very hacky and subject to change but should give an idea that setup is not very involved, most is reading bone IDs from a txt file. I'll probably replace it with something much simpler.

num_feet = 4;
// Leg IK chains, end bone, chain length, chain effector, knee joint flip, shoulder joint flip
chain_leg[0].Create(23, 3, 0, true, false);
chain_leg[1].Create(27, 3, 0, true, false);
chain_leg[2].Create(12, 3, 0, false, true);
chain_leg[3].Create(16, 3, 0, false, true);

// Limit knee angles (smallest, largest)
ANGLES_FRONT(0, 104);
ANGLES_BACK(0, 80);

// hips and shoulders, might be inferred from the chains later
SetLegRoots(22, 26, 11, 15);

// chain for back, end bone ID, num bones, effector
chain_back.Create(2, 2, 1);
// chain for tail
chain_tail.Create(19, 3, 0);

// offset in model space from last tail bone joint
m_TailOffset_MS = 6000;
// croc uses an extra 3rd probe for torso orientation
Set_UseMiddleProbe(true);
// neck and 'nose' bone

// used for calcing head angles, some of these might be inferred from bones
m_NoseOffset_MS = 4000;
m_NeckNoseDist_WS = 0.2f * Scale;
m_GlobalZOffset_MS = 1500.0f; // overall bodge to the torso height above ground

## Create an account

Register a new account