3d坐标转换 欧拉角(姿态角) unity3d把 x的欧拉角赋给y什么作用

\u5750\u6807\u7cfb\u53d8\u6362\uff08\u6b27\u62c9\u89d2\uff09\u76f8\u5173

\u8bbe\u5750\u6807\u7cfb1\u7684\u539f\u70b9\u5750\u6807\u4e3aXo1,Yo1,Zo1:\u5750\u6807\u7cfb2\u7684\u539f\u70b9\u5750\u6807\u4e3aXo2\uff0cYo2\uff0cZo2, \u90a3\u4e48\u5982\u679c\u5750\u6807\u7cfb1\u5185\u67d0\u4e00\u70b9\u7684\u5750\u6807\u4e3aX1,Y1,Z1\uff0c\u5219\u5728\u5750\u6807\u7cfb2\u5185\u8be5\u70b9\u7684\u5750\u6807\u4e3a\uff1a X2=X1-(Xo2-Xo1); Y2=Y1-(Yo2-Yo1); Z2=Z1-(Zo2-Zo1)

1.\u6b27\u62c9\u89d2Vector3(x,y,z)\u4ee3\u8868\u7684\u662f\u65cb\u8f6c\u7269\u4f53\uff08\u82e5\u662f\u6807\u51c6\u65cb\u8f6c\u90a3\u4e48\u662f\u65cb\u8f6c\u5750\u6807\u8f74x,y,z\uff0c\u8f6c\u6362\u4e3a\u65cb\u8f6c\u7269\u4f53\u5219\u65cb\u8f6c\u89d2\u5ea6\u53d6\u53cd\u987a\u5e8f\u4e0d\u53d8\uff09\uff0c\u4e14\u662f\u5c06\u7269\u4f53\u4ece\u7269\u4f53\u5750\u6807\u7cfb\u65cb\u8f6c\u5230\u60ef\u6027\u5750\u6807\u7cfb\uff08\u4e16\u754c\u5750\u6807\u7cfb\u4e2d\u4e3a\u4e86\u6e32\u67d3\uff09\uff0c\u6545\u65cb\u8f6c\u987a\u5e8f\u4e3a z, y, x\u4e5f\u5c31\u662froll pitch yaw\u3002
2.\u6b27\u62c9\u89d2\u6709\u522b\u540d\u548c\u4e07\u5411\u9501\u95ee\u9898\uff0c\u4e0d\u8981\u968f\u4fbf\u589e\u957f\u6b27\u62c9\u89d2\u7684\u503c\uff0c\u4e5f\u4e0d\u8981\u5355\u72ec\u6539\u53d8\u6b27\u62c9\u89d2\u7684\u4e00\u4e2a\u65cb\u8f6c\u89d2\u5ea6\u503c\uff0c\u800c\u662f\u7528\u4e00\u4e2aVector3\u5168\u90e8\u4e00\u8d77\u6539\u53d8\u3002
3.\u6b27\u62c9\u89d2\u6765\u81ea\u4e8eTransform.eulerAngles, \u6e32\u67d3\u65f6\u5019\u4e5f\u4f1a\u8f6c\u6362\u5230Transform.rotation\u56db\u5143\u6570\u7136\u540e\u5230\u65cb\u8f6c\u77e9\u9635\u6765\u65cb\u8f6c\u7269\u4f53\u3002
4.\u6b27\u62c9\u89d2\u63d2\u503c\u4f7f\u7528 float fAngles = Mathf.MoveTowardsAngle(oldAngles, target, m_rotSpeed * Time.deltaTime); m_transform.eulerAngles = new Vector3(0, fAngles, 0);
\u6d4b\u8bd5\u5b9e\u4f8b\uff1a
// align Camera
float fRoll = 0.0f; // \u4e0d\u80fd\u7ed5Z\u8f74\u6eda\u52a8
// \u6b27\u62c9\u89d2\u4e2d\u7684\u7ed5X\u8f74\u65cb\u8f6c\uff0c\u5bf9\u5e94Mouse Y\u79fb\u52a8\uff08\u4e0a\u4e0b\uff09\uff0c
float rPitch = Input.GetAxis("Mouse Y");
// \u6b27\u62c9\u89d2\u4e2d\u7684\u7ed5Y\u8f74\u65cb\u8f6c,\u5bf9\u5e94Mouse X\u79fb\u52a8\uff08\u5de6\u53f3\uff09
float rYaw = Input.GetAxis("Mouse X");

