Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New and improved IK in Skeleton3D #39353

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3587bc0
Changes:
TwistedTwigleg Aug 3, 2020
92e13b6
Changes:
TwistedTwigleg Aug 3, 2020
e51a467
Changes:
TwistedTwigleg Aug 3, 2020
841ed99
Changes:
TwistedTwigleg Aug 3, 2020
1274eed
Changes:
TwistedTwigleg Aug 3, 2020
fc5c3cb
Changes:
TwistedTwigleg Aug 3, 2020
b5f8156
Changes:
TwistedTwigleg Aug 3, 2020
d614c63
Fixed style and formatting issues with the SkeletonModification code …
TwistedTwigleg Aug 4, 2020
0727760
Changes:
TwistedTwigleg Aug 4, 2020
b194f81
Changes:
TwistedTwigleg Aug 5, 2020
02e7ade
Changes:
TwistedTwigleg Aug 5, 2020
fe8244c
Changes:
TwistedTwigleg Aug 6, 2020
cd6bae2
Changes:
TwistedTwigleg Aug 6, 2020
5e3fad9
Changes:
TwistedTwigleg Aug 7, 2020
bd9a4ef
Changes:
TwistedTwigleg Aug 10, 2020
ad32951
Changes:
TwistedTwigleg Aug 11, 2020
7dfc085
Changes:
TwistedTwigleg Aug 12, 2020
634026d
Changes:
TwistedTwigleg Aug 12, 2020
a3160c1
Changes:
TwistedTwigleg Aug 13, 2020
74524d2
Changes:
TwistedTwigleg Aug 14, 2020
db37c20
Changes:
TwistedTwigleg Aug 17, 2020
5fd0708
Changes:
TwistedTwigleg Aug 18, 2020
57ca076
Changes:
TwistedTwigleg Aug 19, 2020
f011bd3
Changes:
TwistedTwigleg Aug 24, 2020
8ca0e58
Changes:
TwistedTwigleg Aug 31, 2020
ec6f7d9
Changes:
TwistedTwigleg Aug 31, 2020
606c5a6
Changes:
TwistedTwigleg Sep 22, 2020
74f97df
Changes:
TwistedTwigleg Sep 23, 2020
d39bee9
Changes needed to make the GSOC 2020 3D IK PR compile with latest ver…
TwistedTwigleg Jun 18, 2021
e36677e
GSOC 2020 3D IK - broke modifications down into their own files
TwistedTwigleg Jun 18, 2021
c5ca642
GSOC 2020 3D IK Changes:
TwistedTwigleg Jun 26, 2021
259c1e7
GSOC 3D IK changes: Updated the documentation using doctool and updat…
TwistedTwigleg Jun 26, 2021
20ed10b
GSOC 2020 3D IK: Fixing test issues on CI builds
TwistedTwigleg Jul 5, 2021
08bc048
GSOC 2020 IK PR: Rebased to latest version of master and made adjustm…
TwistedTwigleg Aug 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/math/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,19 @@ 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
// TODO: optimize this function using the link above.
Vector3 axis = p_start_direction.cross(p_end_direction).normalized();
if (axis.length_squared() != 0) {
float dot = p_start_direction.dot(p_end_direction);
dot = CLAMP(dot, -1.0, 1.0);
float 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: 143 additions & 15 deletions doc/classes/Skeleton3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
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.
</description>
</method>
<method name="clear_bones">
Expand All @@ -40,13 +39,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" qualifiers="const">
<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 +107,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 +142,47 @@
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" qualifiers="const">
<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].
</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 +192,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 +244,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 +286,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 +324,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 +335,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 +363,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