Jump to content
  • Advertisement

Matching feet to terrain using IK

Sign in to follow this  



For a long time I had been delaying finding a solution to feet etc interpenetrating terrain in my game. Finally I asked for suggestions here, and came to the conclusion that Inverse Kinematics (IK) was probably the best solution.


There seem to be quite a few 'ready built' solutions for Unity and Unreal, but I'm doing this from scratch so had to figure it out myself. I will detail here the first foray into getting IK working, some more steps are remaining to make it into a working solution.

Inverse Kinematics - how is it done?

The two main techniques for IK seem to be an iterative approach such as CCD or FABRIK, or an analytical solution where you directly calculate the solution. After some research CCD and FABRIK looked pretty simple, and I will probably implement one of these later. However for a simple 2 bone chain such as a leg, I decided that the analytical solution would probably do the job, and possibly be more efficient to calculate.

The idea is that based on some school maths, we can calculate the change in angle of the knee joint in order for the foot to reach a required destination.

The formula I used was based on the 'law of cosines':

I will not detail here but it is easy enough to look up.

For the foot itself I used a different system, I calculated the normal of the ground under the foot in the collision detection, then matched the orientation of the foot to the ground.


My test case was to turn off the animation and just have animals in an idle pose, and get the IK system to try to match the feet to the ground as I move them around. The end effect is like ice skating over the terrain. First I attempted to get it working with the main hero character.


The biggest hurdle was not understanding IK itself, but in implementing it within an existing skeletal animation system. At first I considered changing the positions of the bones in local space (relative to the joint), but then realised it would be better to calculate the IK in world space (actually model space in my case), then somehow interpolate between the local space animation rotations and the world space IK solution.

I was quite successful in getting it working until I came to blending between the animation solution and the IK solution. The problems I was having seemed to be stemming from my animation system concatenating transforms using matrices, rather than quaternions and translates. As a result, I was ending up trying to decompose a matrix to a quaternion in order to perform blends to and from IK.

This seemed a bit ridiculous, and I had always been meaning to see whether I could totally work the animation system using quaternion / translate pairs rather than matrices, and it would clearly make things much easier for IK. So I went about converting the animation system. I wasn't even absolutely sure it would work, but after some fiddling, yay! It was working.

I now do all the animation blending / concatenation / IK as quaternions & translates, then only as a final stage convert the quaternion/translate pairs to matrices, for faster skinning.

This made it far easier in particular to rotate the foot to match the terrain.


Another snag I found was that blender seemed to be exporting some bones with an 'extra' rotation, i.e. if you use an identity local rotation the skin doesn't always point along the bone axis. I did some tests with an ultra simple 3 bone rig, trying to figure out what was causing this (perhaps I had set up my rig wrong?) but no joy. It is kind of hard to explain and I'm sure there is good reason for it. But I had to compensate for this in my foot rotation code.

Making it generic

To run the IK on legs, I set up each animal with a number of legs, and the foot bone ID, number of bones in the chain etc. Thus I could reuse the same IK routines for different animals just changing these IK chain lists. I also had to change the polarity of IK angles in some animals .. maybe because some legs work 'back to front' (look at the anatomy of e.g. a horse rear leg).

The IK now appears to be working on most of the animals I have tested. This basic solution simply bends the knees when the ground level is higher than the foot placed by the animation. This works passably with 2 legged creatures but it is clear that with 4 legged creatures such as elephant I will also have to rotate the back / pelvis to match the terrain gradient, and perhaps adjust the leg angles correspondingly to line up with gravity.

At the moment the elephant looks like it is sliding in snow down hills. :)



To blend the IK solution with the animation is kind of tricky to get to look perfect. It is clear when the foot from the animation is at ground level or below, the IK solution should be blended in fully. At a small height above the ground I gradually blend back from the IK into the animation. This 'kind of' works, but doesn't look as good as the original animation, I'm sure I will tweak it.

Another issue is that when one leg is on an 'overhang', you can end up with a situation where the fully outstretched leg cannot reach the ground. I have seen that others offset the skeleton downwards in these cases, which I will experiment with. Of course this means that the other leg may have a knee bent further than physically possible. So there are limits to what can be achieved without rotating the animals pelvis / back.
 Anyway this is just description of the trials I had, hopefully helpful to those who haven't done IK, and maybe will generate some tips from those of you that have already solved these problems. :)

Sign in to follow this  


Recommended Comments

