当前位置:网站首页>Quaternion and its simple application in unity
Quaternion and its simple application in unity
2022-07-29 08:47:00 【qq_ forty-two million nine hundred and eighty-seven thousand ni】
Pre knowledge , Be able to understand the rotation process of Euler angle and its universal joint deadlock .
One 、 Basic introduction of quaternion
1. Characteristics and disadvantages of quaternion
1) advantage :
Universal joint locking can be avoided ;
Just one 4 The quaternion of dimension can perform the rotation around any vector passing through the origin , Convenient and quick , In some implementations, it is more efficient than rotation matrix ;
Can provide smooth interpolation ;
2) shortcoming :
It's a little more complicated than Euler's rotation , Because there is one more dimension ;
More difficult to understand , Is not intuitive ;
Next, it will be more targeted to understand quaternions with their advantages and disadvantages .
2. The basic operation law of quaternion
1) There are too many things in it , Post some good posts. Let's go and have a look .
Four yuan number —— Basic concepts - You know
https://krasjet.github.io/quaternion/quaternion.pdf
Four yuan number (Quaternions) - You know
But this is the most recommended , It looks more comfortable : Graphics notes - You know
2) Roughly summarize the more important concepts :
Imaginary part and real part of quaternion 、 The unit is 4 yuan 、 Pure quaternion 、 Dot product of quaternions 、 The product of 、 Cross product 、 Conjugate and inverse of quaternion 、 Quaternions represent rotation 、 Quaternion interpolation . I feel that understanding these concepts should be regarded as the entry quaternion .
3. Can roughly understand the geometric meaning of quaternion operation
You can refer to this :https://jingyan.baidu.com/article/4ae03de3dbbac83eff9e6b00.html
The argument that quaternions can represent states and actions is very good , It is helpful for understanding and memorizing quaternions .
Two 、 The mutual transformation of quaternion and Euler angle
This can be regarded as an exercise of theoretical understanding of quaternions , Derivation is too cumbersome , Just post reference links directly , These four links give a good derivation process , What we need to pay attention to is unity Rotation order of Central European pull angle .
Quaternion and Euler angle (RPY horn ) Mutual conversion of - XXX Lost contact - Blog Garden
Next, I will directly post the test code of mutual transformation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuaternionRotate : MonoBehaviour
{
void Start()
{
Quaternion q=Quaternion.Euler(60,60,60);
Debug.Log(" Original Euler angle " + new Vector3(60, 60, 60));
Debug.Log(" Original quaternion "+q);
Debug.Log(" Converted Euler angle " + QuaternionToEuler(q.x, q.y, q.z, q.w));
Debug.Log(" Converted quaternion " + EulerToQuaternion(60,60,60));
}
public Quaternion EulerToQuaternion(float xx, float yy, float zz)
{
float X = xx / 180 * Mathf.PI;
float Y = yy / 180 * Mathf.PI;
float Z = zz / 180 * Mathf.PI;
float x = Mathf.Cos(Y / 2) * Mathf.Sin(X / 2) * Mathf.Cos(Z / 2) + Mathf.Sin(Y / 2) * Mathf.Cos(X / 2) * Mathf.Sin(Z / 2);
float y = Mathf.Sin(Y / 2) * Mathf.Cos(X / 2) * Mathf.Cos(Z / 2) - Mathf.Cos(Y / 2) * Mathf.Sin(X / 2) * Mathf.Sin(Z / 2);
float z = Mathf.Cos(Y / 2) * Mathf.Cos(X / 2) * Mathf.Sin(Z / 2) - Mathf.Sin(Y / 2) * Mathf.Sin(X / 2) * Mathf.Cos(Z / 2);
float w = Mathf.Cos(Y / 2) * Mathf.Cos(X / 2) * Mathf.Cos(Z / 2) + Mathf.Sin(Y / 2) * Mathf.Sin(X / 2) * Mathf.Sin(Z / 2);
Quaternion quataion = new Quaternion(x, y, z, w);
return quataion;
}
public Vector3 QuaternionToEuler(float xx, float yy, float zz, float ww)
{
float X = Mathf.Asin(2 * (ww * xx - yy * zz));
float Y = Mathf.Atan2(2 * (ww * yy + xx * zz), 1 - 2 * (xx * xx + yy * yy));
float Z = Mathf.Atan2(2 * (ww * zz + yy * xx), 1 - 2 * (xx * xx + zz * zz));
Vector3 euler = new Vector3(X * 180f / Mathf.PI, Y * 180f / Mathf.PI, Z * 180f / Mathf.PI);
return euler;
}
}

