Skip to content

feat: new 'IKangle' converter , inverse kinematics bone - #3623

Open
zaynopenapp wants to merge 12 commits into
synfig:masterfrom
zaynopenapp:master
Open

zaynopenapp wants to merge 12 commits into
synfig:masterfrom
zaynopenapp:master

Conversation

@zaynopenapp

@zaynopenapp zaynopenapp commented Aug 9, 2025 •

Copy link
Copy Markdown

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

ik angle converter
ik angle converter
Inverse Kinematic converter
IK angle converter. new converter for native IK 2025

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_IK class 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

  1. 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. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++.

Comment on lines +65 to +67
if (value.get_type() == type_angle)

{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There is a stray semicolon after the if condition, which makes the following block {...} execute unconditionally. This means the default parameters will be set for any ValueBase type, not just for type_angle. This is a critical bug.

    if (value.get_type() == type_angle)
    {

Comment on lines +29 to +30
#ifndef __SYNFIG_VALUENODE_IK_H
#define __SYNFIG_VALUENODE_IK_H

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
#ifndef __SYNFIG_VALUENODE_IK_H
#define __SYNFIG_VALUENODE_IK_H
#ifndef __SYNFIG_VALUENODE_IK_ANGLE_H
#define __SYNFIG_VALUENODE_IK_ANGLE_H

Comment on lines +45 to +55
enum Joint {
TWOBONE = 1,THREEBONE = 2,
};

enum Jenisbone {
ANIMAL= 1,HUMAN = 2,
};

enum Forbone {
BONE1 = 1,BONE2 = 2, BONE3 = 3,
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Please update the #endif to match the new include guard name for consistency.

Suggested change
#endif
#endif // __SYNFIG_VALUENODE_IK_ANGLE_H

Comment on lines +76 to +78
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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

Comment on lines +107 to +125
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());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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_alpha
  • bone2 -> joint_mode
  • type -> rig_type
  • for_bone -> output_bone_index

Vector delta = target - origin;
Real dist = delta.mag();
// ----- 2-BONE MODE -----
if (bone2==1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Comparisons are made against magic numbers (e.g., bone2==1). Please use the enums defined in the header (Joint, Jenisbone, Forbone) for these comparisons to improve readability and type safety. For example, this line should use Joint::TWOBONE.

    if (bone2 == Joint::TWOBONE) {

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)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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);

Comment on lines +218 to +232
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;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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;
}

@zaynopenapp
zaynopenapp marked this pull request as ready for review August 9, 2025 22:14
@BobSynfig BobSynfig changed the title new 'IK angle' converter feat(converter): new 'IK angle' converter Aug 10, 2025
@github-actions

Copy link
Copy Markdown

Thank you for attempting to improve your contribution description!
Below is how your updated contribution will appear in Synfig's release notes. Review it and make further improvements until you're satisfied!

✨ New Features

@zaynopenapp
zaynopenapp marked this pull request as draft August 12, 2025 22:57

@zaynopenapp zaynopenapp left a comment •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why my code error?

@rodolforg

Copy link
Copy Markdown
Contributor

Why my code error?

image

@zaynopenapp

zaynopenapp commented Aug 13, 2025 •

Copy link
Copy Markdown
Author

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

@zaynopenapp
zaynopenapp marked this pull request as ready for review August 13, 2025 10:52

@zaynopenapp zaynopenapp left a comment •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve fixed it according to Gemini’s suggestions, though not all of them. I’ve also tested the Windows artifact, and it runs just like the Linux version I built myself.
ik angle

@zaynopenapp

Copy link
Copy Markdown
Author

Below is a comparison of files from an IK example using a plugin and an IK angle converter:

ik file compare

Although compressing into a .sifz file reduces the file size, the original file still contains a large number of converters, which significantly impacts Synfig’s performance. This is only for a single IK rig object (a dinosaur). One can imagine how heavy it would become with 5 such objects using the IK plugin.

comparefile.zip

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
@codacy-production

codacy-production Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 84 complexity · 5 duplication

Metric Results
Complexity 84
Duplication 5

View in Codacy

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.

@SlamRockwell

Copy link
Copy Markdown

This would be amazing to have in Synfig and I hope the developers can pull it in soon! :)

@BobSynfig

Copy link
Copy Markdown
Collaborator

AppImage for test UNOFFICIAL-1.5.5-2026.05.06-test-pr-3623

@zaynopenapp

Copy link
Copy Markdown
Author

You can find the documentation for the IK angle converter here. Thank you.
link

ikangle_tuto

@BobSynfig
BobSynfig requested review from Svarov-RZM and rodolforg May 7, 2026 08:32
@zaynopenapp

Copy link
Copy Markdown
Author

This would be amazing to have in Synfig and I hope the developers can pull it in soon! :)

@SlamRockwell Thank you for your support. you can review this PR

Refactor: simplify the implementation without changing behavior.
@Svarov-RZM

Copy link
Copy Markdown
Contributor

I see I've been tagged on this, but it's a bit too much for me to handle (I am a sysadmin, remember).
Also, I don't use bones in Synfig so I won't be able to do a good test.

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.

@zaynopenapp zaynopenapp changed the title feat(converter): new 'IK angle' converter feat(converter): new 'IKangle' converter Jul 25, 2026
@zaynopenapp zaynopenapp changed the title feat(converter): new 'IKangle' converter feat: new 'IKangle' converter , inverse kinematics bone Jul 25, 2026

@zaynopenapp zaynopenapp left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this issue

Bone length can be animated now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants