Repository navigation
feat: new 'IKangle' converter , inverse kinematics bone - #3623
zaynopenapp wants to merge 12 commits into
Conversation
ik angle converter
ik angle converter
Inverse Kinematic converter
IK angle converter. new converter for native IK 2025
There was a problem hiding this comment.
Summary of Changes
Hello @zaynopenapp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
I've introduced a new 'IK angle' converter, which provides a simple inverse kinematics solution for Synfig. This change aims to finally bring inverse kinematics bones functionality to users, a feature that has been highly requested and discussed within the community for a long time.
Highlights
- New IK Angle Converter: I've implemented a new
ValueNode_IKclass to handle inverse kinematics calculations. - Flexible IK Configurations: The new implementation supports both 2-bone and 3-bone IK setups, providing flexibility for different rigging needs.
- Configurable IK Parameters: I've introduced several configurable parameters for the IK system, including pole position, target, individual bone lengths, flip direction, joint type (2-bone/3-bone), bone type (human/animal for 3-bone), target bone for angle output, and weight for 3-bone IK.
- Build System Integration: The new IK valuenode has been integrated into Synfig's build system and translation files, ensuring it's properly compiled and localized.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces a new 'IK angle' converter, which is a great addition for inverse kinematics. The implementation is quite comprehensive, covering both 2-bone and 3-bone IK systems. My review focuses on improving code clarity, maintainability, and fixing a critical logic bug. Key suggestions include correcting a faulty if statement, using enums instead of magic numbers for better readability, improving variable names, and some minor refactoring for idiomatic C++.
| if (value.get_type() == type_angle) | ||
|
|
||
| { |
There was a problem hiding this comment.
| #ifndef __SYNFIG_VALUENODE_IK_H | ||
| #define __SYNFIG_VALUENODE_IK_H |
There was a problem hiding this comment.
The include guard __SYNFIG_VALUENODE_IK_H does not match the filename valuenode_ik_angle.h. For consistency and to prevent potential name collisions, it's recommended to align the include guard with the filename.
| #ifndef __SYNFIG_VALUENODE_IK_H | |
| #define __SYNFIG_VALUENODE_IK_H | |
| #ifndef __SYNFIG_VALUENODE_IK_ANGLE_H | |
| #define __SYNFIG_VALUENODE_IK_ANGLE_H |
| enum Joint { | ||
| TWOBONE = 1,THREEBONE = 2, | ||
| }; | ||
|
|
||
| enum Jenisbone { | ||
| ANIMAL= 1,HUMAN = 2, | ||
| }; | ||
|
|
||
| enum Forbone { | ||
| BONE1 = 1,BONE2 = 2, BONE3 = 3, | ||
| }; |
There was a problem hiding this comment.
The enums Joint, Jenisbone, and Forbone are unscoped. It's modern C++ practice to use enum class for strong typing and to avoid polluting the enclosing scope.
Also, the names Jenisbone and Forbone are not very descriptive in English. Consider renaming them to something like RigType and TargetBone respectively for better readability and maintainability.
This would require corresponding changes in the .cpp file to use the new names and scoped enum access (e.g., Joint::TWOBONE would become ValueNode_IK::Joint::TWOBONE, and you'd need static_cast<int>() to get the integer value).
Example refactoring:
enum class Joint {
TWOBONE = 1, THREEBONE = 2,
};
enum class RigType { // Was Jenisbone
ANIMAL = 1, HUMAN = 2,
};
enum class TargetBone { // Was Forbone
BONE1 = 1, BONE2 = 2, BONE3 = 3,
};|
|
||
| /* === E N D =============================================================== */ | ||
|
|
||
| #endif |
| set_link("joint_bone",ValueNode_Const::create(int(1))); | ||
| set_link("t_bone",ValueNode_Const::create(int(1))); | ||
| set_link("f_bone",ValueNode_Const::create(int(1))); // 1 = for bone 1 ,2 for bone 2, 3 for bone 3 |
There was a problem hiding this comment.
The default values for joint_bone, t_bone, and f_bone are set using magic numbers (int(1)). It would be much clearer and safer to use the corresponding enum members defined in the header file.
set_link("joint_bone",ValueNode_Const::create(int(Joint::TWOBONE)));
set_link("t_bone",ValueNode_Const::create(int(Jenisbone::ANIMAL)));
set_link("f_bone",ValueNode_Const::create(int(Forbone::BONE1))); // 1 = for bone 1 ,2 for bone 2, 3 for bone 3| auto hitung_theta2_alpha = [](Real d, Real L1, Real L2, Real p) -> std::pair<Real, Real> { | ||
| L1 = (L1 > p) ? L1 : p; | ||
| L2 = (L2 > p) ? L2 : p; | ||
| Real cos_theta2 = synfig::clamp((d*d - L1*L1 - L2*L2) / (2 * L1 * L2), -1.0, 1.0); | ||
| Real theta2 = std::acos(cos_theta2); | ||
| Real cos_alpha = synfig::clamp((d*d + L1*L1 - L2*L2) / (2 * d * L1), -1.0, 1.0); | ||
| Real alpha = std::acos(cos_alpha); | ||
| return std::make_pair(theta2, alpha); | ||
| }; | ||
| static const Real precision = 0.000000001; | ||
| Vector origin = (*link_pole_)(t).get(Vector()); | ||
| Vector target = (*link_target_)(t).get(Vector()); | ||
| Real L1 = (*length_bone1_)(Time(0)).get(Real()); | ||
| Real L2 = (*length_bone2_)(Time(0)).get(Real()); | ||
| Real L3 = (*length_bone3_)(Time(0)).get(Real()); | ||
| bool flip = (*flip_)(t).get(bool()); | ||
| int bone2 = (*joint_bone_)(Time(0)).get(int()); | ||
| int type = (*t_bone_)(Time(0)).get(int()); | ||
| int for_bone = (*f_bone_)(Time(0)).get(int()); |
There was a problem hiding this comment.
Several variable and function names in this method are not in English (e.g., hitung_theta2_alpha) or are not very descriptive (e.g., bone2, type, for_bone). For better maintainability in an international project, please consider using clear English names. For example:
hitung_theta2_alpha->calculate_theta2_and_alphabone2->joint_modetype->rig_typefor_bone->output_bone_index
| Vector delta = target - origin; | ||
| Real dist = delta.mag(); | ||
| // ----- 2-BONE MODE ----- | ||
| if (bone2==1) { |
There was a problem hiding this comment.
| Real angle_to_target = std::atan2(delta[1], delta[0]); | ||
| auto [theta2, alpha] = hitung_theta2_alpha(dist, L4, L3, precision); | ||
| Real theta1 = angle_to_target - alpha; | ||
| if ((flip && type !=1) || (!flip && type == 1)) { |
There was a problem hiding this comment.
This boolean expression is a bit complex. It can be simplified for better readability. The expression (flip && type != 1) || (!flip && type == 1) is equivalent to an XOR operation: flip != (type == 1). Using the enum Jenisbone::ANIMAL for 1 would make it even clearer: flip != (type == Jenisbone::ANIMAL).
| Real dx3 = (sx - x0) - (x2 - x0); | ||
| Real dy3 = (sy - y0) - (y2 - y0); | ||
| Real dist3 = std::sqrt(dx3 * dx3 + dy3 * dy3); | ||
| auto [theta3, alphax] = hitung_theta2_alpha(dist3, L2, L3, precision); |
There was a problem hiding this comment.
The variable alphax is initialized here but is not used later in the function. This will likely cause a compiler warning. To signify that it's intentionally unused, you can use std::ignore. This will require including the <tuple> header.
auto [theta3, std::ignore] = hitung_theta2_alpha(dist3, L2, L3, precision);| ValueNode_IK::get_link_vfunc(int i)const | ||
| { | ||
| assert(i>=0 && i<link_count()); | ||
| if(i==0) return link_pole_; | ||
| if(i==1) return link_target_; | ||
| if(i==2) return length_bone1_; | ||
| if(i==3) return length_bone2_; | ||
| if(i==4) return length_bone3_; | ||
| if(i==5) return flip_; | ||
| if(i==6) return joint_bone_; | ||
| if(i==7) return t_bone_; | ||
| if(i==8) return f_bone_; | ||
| if(i==9) return weight_; | ||
| return 0; | ||
| } |
There was a problem hiding this comment.
This sequence of if statements can be refactored into a switch statement. This is more idiomatic for this kind of dispatch, often more readable, and can be more efficient.
ValueNode::LooseHandle
ValueNode_IK::get_link_vfunc(int i)const
{
assert(i>=0 && i<link_count());
switch(i)
{
case 0: return link_pole_;
case 1: return link_target_;
case 2: return length_bone1_;
case 3: return length_bone2_;
case 4: return length_bone3_;
case 5: return flip_;
case 6: return joint_bone_;
case 7: return t_bone_;
case 8: return f_bone_;
case 9: return weight_;
}
return 0;
}|
Thank you for attempting to improve your contribution description! ✨ New Features
|
fix enum RigType , enum TargetBone
|
Ok. Thank you. I will immediately fix the part that has the error. Edit BobSynfig: The image was not displayed in the quote, still taking empty space |
fix type
Added a new parameter 'color_bones' to Layer_Skeleton.
There is an update to the IK angle converter. It can now determine the positions of elbow 1 and elbow 2 (3 joint bones). Of course, it requires another converter such as a vector angle. It can also stretch the bones by calculating data from the bone length
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 84 |
| Duplication | 5 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
This would be amazing to have in Synfig and I hope the developers can pull it in soon! :) |
|
AppImage for test UNOFFICIAL-1.5.5-2026.05.06-test-pr-3623 |
|
You can find the documentation for the IK angle converter here. Thank you.
|
@SlamRockwell Thank you for your support. you can review this PR |
Refactor: simplify the implementation without changing behavior.
|
I see I've been tagged on this, but it's a bit too much for me to handle (I am a sysadmin, remember). I can do a surface level test of this PR if you want me to, but the code review must be done by someone more competent. |
Bone length can be animated now.




The new 'IK angle' converter is a simple inverse kinematics solution. In line with the issues discussed in #3571 or https://forums.synfig.org/t/inverse-kinematic-for-bones/3068, and (https://forums.synfig.org/t/ik-system/4132)it is hoped that inverse kinematics will finally be available for users who have long wanted Synfig to have IK bones.
forum: https://forums.synfig.org/t/bone-like-movement/588, https://forums.synfig.org/t/skeleton-bones-duplication/3650, https://forums.synfig.org/t/inverse-kinematic-for-bones/3068
sample here. https://forums.synfig.org/t/ik-angle-converter-test/16552 and https://youtu.be/kgnKCwXtjIQ
The basic idea behind my IK Angle converter is based on the geometric intersection of two circles.
Imagine a simple two-bone chain. The first circle is centered at the root bone with a radius equal to the length of the first bone. The second circle is centered at the target with a radius equal to the length of the second bone.
The intersection of these two circles gives the possible position of the intermediate joint (for example, the elbow or the knee). Once that joint position is known, the rotation angle of each bone can be calculated directly using vector angles.
Because this approach is based on geometry rather than an iterative solver, it produces a stable solution with very little computation. There is no need for repeated iterations such as those used by CCD or FABRIK for a simple two-bone chain.
To support different bending directions, the solver simply chooses one of the two possible intersection points (for example, left/right or up/down), depending on the desired pole direction.
This geometric approach is the core idea behind my IK Angle converter.
The distance between the root and the target determines whether the circles intersect. When they do, there are generally two possible solutions, corresponding to the two possible elbow or knee positions. The converter selects one of these solutions and computes the bone rotations from the resulting vectors.
interception_cycle_IK.webm