RavEngine
Loading...
Searching...
No Matches
ik_two_bone_job.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_ANIMATION_RUNTIME_IK_TWO_BONE_JOB_H_
29#define OZZ_OZZ_ANIMATION_RUNTIME_IK_TWO_BONE_JOB_H_
30
31#include "ozz/animation/runtime/export.h"
32#include "ozz/base/platform.h"
33
34#include "ozz/base/maths/simd_math.h"
35
36namespace ozz {
37// Forward declaration of math structures.
38namespace math {
39struct SimdQuaternion;
40}
41
42namespace animation {
43
44// ozz::animation::IKTwoBoneJob performs inverse kinematic on a three joints
45// chain (two bones).
46// The job computes the transformations (rotations) that needs to be applied to
47// the first two joints of the chain (named start and middle joints) such that
48// the third joint (named end) reaches the provided target position (if
49// possible). The job outputs start and middle joint rotation corrections as
50// quaternions.
51// The three joints must be ancestors, but don't need to be direct
52// ancestors (joints in-between will simply remain fixed).
53// Implementation is inspired by Autodesk Maya 2 bone IK, improved stability
54// wise and extended with Soften IK.
55struct OZZ_ANIMATION_DLL IKTwoBoneJob {
56 // Constructor, initializes default values.
58
59 // Validates job parameters. Returns true for a valid job, or false otherwise:
60 // -if any input pointer is nullptr
61 // -if mid_axis isn't normalized.
62 bool Validate() const;
63
64 // Runs job's execution task.
65 // The job is validated before any operation is performed, see Validate() for
66 // more details.
67 // Returns false if *this job is not valid.
68 bool Run() const;
69
70 // Job input.
71
72 // Target IK position, in model-space. This is the position the end of the
73 // joint chain will try to reach.
74 math::SimdFloat4 target;
75
76 // Normalized middle joint rotation axis, in middle joint local-space. Default
77 // value is z axis. This axis is usually fixed for a given skeleton (as it's
78 // in middle joint space). Its direction is defined like this: a positive
79 // rotation around this axis will open the angle between the two bones. This
80 // in turn also to define which side the two joints must bend. Job validation
81 // will fail if mid_axis isn't normalized.
82 math::SimdFloat4 mid_axis;
83
84 // Pole vector, in model-space. The pole vector defines the direction the
85 // middle joint should point to, allowing to control IK chain orientation.
86 // Note that IK chain orientation will flip when target vector and the pole
87 // vector are aligned/crossing each other. It's caller responsibility to
88 // ensure that this doesn't happen.
89 math::SimdFloat4 pole_vector;
90
91 // Twist_angle rotates IK chain around the vector define by start-to-target
92 // vector. Default is 0.
93 float twist_angle;
94
95 // Soften ratio allows the chain to gradually fall behind the target
96 // position. This prevents the joint chain from snapping into the final
97 // position, softening the final degrees before the joint chain becomes flat.
98 // This ratio represents the distance to the end, from which softening is
99 // starting.
100 float soften;
101
102 // Weight given to the IK correction clamped in range [0,1]. This allows to
103 // blend / interpolate from no IK applied (0 weight) to full IK (1).
104 float weight;
105
106 // Model-space matrices of the start, middle and end joints of the chain.
107 // The 3 joints should be ancestors. They don't need to be direct
108 // ancestors though.
109 const math::Float4x4* start_joint;
110 const math::Float4x4* mid_joint;
111 const math::Float4x4* end_joint;
112
113 // Job output.
114
115 // Local-space corrections to apply to start and middle joints in order for
116 // end joint to reach target position.
117 // These quaternions must be multiplied to the local-space quaternion of their
118 // respective joints.
119 math::SimdQuaternion* start_joint_correction;
120 math::SimdQuaternion* mid_joint_correction;
121
122 // Optional boolean output value, set to true if target can be reached with IK
123 // computations. Reachability is driven by bone chain length, soften ratio and
124 // target distance. Target is considered unreached if weight is less than 1.
125 bool* reached;
126};
127} // namespace animation
128} // namespace ozz
129#endif // OZZ_OZZ_ANIMATION_RUNTIME_IK_TWO_BONE_JOB_H_
Definition simd_math_config.h:121
Definition ik_two_bone_job.h:55
Definition simd_math.h:1066
Definition simd_quaternion.h:39