TheComet / ik

Minimal Inverse Kinematics library
MIT License
488 stars 70 forks source link

How to set and get the rotation of each joint #10

Open Mr-djh opened 4 years ago

Mr-djh commented 4 years ago

Hi there,

I would like to use your library for my inverse kinematics of Ergonomic project ,however the instructions are not very clear for a beginner.Mainly lie in the input and output of rotations.The questions can be listed as follows: 1、Before ik.solver.solve(solver),should we set the rotations of root->rotation and target->rotation in global space ? At the same time,should we set the rotation of child->rotation in local space? 2、After ik.solver.solve(solver),I want to get the rotation of each joint in order to realize the inverse kinematics,but I don't know whether the rotation that I get is a global rotation or a local rotation? 3、After ik.solver.solve(solver),whether the joint's rotation that I get is a delta rotation?

What's more,I have tried several forms of input and output of rotations, but the result is always unsatisfactory.Look forward to hearing from you.

TheComet commented 4 years ago

Hey!

1) Yes, you should write the rotations to each node->rotation field before calling ik.solver.solve(). These rotations are all in local space. root->rotation usually is always an identity rotation in most game engines, but if you do want to set this you can. For the root node, local space is the same thing as global space. target->rotation is set in global space.

2) After calling ik.solver.solve() you get the rotations back from each node->rotation field in local space.

3) You do not get delta rotations, you get the "solved" rotations. That is, each node will have an absolute orientation that represents the solution. If you want to get delta rotations you can subtract the solved rotations from the initial rotations that are stored in each node->initial_rotation field.

Please let me know if this helped.

This library has undergone a lot of changes and I must admit I am no longer certain if V1.1 fully works as intended. If you are unable to get satisfactory results then there is an older API V1.0 which definitely does work, as this is the version used in the Urho3D game engine. However, it is slightly different.

Did you find the wiki page? Most of the information should be here: https://github.com/TheComet/ik/wiki/Tutorial-1.1

Mr-djh commented 4 years ago

Thanks for your response!

According to your guidance,I updated my own program,But I encountered a problem ,namely, after calling ik.solver.solve(),the positions I get back from each node->position can work well;however, I use the rotations which I get back from each node->rotation to set the joint angle ,but I can not get the desired posture.Now I suspect I didn't use the solved rotations correctly or other reasons.Pardon my asking,whether you have relative sample program?

TheComet commented 4 years ago

Can you check if you have set the IK_ENABLE_JOINT_ROTATIONS flag? Without it, FABRIK does not calculate rotations.

solver->features |= IK_ENABLE_JOINT_ROTATIONS;

Do this once prior to ik.solver.solve().

Right now there are no samples that directly use this library unfortunately. There are only Urho3D samples that use the library indirectly. They're probably not that useful but if you want to go digging in Urho3D's source code you can find those here: https://github.com/urho3d/Urho3D/tree/master/Source/Samples/45_InverseKinematics and https://github.com/urho3d/Urho3D/tree/master/Source/Urho3D/IK

Mr-djh commented 4 years ago

I have set the "solver->flag |= IK_ENABLE_JOINT_ROTATIONS;".

I plan to use the library(API V1.0) and try to dig in the Urho3D's sourse code.

Thanks again.If there is any progress, I will communicate with you in time.

TheComet commented 4 years ago

Alright. The biggest difference in V1.0 is all of the node positions and rotations are in global space, and the function names are global rather than ik.group.func.

Hopefully it won't be long until I can release V2.0.

TheComet commented 4 years ago

Hey! I was going to test the code you posted a week or so ago just now but I think you deleted it.

Does this mean you got it to work? :)

Mr-djh commented 4 years ago

Thanks! I have corrected the code that I posted somedays ago and it can work,so I delete the code.

At the same time,I plan to study how to add constraints to joints,because I find that it(the virtual human) can go wrong all of a sudden without the constraints.