当前位置:网站首页>[question 018: how does unity understand quaternion.angleaxis?]
[question 018: how does unity understand quaternion.angleaxis?]
2022-07-18 02:03:00 【valaki】
One 、 background
In the game development process , Sometimes an object will rotate at different angles around different axes for many times , So we can calculate a direction , So as to detect whether there is a corresponding object or other objects in the corresponding direction , Therefore, this section is for Quaternion.AngleAxis Make simple records ;
Two 、Quaternion.AngleAxis
For the API To understand :
- Quaternion: Four yuan number , This is in Unity It is basically used to calculate the azimuth 、 Spinning ;
- AngleAxis: This means around an axis , Rotate at the specified angle
The whole is : Specify an axis , Rotate the specified angle around this axis , Get a new rotation result ;
3、 ... and 、 practice
1、 The demo 
You can see two in the above figure Cube, All at the same angle , Rotating around the same axis ; Here is just a simple use of this API Result :
targetA.rotation = Quaternion.AngleAxis(angel, direction);
targetB.rotation = Quaternion.AngleAxis(angel, direction);
Angel: Represents how many degrees of rotation , For example, the value controlled by the slider in the above figure 0-180; Of course 0 There will be no change ;
direction: Represents the axis , That is, about which axis ; The above animation only uses basic
Vector3.up、Vector3.right,Vector3.forward Perform basic demonstrations . But most of the directions in actual operation are not these basic directions , There are more other directions .
2、 The rotation difference between the world axis and the local axis
The demo :
First of all, will CubeA and CubeB Adjust the same rotation angle ; Then adjust the parameters in the right panel , Rotate them ; It can be found that the actual effect of rotation is different 【 According to xyz The direction of the axis to send details found 】; This is because the coordinate axes used when rotating are different ;
CubeA: Use the world coordinate axis to rotate ;
CubeB: It's using CubeB Its coordinate axis rotates ;
targetA.rotation = Quaternion.AngleAxis(angel, direction);
targetB.rotation = Quaternion.AngleAxis(angel, rotationB * direction);
rotationB * direction: Express CubeB The direction of the specified axis of itself ; such as Vector3.right It means the right , But it cannot directly represent Cube The right side of itself , The reason is Cube There is rotation in itself ;
- If CubeB No rotation , And there is no parent node in the scene ; Or all the parent nodes have no rotation ; So now CubeB Right and Vector3.right In the same direction ;
- If CubeB It's spinning , Then we need to use CubeB Multiply the current rotation by Vector3.right, The result really means Cube The right side of the ;
3、 Quaternion Multiply
If the results of the two directions of rotation are multiplied , So what will the result be : Watch the demo 
As can be seen from the animation above :
- Yes CubeA Surround Y Axis rotation assignment angle A, then CubeB Adjust the following angle B1、B2
- When angle A =B1+B2 when , The result of two rotations is the same ; That is, it has the same rotation behavior ;
targetA.rotation = Quaternion.AngleAxis(angel, rotationA * direction);
targetB.rotation = Quaternion.AngleAxis(angelB1, rotationB * direction) *
Quaternion.AngleAxis(angelB2, rotationB * direction);
Two Quaternion.AngleAxis Multiply ; That is to say Quaternion Multiplication actually means The two rotation angles add up ;
Four 、 Source code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuaternionAngleAxis : MonoBehaviour
{
[SerializeField] private Transform targetA;
[SerializeField] private Transform targetB;
[SerializeField] [Range(0, 180)] private int angel;
[SerializeField] [Range(0, 180)] private int angelB1;
[SerializeField] [Range(0, 180)] private int angelB2;
[SerializeField] private bool isReset;
public enum Axis
{
Up,
Forward,
Right,
}
public enum TestModel
{
/// <summary>
/// Normal rotation
/// </summary>
Normal,
/// <summary>
/// The world coordinates rotate with itself
/// </summary>
WLRotation,
/// <summary>
/// Rotate multiply
/// </summary>
Mult,
}
private Quaternion rotationA;
private Quaternion rotationB;
public TestModel model;
public Axis axis = Axis.Right;
private Vector3 direction;
private void OnDrawGizmos()
{
if (isReset)
{
rotationA = targetA.rotation;
rotationB = targetB.rotation;
return;
}
switch (axis)
{
case Axis.Forward:
direction = Vector3.forward;
break;
case Axis.Right:
direction = Vector3.right;
break;
case Axis.Up:
direction = Vector3.up;
break;
}
switch (model)
{
case TestModel.Normal:
NormalAngelAxis();
break;
case TestModel.WLRotation:
WLRotationAngelAxis();
break;
case TestModel.Mult:
MultAngelAxis();
break;
}
}
void NormalAngelAxis()
{
targetA.rotation = Quaternion.AngleAxis(angel, direction);
targetB.rotation = Quaternion.AngleAxis(angel, direction);
}
void WLRotationAngelAxis()
{
targetA.rotation = Quaternion.AngleAxis(angel, direction);
targetB.rotation = Quaternion.AngleAxis(angel, rotationB * direction);
}
void MultAngelAxis()
{
targetA.rotation = Quaternion.AngleAxis(angel, rotationA * direction);
targetB.rotation = Quaternion.AngleAxis(angelB1, rotationB * direction) *
Quaternion.AngleAxis(angelB2, rotationB * direction);
}
}
Conclusion :
I seem to be getting fat recently –【valaki】
边栏推荐
- Use scroll view to scroll the content list vertically
- C language to achieve a simple minesweeping game (two-dimensional array)
- 第十四章 多进程
- Serial communication of raspberry pie
- [C exercise] input the month and year, and calculate the number of days in the month
- 容器介绍及总结
- 第四十二期:所谓的管理团队
- 搜索结果页竞价广告展现升级,对SEO有什么影响?
- Servlet API 代码示例:服务器版表白墙
- Baijiahao ranks well. How can the conversion rate be improved?
猜你喜欢

