Skip to content

Commit

Permalink
Merge pull request #51368 from TwistedTwigleg/GSOC_2020_Working_Branc…
Browse files Browse the repository at this point in the history
…h_IK_SQUASHED

New and improved IK system for Skeleton3D - Squashed!
  • Loading branch information
akien-mga authored Aug 16, 2021
2 parents 1ee61a5 + 5ffed49 commit fff9a45
Show file tree
Hide file tree
Showing 39 changed files with 6,027 additions and 286 deletions.
12 changes: 12 additions & 0 deletions core/math/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,18 @@ Quaternion Basis::get_rotation_quaternion() const {
return m.get_quaternion();
}

void Basis::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction) {
// Takes two vectors and rotates the basis from the first vector to the second vector.
// Adopted from: https://gist.github.com/kevinmoran/b45980723e53edeb8a5a43c49f134724
const Vector3 axis = p_start_direction.cross(p_end_direction).normalized();
if (axis.length_squared() != 0) {
real_t dot = p_start_direction.dot(p_end_direction);
dot = CLAMP(dot, -1.0, 1.0);
const real_t angle_rads = Math::acos(dot);
set_axis_angle(axis, angle_rads);
}
}

void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const {
// Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
// and returns the Euler angles corresponding to the rotation part, complementing get_scale().
Expand Down
2 changes: 2 additions & 0 deletions core/math/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class Basis {
Quaternion get_rotation_quaternion() const;
Vector3 get_rotation() const { return get_rotation_euler(); };

void rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction);

Vector3 rotref_posscale_decomposition(Basis &rotref) const;

Vector3 get_euler_xyz() const;
Expand Down
65 changes: 64 additions & 1 deletion doc/classes/BoneAttachment3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,76 @@
A node that will attach to a bone.
</brief_description>
<description>
This node must be the child of a [Skeleton3D] node. You can then select a bone for this node to attach to. The BoneAttachment3D node will copy the transform of the selected bone.
This node will allow you to select a bone for this node to attach to. The BoneAttachment3D node can copy the transform of the select bone, or can override the transform of the selected bone.
The BoneAttachment3D node must either be a child of a [Skeleton3D] node or be given an external [Skeleton3D] to use in order to function properly.
</description>
<tutorials>
</tutorials>
<methods>
<method name="get_external_skeleton" qualifiers="const">
<return type="NodePath" />
<description>
Returns the [NodePath] to the external [Skeleton3D] node, if one has been set.
</description>
</method>
<method name="get_override_mode" qualifiers="const">
<return type="int" />
<description>
Returns the override mode for the BoneAttachment3D node.
</description>
</method>
<method name="get_override_pose" qualifiers="const">
<return type="bool" />
<description>
Returns whether the BoneAttachment3D node is overriding the bone pose of the bone it's attached to.
</description>
</method>
<method name="get_use_external_skeleton" qualifiers="const">
<return type="bool" />
<description>
Returns whether the BoneAttachment3D node is using an external [Skeleton3D] rather than attempting to use its parent node as the [Skeleton3D].
</description>
</method>
<method name="on_bone_pose_update">
<return type="void" />
<argument index="0" name="bone_index" type="int" />
<description>
A function that is called automatically when the [Skeleton3D] the BoneAttachment3D node is using has a bone that has changed its pose. This function is where the BoneAttachment3D node updates its position so it is correctly bound when it is [i]not[/i] set to override the bone pose.
</description>
</method>
<method name="set_external_skeleton">
<return type="void" />
<argument index="0" name="external_skeleton" type="NodePath" />
<description>
Sets the [NodePath] to the external skeleton that the BoneAttachment3D node should use. The external [Skeleton3D] node is only used when [code]use_external_skeleton[/code] is set to [code]true[/code].
</description>
</method>
<method name="set_override_mode">
<return type="void" />
<argument index="0" name="override_mode" type="int" />
<description>
Sets the override mode for the BoneAttachment3D node. The override mode defines which of the bone poses the BoneAttachment3D node will override.
</description>
</method>
<method name="set_override_pose">
<return type="void" />
<argument index="0" name="override_pose" type="bool" />
<description>
Sets whether the BoneAttachment3D node will override the bone pose of the bone it is attached to. When set to [code]true[/code], the BoneAttachment3D node can change the pose of the bone.
</description>
</method>
<method name="set_use_external_skeleton">
<return type="void" />
<argument index="0" name="use_external_skeleton" type="bool" />
<description>
Sets whether the BoneAttachment3D node will use an extenral [Skeleton3D] node rather than attenpting to use its parent node as the [Skeleton3D]. When set to [code]true[/code], the BoneAttachment3D node will use the external [Skeleton3D] node set in [code]set_external_skeleton[/code].
</description>
</method>
</methods>
<members>
<member name="bone_idx" type="int" setter="set_bone_idx" getter="get_bone_idx" default="-1">
The index of the attached bone.
</member>
<member name="bone_name" type="String" setter="set_bone_name" getter="get_bone_name" default="&quot;&quot;">
The name of the attached bone.
</member>
Expand Down
158 changes: 145 additions & 13 deletions doc/classes/Skeleton3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
Adds a bone, with name [code]name[/code]. [method get_bone_count] will become the bone index.
</description>
</method>
<method name="bone_transform_to_world_transform">
<return type="Transform3D" />
<argument index="0" name="bone_transform" type="Transform3D" />
<method name="add_bone_child">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="child_bone_idx" type="int" />
<description>
Takes the given bone pose/transform and converts it to a world transform, relative to the [Skeleton3D] node.
This is useful for using the bone transform in calculations with transforms from [Node3D]-based nodes.
Expand All @@ -40,13 +41,47 @@
Removes the global pose override on all bones in the skeleton.
</description>
</method>
<method name="clear_bones_local_pose_override">
<return type="void" />
<description>
Removes the local pose override on all bones in the skeleton.
</description>
</method>
<method name="execute_modifications">
<return type="void" />
<argument index="0" name="delta" type="float" />
<argument index="1" name="execution_mode" type="int" />
<description>
Executes all the modifications on the [SkeletonModificationStack3D], if the Skeleton3D has one assigned.
</description>
</method>
<method name="find_bone" qualifiers="const">
<return type="int" />
<argument index="0" name="name" type="String" />
<description>
Returns the bone index that matches [code]name[/code] as its name.
</description>
</method>
<method name="force_update_all_bone_transforms">
<return type="void" />
<description>
Force updates the bone transforms/poses for all bones in the skeleton.
</description>
</method>
<method name="force_update_bone_child_transform">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<description>
Force updates the bone transform for the bone at [code]bone_idx[/code] and all of its children.
</description>
</method>
<method name="get_bone_children">
<return type="PackedInt32Array" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns an array containing the bone indexes of all the children node of the passed in bone, [code]bone_idx[/code].
</description>
</method>
<method name="get_bone_count" qualifiers="const">
<return type="int" />
<description>
Expand Down Expand Up @@ -74,6 +109,19 @@
Returns the overall transform of the specified bone, with respect to the skeleton, but without any global pose overrides. Being relative to the skeleton frame, this is not the actual "global" transform of the bone.
</description>
</method>
<method name="get_bone_global_pose_override" qualifiers="const">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
<description>
</description>
</method>
<method name="get_bone_local_pose_override" qualifiers="const">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns the local pose override transform for [code]bone_idx[/code].
</description>
</method>
<method name="get_bone_name" qualifiers="const">
<return type="String" />
<argument index="0" name="bone_idx" type="int" />
Expand All @@ -96,16 +144,49 @@
Returns the pose transform of the specified bone. Pose is applied on top of the custom pose, which is applied on top the rest pose.
</description>
</method>
<method name="get_bone_process_orders">
<method name="get_bone_rest" qualifiers="const">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns the rest transform for a bone [code]bone_idx[/code].
</description>
</method>
<method name="get_modification_stack">
<return type="SkeletonModificationStack3D" />
<description>
Returns the modification stack attached to this skeleton, if one exists.
</description>
</method>
<method name="get_parentless_bones">
<return type="PackedInt32Array" />
<description>
Returns an array with all of the bones that are parentless. Another way to look at this is that it returns the indexes of all the bones that are not dependent or modified by other bones in the Skeleton.
</description>
</method>
<method name="get_bone_rest" qualifiers="const">
<method name="global_pose_to_local_pose">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="global_pose" type="Transform3D" />
<description>
Returns the rest transform for a bone [code]bone_idx[/code].
Takes the passed-in global pose and converts it to local pose transform.
This can be used to easily convert a global pose from [method get_bone_global_pose] to a global transform in [method set_bone_local_pose_override].
</description>
</method>
<method name="global_pose_to_world_transform">
<return type="Transform3D" />
<argument index="0" name="global_pose" type="Transform3D" />
<description>
Takes the passed-in global pose and converts it to a world transform.
This can be used to easily convert a global pose from [method get_bone_global_pose] to a global transform usable with a node's transform, like [member Node3D.global_transform] for example.
</description>
</method>
<method name="global_pose_z_forward_to_bone_forward">
<return type="Basis" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="basis" type="Basis" />
<description>
Rotates the given [Basis] so that the forward axis of the Basis is facing in the forward direction of the bone at [code]bone_idx[/code].
This is helper function to make using [method Transform3D.looking_at] easier with bone poses.
</description>
</method>
<method name="is_bone_rest_disabled" qualifiers="const">
Expand All @@ -115,6 +196,15 @@
Returns whether the bone rest for the bone at [code]bone_idx[/code] is disabled.
</description>
</method>
<method name="local_pose_to_global_pose">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="local_pose" type="Transform3D" />
<description>
Converts the passed-in local pose to a global pose relative to the inputted bone, [code]bone_idx[/code].
This could be used to convert [method get_bone_pose] for use with the [method set_bone_global_pose_override] function.
</description>
</method>
<method name="localize_rests">
<return type="void" />
<description>
Expand Down Expand Up @@ -158,13 +248,30 @@
Binds the given Skin to the Skeleton.
</description>
</method>
<method name="remove_bone_child">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="child_bone_idx" type="int" />
<description>
Removes the passed in child bone index, [code]child_bone_idx[/code], from the passed-in bone, [code]bone_idx[/code], if it exists.
[b]Note:[/b] This does not remove the child bone, but instead it removes the connection it has to the parent bone.
</description>
</method>
<method name="set_bone_children">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="bone_children" type="PackedInt32Array" />
<description>
Sets the children for the passed in bone, [code]bone_idx[/code], to the passed-in array of bone indexes, [code]bone_children[/code].
</description>
</method>
<method name="set_bone_custom_pose">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="custom_pose" type="Transform3D" />
<description>
Sets the custom pose transform, [code]custom_pose[/code], for the bone at [code]bone_idx[/code]. This pose is an addition to the bone rest pose.
[b]Note[/b]: The pose transform needs to be in bone space. Use [method world_transform_to_bone_transform] to convert a world transform, like one you can get from a [Node3D], to bone space.
[b]Note:[/b] The pose transform needs to be in bone space. Use [method world_transform_to_global_pose] to convert a world transform, like one you can get from a [Node3D], to bone space.
</description>
</method>
<method name="set_bone_disable_rest">
Expand All @@ -183,8 +290,20 @@
<argument index="3" name="persistent" type="bool" default="false" />
<description>
Sets the global pose transform, [code]pose[/code], for the bone at [code]bone_idx[/code].
[code]amount[/code] is the interpolation strength that will be used when applying the pose, and [code]persistent[/code] determines if the applied pose will remain.
[b]Note[/b]: The pose transform needs to be in bone space. Use [method world_transform_to_bone_transform] to convert a world transform, like one you can get from a [Node3D], to bone space.
[code]amount[/code] is the interpolation strengh that will be used when applying the pose, and [code]persistent[/code] determines if the applied pose will remain.
[b]Note:[/b] The pose transform needs to be a global pose! Use [method world_transform_to_global_pose] to convert a world transform, like one you can get from a [Node3D], to a global pose.
</description>
</method>
<method name="set_bone_local_pose_override">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="pose" type="Transform3D" />
<argument index="2" name="amount" type="float" />
<argument index="3" name="persistent" type="bool" default="false" />
<description>
Sets the local pose transform, [code]pose[/code], for the bone at [code]bone_idx[/code].
[code]amount[/code] is the interpolation strengh that will be used when applying the pose, and [code]persistent[/code] determines if the applied pose will remain.
[b]Note:[/b] The pose transform needs to be a local pose! Use [method global_pose_to_local_pose] to convert a global pose to a local pose.
</description>
</method>
<method name="set_bone_name">
Expand All @@ -209,7 +328,7 @@
<argument index="1" name="pose" type="Transform3D" />
<description>
Sets the pose transform for bone [code]bone_idx[/code].
[b]Note[/b]: The pose transform needs to be in bone space. Use [method world_transform_to_bone_transform] to convert a world transform, like one you can get from a [Node3D], to bone space.
[b]Note:[/b] The pose transform needs to be in bone space. Use [method world_transform_to_global_pose] to convert a world transform, like one you can get from a [Node3D], to bone space.
</description>
</method>
<method name="set_bone_rest">
Expand All @@ -220,19 +339,26 @@
Sets the rest transform for bone [code]bone_idx[/code].
</description>
</method>
<method name="set_modification_stack">
<return type="void" />
<argument index="0" name="modification_stack" type="SkeletonModificationStack3D" />
<description>
Sets the modification stack for this skeleton to the passed-in modification stack, [code]modification_stack[/code].
</description>
</method>
<method name="unparent_bone_and_rest">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<description>
Unparents the bone at [code]bone_idx[/code] and sets its rest position to that of its parent prior to being reset.
</description>
</method>
<method name="world_transform_to_bone_transform">
<method name="world_transform_to_global_pose">
<return type="Transform3D" />
<argument index="0" name="world_transform" type="Transform3D" />
<description>
Takes the given world transform, relative to the [Skeleton3D], and converts it to a bone pose/transform.
This is useful for using setting bone poses using transforms from [Node3D]-based nodes.
Takes the passed-in global transform and converts it to a global pose.
This can be used to easily convert a global transform from [member Node3D.global_transform] to a global pose usable with [method set_bone_global_pose_override], for example.
</description>
</method>
</methods>
Expand All @@ -241,6 +367,12 @@
</member>
</members>
<signals>
<signal name="bone_pose_changed">
<argument index="0" name="bone_idx" type="int" />
<description>
This signal is emitted when one of the bones in the Skeleton3D node have changed their pose. This is used to inform nodes that rely on bone positions that one of the bones in the Skeleton3D have changed their transform/pose.
</description>
</signal>
<signal name="pose_updated">
<description>
</description>
Expand Down
Loading

0 comments on commit fff9a45

Please sign in to comment.