当前位置:网站首页>Unity -- Euler angle, quaternion
Unity -- Euler angle, quaternion
2022-07-26 00:06:00 【Small digital media members】
Today I continue to learn Euler angles and quaternions , Starting tomorrow “ Synthetic watermelon ”!
Catalog
Vector operation ( To continue )
Point multiplication , also called “ Inner product ” or “ Dot product ”
Relationship between result and angle
1. The expression of orientation is not unique
Vector operation ( To continue )
Point multiplication , also called “ Inner product ” or “ Dot product ”
The formula :
Sum of products of components [x1,y1,z1]·[x2,y2,z2]=x1x2+y1y2+z1z2
Geometric meaning :
Multiply the unit vectors of the two vectors by the cosine of the angle between them
a·b=|a|·|b|cos<a,b>
API:float dot=Vector3.Dot(va,vb);
Calculate the included angle by dot multiplication :
Do two vectors narmalized Handle , The result of two normalized vectors is cos<a,b>, Re arccosine Mathf.Acos You get the angle
The angle obtained by dot multiplication is the small angle
application :
For normalized vectors , The result of dot multiplication is equal to the cosine of the angle between two vectors
Calculate the angle between two vectors
public Transform t1, t2;
float dot = Vector3.Dot(t1.position.normalized, t2.position.normalized);
float angle = Mathf.Acos(dot) * Mathf.Rad2Deg;
Relationship between result and angle
For normalized vectors , In exactly the same direction , The dot multiplication result is 1, The opposite , The dot multiplication result is -1, Perpendicular to each other is 0

If the angle between two vectors is greater than 60 degree , be
if(angle>60){} or if(dot<0.5f){}// advantage : Save CPU Then calculate the angle
private void Update()
{
Debug.DrawLine(Vector3.zero, t1.position,Color.red);
Debug.DrawLine(Vector3.zero, t2.position,Color.red);
float dot = Vector3.Dot(t1.position.normalized, t2.position.normalized);
float angle = Mathf.Acos(dot) * Mathf.Rad2Deg;
}
Cross riding
also called “ Cross product ” or “ Exoproduct ”
The formula
[x1,y1,z1]*[x2,y2,y3]=[y1*z2-z1*y2,z1*x2-x1*z2,x1*y2-y1*x2]
Geometric meaning
The result is the vertical vector of the two faces , The module length is the product of two vector module lengths and then multiplied by the sine of the included angle
API:Vector3 vector=Vector3.Cross(a,b);
application :
· Create a vector perpendicular to the plane
· Judge the relative position of two vectors , That is, clockwise and counterclockwise


private void Update()
{
Debug.DrawLine(Vector3.zero, t1.position, Color.red);
Debug.DrawLine(Vector3.zero, t2.position, Color.red);
dot = Vector3.Dot(t1.position.normalized, t2.position.normalized);
angle = Mathf.Acos(dot) * Mathf.Rad2Deg;
Vector3 cross = Vector3.Cross(t1.position, t2.position);
if (cross.y < 0)
angle = 360 - angle;
Debug.DrawLine(Vector3.zero, cross, Color.green);
}

The relationship between the module length and angle of the vector obtained by cross multiplication :
0~90 Degree angle
Vector3 cross=Vector3.Cross(a.normalized,b.normalized);
float angle=Mathf.Asin(cross.magnitude)*Mathf.Rad2Deg;
( Rarely used )

Euler Angle
What is the angle of Euler
Use three angles to save the orientation
x And z Rotate along its own coordinate system ,y Rotate along the world coordinate system
API:Vector3 eulerAngle=this.transform.eulerAngles;
advantage :
Use only three numbers to express orientation , Small footprint
The unit of rotation along the coordinate axis is angle , In line with people's way of thinking
Any three numbers are legal , There is no illegal Euler angle
shortcoming :
1. The expression of orientation is not unique
There are multiple Euler angle descriptions for a bearing , Therefore, it is impossible to judge whether the angular displacements represented by multiple Euler angles are the same
example : angle 0,5,0 And angle 0,365,0
angle 250,0,0 And angle 290,180,180
In order to ensure that there is only a unique representation of any direction ,unity Limits the range of angles , Namely along x Axis rotation is limited to -90 To 90 Between , Along the y And x Axis rotation is limited to 0 To 360 Between ( stay unity There are no restrictions in the compiler , Limit in code )
Vector3 euler=this.transform.eulerAngles;
Be careful : Euler angle has no direction 、 The concept of size
Because three-dimensional vectors , contain x,y,z, So in Unity The data type of Central European pull angle is Vector3
Euler angle x,y,z Represents the rotation angle on each axis
Vector3 por=this.transform.position;
Location , There is a direction ( Point to the current position from the world origin ), There are sizes ( Distance from the current position to the world origin )
Vectorial x,y,z, Indicates the directional displacement on each axis
It can be used to detect Euler angle
public Vector3 euler;
// It can be used to detect Euler angle
public void OnGUI()
{
euler = this.transform.eulerAngles;
if (GUILayout.RepeatButton(" Along the X Shaft rotation "))
this.transform.eulerAngles += new Vector3(1, 0, 0);
if (GUILayout.RepeatButton(" Along the y Shaft rotation "))
this.transform.eulerAngles += Vector3.up;
if (GUILayout.RepeatButton(" Along the z Shaft rotation "))
this.transform.eulerAngles += new Vector3(0, 0,1);
}2. Universal joint deadlocks
Objects along x Shaft rotation +(-)90 degree , Self coordinate system z Axis and world coordinate system y The axes will coincide , Now go along y or z When axis rotates , Will lose a degree of freedom .
In case of cardan deadlock , Specified edge z The axis completes all rotation around the numerical axis , Is this time y The axis rotates to 0