High weight website has not been filed. Is it normal to reduce the weight in batches?

知乎SEO怎么做,如何提高知乎SEO排名?

解析协同办公“协同”为何意,数字化办公又如何轻松“破题”?

“OpenCvSharp.Mat“的类型初始值设定项引发异常

How to do Zhihu SEO and how to improve Zhihu SEO ranking?
![[C exercise] print 'x' graphics](/img/4a/d9b69b787bf585b4786b2074063370.png)
[C exercise] print 'x' graphics

泛云桌面关键技术分析

容器介绍及总结
![[HCIA] OSI model](/img/e2/bfbd7f9b452d4f755be43f5c91f37e.png)
[HCIA] OSI model
![[C exercise] print diamond](/img/fb/3d60abbadbed41b51e76cf397407e8.png)
[C exercise] print diamond
随机推荐
Idea setting / modifying shortcut keys
STM32与物联网02-网络数据收发
Enterprise station, there is ranking, no traffic, how to do?
[C exercise] find the K power of n (recursive implementation)
Issue 45: systematic learning ability
保持电气化时代的交通安全“零伤亡”,沃尔沃底气何来?
30万奖池等你来战!自然语言处理(NLP)赛事合集来啦
蜘蛛抓取频率提高,可以快速增加网站收录吗?
fast Fourier transform
VDD,VCC,VSS,GND,地之间有何区别?
送你的代碼上太空,與華為雲一起開發“最偉大的作品”
第四十五期:系统学习能力
容器介绍及总结
2022-07-14 第五小组 修身课 学习笔记(every day)
Codeforces Round #736 (Div. 2) (A-D)
泛云桌面关键技术分析
[C data storage]
Compare the high-quality [test report] template with your own?
Codeforces Round #736 (Div. 2) (A-D)
高权重没备案网站,批量降权,正常吗?