There are no comments to display.

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
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By Nilmani Gautam
      Hello and welcome everyone. I am starting another complete new Tutorial on 3d modelling and animation on blender
      Please comment what types of chapter could i include to make it complete from very beginning to exper
    • By GreenGodDiary
      Sup dudes and dudettes!
      I'm in the process of implementing an animation state machine and am currently making a 2D blendspace state for it.
      I think I've figured out how to blend the different clips together given an [x,y] coordinate but I have one problem I'm not sure how to solve; matching the different blended clips' animation speed.

      Say you have your run-o-the-mill twin-stick character locomotion blendspace, where max Y, zero X means running straight forward, and max Y, max X (in either direction) means running at an angle (thus blending run_forward with run_strafe).
      In this case the animations' speed probably match, so there's no worry. However, say I'm halfway up Y, meaning I'm "jogging", in the sense I'm halfway between walk_forward and run_forward, and my X is at some arbitrary point.
      How would I blend these animations together so that their speeds match? Would it be as simple as 'lerping' the animation speed of the walk towards the speed of the run and scaling the speeds of all the clips to match this speed?

      Sorry if the question is poorly written.
    • By bilbo92
      Hello guys!

      I want to learn to make my own assets, characters, model and so on for my own games, instead of having to rely on others assets (and be able to create the things I actually want, and not just what I can find that is avalible)
      So I have spend on/off the past month trying to figure out the basic of how it works and such, what to expect and which programs there are avalible and what they cost.
      Truth be told, considering how many different software, tutorials, and complexity of it all, it seems very hard to figure out where to actually start, with this. Especially because it seems as though most of any software that's commonly used when you search the interwebs, it seems as though they are all insanely expensive for a mere mortals. However, I looked at the software called ZBrush because I can see it's a software that is used in big companies like Blizzard, EA and a lot of other big studios, both related and unrelated to the gaming industry like movies and such.
      Though it is still a pricy piece of software it is the only software of the big ones that I found that is not a yearly or monthly subscription based software, but a perpetual software license purchase software. A one year autodesk Maya subscription cost more than a perpetual software license from the newest ZBrush version that last forever. In a perfect world were money didn't matter to me, I'd just buy them all and use whatever I liked the best, but I'm not made of gold so I'll have to choose between one or the other of them all. But it just so happens that while I looked up on ArtStation few days ago that I can see there is a 20% of on the newest version of ZBrush until October 1st 11:59 PM which will save me 179$ which is A LOT to me  so I seriously consider to buy ZBrush now that it seems to be the cheapest software of the big ones there is (that I have found so far) and especially now when I have a chance to save A LOT!

      However, as I said before: considering how many different software, tutorials, and complexity of it all, it seems very hard to figure out where to actually start. Even when I look at ArtStation, DeviantArt it seems as though a lot of people even have used both Maya, PS, ZBrush or up to 6-7 programs whereas half of them I havn't even come across or heard of before. So this basicly just make me more confused whenever I think I finally got how it works... So at this point I don't even know anymore, which software does what, which software do I actually NEED, and which software is just "luxury" software (one where they might use 'software A' for this and 'software b' for this, but you can actually do the same in both 'software a' and 'software b', it is just easier to different things in boths software).

      So the question I want to make is truly: If I want to make a character for my game (let's say Sombra Cyberspace from Overwatch), fully animated, complete and all like she is in Overwatch, into my Unity 3D game, which software do I actually use to make all the required steps? How many "steps" are there in the process of doing this? Do I actually need more than Unity and modelling software like Zbrush (If that is what they are called - please correct me if I'm wrong)? Is ZBrush the choice for me or am I just doing a very costly mistake, because it can't actually do what I think it can? Or do I end up having to buy a lot of other different software aswell if I buy ZBrush in contrast to some other software solutions?  Can I actually live without ZBrush or any of all these expensive software solutions?
      When you answer please bare in mind I'm a newb, so please refrain from using very technical answers, or to answer like you think I know something, because I don't. Talk to me as if I'm a total rookie and moron and explain it to me as if I were a 5-year old  
    • By addictCoderCS
      Hi there,

      I'm in need of a dedicated artist who wants to help in the development of a game for switch.
      Stylized/Cartoony style is preferred.

      Quality 2D Character design
      Ability to create custom weapons and armor + animating them
      Experience with 2D puppet rigging

      Experience creating 3D environment objects. Exterior and Interior

      Please do not respond if you don't have the time to dedicate to a larger size project.
      If you aren't good with 2D art and animation, but great with 3D environment objects, you can still contact me.

      If you're interested, contact me at addictcodercs@gmail.com
      Please send portfolio. Bonus for animated art. Double bonus for characters switching gear. Triple bonus for animated characters switching gear. 

      Thank you all,
    • By null777
      Hello, I'm working on developing a 2D platformer. In the game, the player can run/jump/etc, and they can also often shoot at the same time that they're performing these other actions. For programming these actions, I have no issue constructing the relevant FSMs and getting them to behave as intended. My issue comes when I need to find the right frames or animations for the sprite to show. Should the game just check if the player is shooting and what state they're in and then have the right animations hardcoded in to play in each possible outcome of that if statement? I've considered some potentially more elegant solutions like having a normal sprite sheet and then a shooting sprite sheet where the shooting sheet has the corresponding shooting frame at the same position in the sheet. Then the animator would just have to load the correct frame of its current sheet no matter what, and whenever the player is shooting, it simply swaps out the regular sprite sheet for the shooting one.

      Basically I can think of a few different ideas that sort of solve this problem, but none of them seem ideal. I wanted to know what the generally accepted best practices are for this type of problem. Any advice would be appreciated.

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!