// \u9f20\u6807\u7684\u4e0a\u4e0b\u5de6\u53f3\u79fb\u52a8,\u8f6c\u6362\u4e3a\u4e3b\u89d2\u7684\u65cb\u8f6c\u6b27\u62c9\u89d2
// eulerAngles\u662f(yaw, pitch,roll)
// rPitch\u53d6\u8d1f\u6570\uff0c\u662f\u56e0\u4e3aMouse Y\u5411\u4e0a\u79fb\u52a8\u5e0c\u671b\u662f\u5f80\u524d\u770b\u7ed5X\u8f74\u65cb\u8f6c\u53cd\u5411\uff0c\u50cfMouse Y\u5411\u4e0b\u662f\u589e\u5927\u7ed5Y\u8f74\u65cb\u8f6c\u6b63\u5411
Vector3 vecMouse = new Vector3(-rPitch, rYaw, fRoll);
m_camRotateAngles += vecMouse; // m_camRotateAngles\u662f\u6444\u50cf\u673a\u65cb\u8f6c\u6b27\u62c9\u89d2

// \u6574\u4e2a\u6444\u50cf\u673a\u65cb\u8f6c\uff0c\u4e3b\u89d2\u7684\u65cb\u8f6c\u5f71\u54cd\u5230\u6444\u50cf\u673a\u65cb\u8f6c(\u5305\u62ec\u4e0a\u4e0b\u5de6\u53f3\uff09\uff0c\u6b27\u62c9\u89d2\u9700\u8981\u4e00\u6b21\u8d4b\u503c
m_camTransform.eulerAngles = m_camRotateAngles;

//// \u539f\u6765\u7684\u8bbe\u8ba1\uff0c\u4e5f\u662f\u6b63\u786e\u7684
//float rh = Input.GetAxis("Mouse X");
//float rv = Input.GetAxis("Mouse Y");
//m_camRotateAngles.x -= rv;
//m_camRotateAngles.y += rh;
//m_camTransform.eulerAngles = m_camRotateAngles;

// \u4e3b\u89d2\u65cb\u8f6c\uff0cPlayer\u4e0d\u4f1a\u7ed5Z\u8f74\u65cb\u8f6c\uff0c\u4e5f\u4e0d\u4f1a\u7ed5x\u8f74\u65cb\u8f6c\uff0c\u53ea\u662f\u4f1a\u7ed5Y\u8f74\u505a\u5de6\u53f3\u65cb\u8f6c
Vector3 transformRot = m_camTransform.eulerAngles;
transformRot.x = transformRot.z = 0;
m_transform.eulerAngles = transformRot;
\u4e0b\u9762\u662f\u5b98\u65b9\u8bf4\u660e\u6587\u6863\uff1ahttp://docs.Unity3D.com/ScriptReference/Transform-eulerAngles.html
Transform.eulerAngles
Switch to Manual
public Vector3 eulerAngles;
Description
The rotation as Euler angles in degrees.
The x, y, and z angles represent a rotation z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).

Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees.Use Transform.Rotate instead.
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
public float yRotation = 5.0F;
void Update() {
yRotation += Input.GetAxis("Horizontal");
transform.eulerAngles = new Vector3(10, yRotation, 0);
}
void Example() {
print(transform.eulerAngles.x);
print(transform.eulerAngles.y);
print(transform.eulerAngles.z);
}
}
Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.When setting them to a new value set them all at once as shown above.Unity will convert the angles to and from the rotation stored in Transform.rotation.

Mathf.MoveTowardsAngle
public static float MoveTowardsAngle(float current,float target,float maxDelta);
Parameters
Description
Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.
Variables current and target are assumed to be in degrees.For optimization reasons, negative values of maxDelta are not supported and may cause oscillation. To push current away from a target angle, add 180 to that angle instead.
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
public float target = 270.0F;
public float speed = 45.0F;
void Update() {
float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, target, speed * Time.deltaTime);
transform.eulerAngles = new Vector3(0, angle, 0);
}
}

把欧拉角先转换成旋转矩阵(或者四元素)

转换公式网上资料很多,也可以参考一下这个网页链接

然后,把欧拉角A转换成旋转矩阵M,把欧拉角B转换成旋转矩阵N

再求矩阵 L = N·inv(M),inv( )是求逆矩阵

最后参考网页链接把 L转换成欧拉角,则这个欧拉角就是从A到B的旋转角



扩展阅读:欧拉角章动角 ... 三维坐标系欧拉角变换 ... 欧拉角是哪三个 ... 四元数 姿态角 ... 欧拉角内旋和外旋的区别 ... 欧拉角坐标系转换动图 ... 欧拉角变换后坐标计算 ... 欧拉角与姿态角的关系 ... 欧拉角变换公式 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网