Skip to content
Viewing as a member
crates · rust · bevy

Closed-form hinge IK forkbve

Two-bone limbs solved on a rest-measured hinge axis — one atan2, one acos, no iteration — with flexion limits, a name-based skeleton role map, a parallel Bevy solver, and a FABRIK chain for everything else.

Legs, arms, any rig

A closed-form solve for anything with a hinge in the middle, a FABRIK pass for anything without, and a bone-name map that finds the limbs on an Epic, Mixamo or Blender skeleton.

  • Solve — closed-form hinge, flexion ceiling, reach report.
  • Rig — bone roles by name, body halves for masking.
  • Bevy — IkLimb goals, par_iter solve, serial apply.
kinetreePackage
2024Edition
0.1.0Version
MITLicense

What it gives you

Features

Closed-form hinge

solve_limb bends the middle joint on the rest hinge axis and swings the root to aim the tip. Exact for knees and elbows; no pole vector to author.

Flexion limits

LimbLimits plus solve_limb_with cap the bend, keep the bend side, and return Reach::Clamped when the goal needed more than the joint allows.

Skeleton roles

role_of and Skeleton::learn resolve Epic, Mixamo and Blender bone names to roles and sides, so one rig profile drives any humanoid.

Bevy plugin

IkLimb, IkLimbBones and LimbOutput; solve_limbs runs par_iter_mut over limbs, apply_limbs writes rotations serially before transform propagation.

Chain fallback

solve_chain is a FABRIK pass with an Effort budget for tails, tentacles and anything without a hinge.

Engine-agnostic core

glam only with default-features off; libm feature for deterministic transcendentals across server and client.

Questions

Frequently asked

What is the kinetree crate?

kinetree is a Rust library for two-bone limb inverse kinematics. A limb with a hinge in the middle, such as a leg or an arm, is solved in closed form on a hinge axis taken from the rest pose. One atan2 and one acos, no iteration, no pole target, no convergence threshold. The core depends only on glam; a bevy feature adds components, a plugin and a parallel solver system.

How does it pick the knee direction?

From a RestHinge, the hinge axis expressed in the root bone's basis. It is measured from a bent rest pose with from_rest, or named outright with from_local_axis when the bind pose is straight and leaves nothing to measure. The axis follows the thigh, so the knee points where the hip points, and a small pre-bend on the calf keeps the solver from picking the backward root.

Does it enforce joint limits?

Yes. LimbLimits sets a flexion ceiling; solve_limb_with clamps the hinge to it, keeps the bend on the same side, and reports Reach::Clamped so the caller can drop the pelvis or shorten the step instead of locking the knee.

Can it run outside Bevy?

Yes. With default-features off and the std feature on, the crate is glam and nothing else, so the same solver can sit inside a Godot gdext cdylib or an Unreal module. A libm feature routes every transcendental through libm for bit-for-bit results across machines, which matters when a server re-simulates a client's limb.

How does it find bones on an unknown rig?

role_of maps a bone name to a Bone role (pelvis, thigh, calf, foot, ball, spine, clavicle, upper arm, lower arm, hand, neck, head) with a side, covering Epic, Mixamo and common Blender naming. Skeleton::learn ingests a whole name list and answers by role, and Bone::half says which half of the body a bone belongs to, so a caller can mask animation clips off the legs. The kbve-registry tests hold bonedb rows to the same role vocabulary.

API at a glance

use glam::{Quat, Vec3};
use kinetree::{LimbLimits, LimbPose, RestHinge, solve_limb_with};
let rest = RestHinge::from_local_axis(Vec3::NEG_X).unwrap();
let pose = LimbPose { root: hip, mid: knee, tip: ankle, root_basis: thigh_world_rotation };
let solve = solve_limb_with(&pose, &rest, goal, LimbLimits::flexion(160f32.to_radians()));
// solve.hinge_turn about solve.hinge_axis at the knee, then solve.root_swing about the hip;
// solve.reach is Reach::Exact, Clamped or Degenerate; solve.tip_after(&pose) previews the ankle.
ItemPurpose
RestHinge::from_rest(root, mid, tip, root_basis)Measure the hinge axis from a bent rest pose.
RestHinge::from_local_axis(axis)Name the axis when the bind pose is straight.
solve_limb, solve_limb_withClosed-form solve, optionally with LimbLimits.
hinge_angle, solve_hingeRead the current bend; solve the hinge turn alone.
solve_chain, segment_lengths, tip_errorFABRIK for chains without a hinge.
role_of, Bone, Side, Half, SkeletonRig-independent bone identity.
IkLimb, IkLimbBones, LimbOutput, KinetreePlugin, KinetreeSystems, bone_world_transformBevy integration (feature bevy).

Tests live in crates/kinetree/tests (limb, chain, rig, plugin). Run them with moon run kinetree:test.

Discuss on the forum

Questions, fixes and notes on this page live in the KBVE forum.