如何解决解开如何将围绕矢量的旋转添加到对象的旋转
您好,我正在为双链/臂编写反向运动学脚本。它工作得很好。但我唯一缺少的是能够围绕底座和目标(蓝色)的矢量旋转整个手臂。这是我创建的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IKSolver : MonoBehavIoUr
{
[Header("Joints")]
public Transform Link0;
public Transform Link1;
public Transform Endpoint;
public float angle;
[Header("Target")]
public Transform Target;
private float length0;
private float length1;
private void Start()
{
length0 = Vector2.distance(Link0.position,Link1.position);
length1 = Vector2.distance(Link1.position,Endpoint.position);
}
void Update()
{
float jointAngle0;
float jointAngle1;
float distance = Vector3.distance(Link0.position,Target.position);
Vector3 diffVector = Target.position - Link0.position;
float acos = Mathf.Acos((Target.position.y - Link0.position.y) / distance) * Mathf.Rad2Deg;
//calculate how much the arm should rotate around the Y-axis to point towards the EndPoint
Vector3 root = Link0.rotation.eulerAngles;
root.y = -Mathf.atan2(diffVector.z,diffVector.x) * Mathf.Rad2Deg;
Link0.transform.eulerAngles = root;
//calculate the inverse kinmatics for both links in arm
if (length0 + length1 < distance)
{
jointAngle0 = acos;
jointAngle1 = 0f;
}
else
{
float cosAngle0 = ((distance * distance) + (length0 * length0) - (length1 * length1)) / (2 * distance * length0);
float angle0 = Mathf.Acos(cosAngle0) * Mathf.Rad2Deg;
float cosAngle1 = ((length1 * length1) + (length0 * length0) - (distance * distance)) / (2 * length1 * length0);
float angle1 = Mathf.Acos(cosAngle1) * Mathf.Rad2Deg;
jointAngle0 = acos - angle0;
jointAngle1 = 180f - angle1;
}
//apply the calculated angles to the local transforms of the links
Vector3 Euler0 = Link0.transform.localEulerAngles;
Euler0.z = -jointAngle0;
Link0.transform.localEulerAngles = Euler0;
Vector3 Euler1 = Link1.transform.localEulerAngles;
Euler1.z = -jointAngle1;
Link1.transform.localEulerAngles = Euler1;
//getting angle that the arm should rotate around the blue vector
Quaternion angleAxis = Quaternion.AngleAxis(angle,diffVector);
//draw ray between Link0 and endPoint;
Debug.DrawRay(Link0.position,angleAxis * diffVector,Color.blue);
//draw ray of the rotation i want to achieve
Debug.DrawRay(Link0.position,(angleAxis * Quaternion.Euler(root)) * Vector3.up,Color.magenta);
}
}
我在 Alan Zucconi's blog post 的帮助下创建了这个脚本。 正如您在视频中看到的,我已经可以以我想要的角度(洋红色)绘制光线。我想将整个手臂旋转到这个角度。但我似乎不知道该怎么做。
你可以看到我确实得到了“Quaternion.AngleAxis”的ange。我确实将它与我之前计算的根一起应用于洋红色射线。
//getting angle that the arm should rotate around the blue vector
Quaternion angleAxis = Quaternion.AngleAxis(angle,diffVector);
但是每当我尝试将此旋转 + 根旋转应用于第一个链接时,它似乎不像洋红色光线那样工作。我似乎无法弄清楚如何解决它。
所以,就像问题的标题一样,我需要弄清楚如何将围绕矢量 diffVector 的旋转添加到已经存在的旋转 root 而不干扰本地eulerRotation Euler0.
感谢任何帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。