当前位置:网站首页>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

边栏推荐
- 1223. Dice simulation range DP
- MySQL的DDL、DML和DQL的基本语法
- Leetcode107-二叉树的层序遍历II详解
- Part 67: conversion between keypoint and point2f in opencv
- How to use yolov5 as an intelligent transportation system for red light running monitoring (1)
- YoloV4-tiny网络结构
- 07_ue4进阶_发射火球扣mp值和攻击扣血机制
- Exercise (2) create a set to store the elements "1", "$", "2", "$", "3", "$", "4"“
- Cherish time and improve efficiency
- [learning notes] solid works operation record
猜你喜欢

Yolov4 tiny network structure

What is parity? How to use C language?

Binary tree -- 257. All paths of binary tree

行为型模式之观察者模式

Binary tree -- 111. Minimum depth of binary tree

The items of listview will be displayed completely after expansion

Sort fake contacts

Part 66: monocular 3D reconstruction point cloud

TOPSIS and entropy weight method

Solve the problem of rapid index bar extrusion
随机推荐
Seventy second: pedestrian detection
NVIDIA cudnn learning
二叉树——112. 路径总和
Firewall command simple operation
Getaverse,走向Web3的远方桥梁
Leetcode107-二叉树的层序遍历II详解
Practical experience of pair programming
行为型模式之观察者模式
Weight file and pre training file of yolov3
Shardingsphere data slicing
NVIDIA可编程推理加速器TensorRT学习笔记(三)——加速推理
SHIB(柴犬币)一月涨幅数百倍,百倍币需具备哪些核心要素?2021-05-09
YoloV4-tiny网络结构
二叉树——110. 平衡二叉树
二叉树——226. 翻转二叉树
Pyqt5 rapid development and actual combat.pdf sharing
Leetcode200-查找岛屿数量详解
二叉树——104. 二叉树的最大深度
Stack and queue - 150. Inverse Polish expression evaluation
Kubernetes网络插件详解 - Calico篇 - 概述