Control Inverse Kinematics in-game?

I’m wondering if it’s possible to have some control over inverse kinematics during the game.

For example, have the player’s hand reach out to an object, rather than having a pre-built animation of the character reaching out for something (which has the obvious limitation of only being able to reach for things that are the same height every time).

Put it this way… I’d like to be able to grab the player’s hand and move it to a specific object.

You can script the inverse kinematics to rotate and transform of your parent/child relationships. This can be done in .js or C#

Cheers,

I have done this type of thing multiple times in Unity. It is done on the heron’s legs in the island demo, and on the enemies hands/gun in avert fate.

It requires you know what you are doing with code, but its pretty easy.

Awesome. Thanks guys. I’m not ready to start coding that kind of thing yet, but I just wanted to make sure it’s possible.

I might be able to dig up so old code from way back in 1997 (Oh Snap! 10 years ago). If I do I’ll post it on the wiki

Or you could hack the code in the Island demo and get it out of the bird like Yoggy said.

Cheers,

Google for info and implimentations of “CCD IK” (Cyclic Coordinate Descent) That should be of good use to you.

-Jeremy

Yea i had to implement this in another engine recently… I based off of the old gdmag article 1998 Game Developer Magazine Companion Source Page the CCD3d is the c++ implementation. it works pretty smooth :slight_smile:

I’m going to have to do it in unity at some point i believe! if i ever break down and get a mac to make the switch :wink:

also heres some in a java implementation, http://mrl.nyu.edu/~perlin/gdc/ik/ik.java.html though i didn’t use that one myself.

I am new to Unity and am trying to understand the heron IK code in the island demo.

As far as I can see, it basically looks at the difference in terrain height below the heron’s two feet, and the foot over the highest terrain is moved up by the terrain height difference, and the knee by half the difference (I assume that the ankle inherits the adjustment of the knee):

		raise = (rightHeight - leftHeight) * 0.5;
		
		rightKnee.position.y += raise;
		rightAnkle.position.y += raise;

Won’t this make the leg shorter? This is not a critique, mind you; I am just trying to make sure I understand the code correctly, and there doesn’t seem to be any code that makes sure that the length of the thigh and shin is maintained constant.

Also, are the rotation and length of the bones automatically adjusted to fit the new points? The rightKnee and rightAnkle are just transforms:

	rightKnee = myT.FindChild("HeronAnimated/MasterMover/RootDummy/Root/Rhip/knee3");
	rightAnkle = rightKnee.FindChild("ankle3");

But if you just move the positions of the transforms and don’t adjust their rotation, how come the legs are rendered correctly? I am unclear how manipulating bones in Unity work…

Also, could raycasts have been used instead of querying the height of just the terrain?

Thanks in advance for any clarification,
Rune

Okay, I have looked further on it, and no, there are no automatically adjusted bones. I tried exaggerating the raise effect in the heron in the island demo and it then shows that the legs gets horribly disjointed and distorted. The reason is that when translating the bone transformations, like in the heron code, the bones no longer fit together like they’re supposed to. The problem is illustrated in the attached illustration.

I’m all for hacks that get the job done, and in the island demo, the deformation of the herons’ legs is so subtle that the error isn’t noticed. However, to call it inverse kinematics is wrong - it is not even proper forward kinematics. I’m just writing this here, to prevent that others might be confused if they try to learn IK from the island demo.

Rune

Right. IK solves the needed rotations for each bone/joint iteratively to reach the target end effector position.

I have ported the implimentation of CCD IK that DaveBuchhofer linked above (1998 Game Developer Magazine Companion Source Page) to C# in Unity and it works fine.

There is also Analytical IK, but IIRC it’s not the best fit for games where the IK chain has to reach for something that might be out of reach. CCD is good for that.

-Jeremy

How do you use these scripts you implemented? I tried dragging them onto an object, but Unity won’t allow it. I use Javascript for everything so far and don’t know the C# syntax.

Basically, how can I actually implement these scripts for CCD IK on an actual in game character. What I want to do is to manually animate the IK in game by dragging objects around in the game. Any idea how to do this?