above EulerToQuaternion Function corresponds to unity Medium Quaternion.Euler function ,QuaternionToEuler Function corresponds to unity Medium q.eulerAngles function .
3、 ... and 、 With Unity For example, the use of quaternions
This part mainly refers to :
Quaternion is mainly used for rotation and rotation interpolation , With the following code, you can make an object rotate with another object ( Rotation following ) The effect of .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Saber : MonoBehaviour {
public Transform targetPos;// Rotate center object
public float speed = 200f;// Rotation speed
public float distance;// Radius of rotation
Vector3 dir;
void Start()
{
dir = transform.position - targetPos.position;
}
void Update() {
// Update the position of the following object
transform.position = targetPos.position + dir.normalized * distance;
// Rotate around the character
transform.RotateAround(targetPos.position, Vector3.up, speed * Time.deltaTime);
// Update direction vector
dir = transform.position - targetPos.position;
}
}
Reference resources RotateAround Source code of function , You can see the use of quaternions .
public void RotateAround(Vector3 point, Vector3 axis, float angle)
{
Vector3 vector = position;
Quaternion quaternion = Quaternion.AngleAxis(angle, axis);
Vector3 vector2 = vector - point;
vector2 = quaternion * vector2;
vector = (position = point + vector2);
RotateAroundInternal(axis, angle * ((float)Math.PI / 180f));
}边栏推荐
- Virtual augmentation and reality Part 2 (I'm a Firebird)
- Transaction management in SQL Server
- Simple operation of SQL server data table
- leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
- Osgsimplegl3 combined with renderdoc tool
- Day4: SQL server is easy to use
- Day15: the file contains the vulnerability range manual (self use file include range)
- SAP sm30 brings out description or custom logical relationship
- Markdown concise grammar manual
- Mathematical modeling - Differential Equations
猜你喜欢

Centos7/8 command line installation Oracle11g

centos7/8命令行安装Oracle11g

Day15: the file contains the vulnerability range manual (self use file include range)
![A little knowledge [synchronized]](/img/4d/4a8beee749328b5867b59740fd7e78.png)
A little knowledge [synchronized]

SAP ooalv-sd module actual development case (add, delete, modify and check)

Clickhouse learning (II) Clickhouse stand-alone installation

AI application lesson 1: C language Alipay face brushing login

Intel将逐步结束Optane存储业务 未来不再开发新产品

C language macro define command exercise

English high frequency suffix
随机推荐
Intel将逐步结束Optane存储业务 未来不再开发新产品
Chrony time synchronization
Analysis of zorder sampling partition process in Hudi - "deepnova developer community"
Leetcode Hot 100 (brush question 9) (301/45/517/407/offer62/mst08.14/)
(视频+图文)机器学习入门系列-第3章 逻辑回归
Thrift installation manual
2022 electrician (elementary) test question simulation test platform operation
SAP sm30 brings out description or custom logical relationship
BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
[opencv] - Operator (Sobel, canny, Laplacian) learning
Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql
2022 Shandong Province safety officer C certificate work certificate question bank and answers
access数据库可以被远程访问吗
Personal study notes
Requests library simple method usage notes
(Video + graphic) introduction to machine learning series - Chapter 3 logical regression
OSG advanced sequence
Simple operation of SQL server data table
2022 P cylinder filling test simulation 100 questions simulation test platform operation
Ar virtual augmentation and reality