Skip to content

Commit

Permalink
Nevermind, seems it may be a false alarm. Everything mostly works oka…
Browse files Browse the repository at this point in the history
…y, though it may not be techincally accurate. I still need to fix the root bone not being active though.

Took longer than I'd like to try and find solutions though. Rotations with Quaternions are hard.
  • Loading branch information
TwistedTwigleg committed Jun 23, 2020
1 parent acf0e82 commit b120060
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 2 additions & 3 deletions core/math/quat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,8 @@ Quat Quat::get_swing_quat(const Vector3 p_axis) const {
return Quat(x, y, z, w) * twist.inverse();
}

// Takes two vectors and creates a Quat that starts with the rotation of p_from
// and rotates to p_to. Similar to the look_at function, but you can define/override
// the starting rotation.
// Takes two vectors and creates a Quat that starts with the rotation of p_from and rotates to p_to.
// Similar to the look_at function, but you can define/override the starting rotation.
// Adopted from: https://bitbucket.org/sinbad/ogre/src/9db75e3ba05c/OgreMain/include/OgreVector3.h#cl-651
void Quat::rotate_from_vector_to_vector(const Vector3 p_from, const Vector3 p_to) {
Vector3 v0 = p_from.normalized();
Expand Down
10 changes: 7 additions & 3 deletions scene/resources/skeleton_modification_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ void SkeletonModification3D_LookAt::execute(float delta) {
new_bone_trans.basis.rotate_local(Vector3(0, 0, 1), -Math::deg2rad(additional_rotation.z));

// Rotate to look at the target.
Quat new_rot = new_bone_trans.basis.get_rotation_euler();
Quat new_rot = Quat();

new_rot.rotate_from_vector_to_vector(
stack->skeleton->get_bone_axis_forward(bone_idx),
skeleton->global_pose_to_local_pose(bone_idx, skeleton->world_transform_to_global_pose(n->get_global_transform())).origin);
Expand Down Expand Up @@ -633,9 +634,10 @@ void SkeletonModification3D_CCDIK::_execute_ccdik_joint(int p_joint_idx, Node3D
stack->skeleton->global_pose_to_local_pose(ccdik_data.bone_idx, stack->skeleton->world_transform_to_global_pose(tip->get_global_transform())).origin * ccdik_data.ccdik_axis_vector_inverse,
stack->skeleton->global_pose_to_local_pose(ccdik_data.bone_idx, stack->skeleton->world_transform_to_global_pose(target->get_global_transform())).origin * ccdik_data.ccdik_axis_vector_inverse);
} else if (ccdik_data.rotate_mode == ROTATE_MODE_FROM_JOINT) {
Vector3 target_position = stack->skeleton->global_pose_to_local_pose(ccdik_data.bone_idx, stack->skeleton->world_transform_to_global_pose(target->get_global_transform())).origin;
ccdik_rotation.rotate_from_vector_to_vector(
stack->skeleton->get_bone_axis_forward(ccdik_data.bone_idx) * ccdik_data.ccdik_axis_vector_inverse,
stack->skeleton->global_pose_to_local_pose(ccdik_data.bone_idx, stack->skeleton->world_transform_to_global_pose(target->get_global_transform())).origin * ccdik_data.ccdik_axis_vector_inverse);
(bone_trans.origin.direction_to(target_position)) * ccdik_data.ccdik_axis_vector_inverse);
} else if (ccdik_data.rotate_mode == ROTATE_MODE_FREE) {
// Free mode: allow rotation on any axis.
ccdik_rotation.rotate_from_vector_to_vector(
Expand Down Expand Up @@ -1550,7 +1552,9 @@ void SkeletonModification3D_Jiggle::_execute_jiggle_joint(int p_joint_idx, Node3
jiggle_data_chain.write[p_joint_idx].last_position = new_bone_trans.origin;

Quat rotation_quat = Quat();
rotation_quat.rotate_from_vector_to_vector(new_bone_trans.basis[1].normalized(), new_bone_trans.origin.direction_to(jiggle_data_chain[p_joint_idx].dynamic_position));
rotation_quat.rotate_from_vector_to_vector(
stack->skeleton->get_bone_axis_forward(jiggle_data_chain[p_joint_idx].bone_idx),
new_bone_trans.origin.direction_to(jiggle_data_chain[p_joint_idx].dynamic_position));
new_bone_trans.basis = Basis(rotation_quat);

new_bone_trans = stack->skeleton->global_pose_to_local_pose(jiggle_data_chain[p_joint_idx].bone_idx, new_bone_trans);
Expand Down

0 comments on commit b120060

Please sign in to comment.