Four yuan number
What is quaternion
Quaternion stay 3D In graphics stands for rotation , By a three-dimensional vector (x,y,z) And a scalar (w) form
The rotation axis is V, The rotation radian is θ, If you use quaternions to represent , Then the four components are :
x=sin(θ/2)*V.x y=sin(θ/2)*V.y
z=sin(θ/2)*V.z w=cos(θ/2)
x,y,z,w The range of phi is zero -1 To 1
API:Quaternion qt=this.transform.rotation;
public void OnGUI()
{
euler = this.transform.eulerAngles;
if (GUILayout.Button(" Set the rotation angle of the object "))
{
// Rotation axis
Vector3 axis = t1.position - t2.position;
// The arc of rotation
float rad = 50 * Mathf.Deg2Rad;
Quaternion qt = new Quaternion();
qt.x = Mathf.Sin(rad / 2) * axis.x;
qt.y = Mathf.Sin(rad / 2) * axis.y;
qt.z = Mathf.Sin(rad / 2) * axis.z;
qt.w = Mathf.Cos(rad / 2);
this.transform.rotation = qt;
}
}advantage :
Avoid U-joint deadlock
this.transform.rotation( Four yuan number )*=Quaternion.Euler(0,1,0);
Can make an object move along its own coordinates Y Shaft rotation
public void OnGUI()
{
euler = this.transform.eulerAngles;
if (GUILayout.RepeatButton(" Along the X Shaft rotation "))
this.transform.rotation *= Quaternion.Euler(1, 0, 0);
if (GUILayout.RepeatButton(" Along the y Shaft rotation "))
this.transform.rotation *= Quaternion.Euler(0, 1, 0);
if (GUILayout.RepeatButton(" Along the z Shaft rotation "))
this.transform.rotation *= Quaternion.Euler(0, 0, 1);
}this.transform.Rotate(Vector3 eulerAngles)
Internally, quaternion multiplication is used to achieve
shortcoming
· Difficult to use , It is not recommended to modify a value separately

· There is an illegal quaternion
Quaternion operations
· Multiply by vector
Quaternion left multiplication vector , Indicates that the vector is rotated according to the angle represented by quaternion
example :Vector3 point =new Vector3(0,0,10);// Walk forward 10 rice
Vector3 newpoint=Quaternion.Euler(0,30,0)( Along the y Shaft rotation 30 degree )*point;
· Multiply with quaternions
Multiply two quaternions to combine the rotation effect
example :Quaternion rotation01=Quaternion.Euler(0,30,0)*Quaternion.Euler(0,20,0);
Quaternion rotation02=Quaternion.Euler(0,50,0);
rotation01 And rotation02 identical

边栏推荐
- Find the intermediate node of the linked list
- Binary tree -- 111. Minimum depth of binary tree
- 07_ue4进阶_发射火球扣mp值和攻击扣血机制
- 二叉树——110. 平衡二叉树
- 栈与队列——239. 滑动窗口最大值
- C language (high level) program environment and preprocessing
- Js理解之路:Object.call与Object.create()实现继承的原理
- Kubernetes网络插件详解 - Calico篇 - 概述
- 没错,请求DNS服务器还可以使用UDP协议
- 二叉树——654. 最大二叉树
猜你喜欢

The mobile version of Duoyu security browser will add new functions to make users browse more personalized

Android solves the risk of database injection vulnerability

Observer model of behavioral model

Binary tree - 404. Sum of left leaves

“动物币”凶猛,陷阱还是机遇?2021-05-12

07_ue4进阶_发射火球扣mp值和攻击扣血机制

Compile live555 with vs2019 in win10

Part 66: monocular 3D reconstruction point cloud

二叉树——222. 完全二叉树的节点个数

Binary tree - 530. Minimum absolute difference of binary search tree
随机推荐
NVIDIA cuDNN学习
Demo of pointer function
The items of listview will be displayed completely after expansion
如何用yolov5 做个闯红灯监控的智能交通系统(1)
The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
SQLZOO——Nobel Quiz
Pyqt5 rapid development and actual combat.pdf sharing
Leetcode169 detailed explanation of most elements
栈与队列——239. 滑动窗口最大值
Observer model of behavioral model
The mobile version of Duoyu security browser will add new functions to make users browse more personalized
Old laptop becomes server (laptop + intranet penetration)
Ten threats to open API ecosystem
Leetcode shahutong series -- 63. Different paths II
Shardingsphere data slicing
[learning notes] solid works operation record
STM32 timer
C language actual combat guessing game
栈与队列——347. 前 K 个高频元素
Stack and queue - 347. Top k high frequency elements