当前位置:网站首页>Unity - Fundamentals of 3D mathematics
Unity - Fundamentals of 3D mathematics
2022-07-28 21:00:00 【Small digital media members】
It took less than half a day today to get a preliminary understanding 3D Mathematical basis ! Enter tomorrow unity Game script writing !
One sentence per day :“ The sun on earth is just right The wind blows through the forest We were young at that time ”
Catalog
· overall situation ( The world ) Coordinate system :
· Local ( Model 、 object ) Coordinate system :
· Calculate the distance between two objects
Coordinate transformation principle
Conversion between world coordinates and local coordinates : Change global and local coordinates
Conversion between world coordinates and screen coordinates : Set world coordinates
· How to set the world coordinate position of an object by clicking the mouse
3D Coordinate system :
· overall situation ( The world ) Coordinate system :
A benchmark that describes the position and direction of all objects in the game scene
Hierrarchy In the view Position attribute by (1,2,3) It means that it is at... From the origin of the global coordinate system x There is... In the direction of the axis 1 Unit coordinates , stay y There is... In the direction of the axis 2 Unit coordinates , stay z There is... In the direction of the axis 3 Unit coordinates
When the game object Yes When the parent object , Its Hierrarchy In the view Position Is shown as Local coordinates , When the game object nothing When the parent object , Its Hierrarchy In the view Position Show Both local and global coordinates
· Local ( Model 、 object ) Coordinate system :
Every object has its own coordinate system , And move or rotate with the object
Model mesh The saved vertex coordinates are Coordinates in the local coordinate system
The coordinate system of the parent-child object is related , The child object will take a coordinate point of the parent object as its coordinate origin
· Camera coordinate system :
The coordinate system established according to the observation position and direction
Judge whether the object is in front of the camera , And the sequence of occlusion between objects
· Screen coordinate system :
Two dimensional coordinate system established on the screen , Used to describe the position of pixels on the screen , Where the origin position (0,0) In the lower left corner of the screen
vector
Used to describe physical quantities with two attributes of size and direction
Vector operation :
· Addition and subtraction
· Number multiplication
A vector is multiplied by a scalar
· Point multiplication
Multiply two vectors to get a scalar , The value is equal to the cosine of the angle between the two vectors multiplied by the length of the two vectors
· Cross riding
The cross multiplication of two vectors yields a new vector , The new vector is perpendicular to the original two vectors , And the length is equal to the sine of the angle multiplied by the length of the original vector
Vector3 class
Vector3 Member variables 
Vector3 Method 
Vector3 application :
· Calculate the distance between two objects
public class Distance : MonoBehaviour
{
// Start is called before the first frame update
public Transform objTrans1;
public Transform objTrans2;
void Start()
{
float dis = Vector3.Distance(objTrans1.position, objTrans2.position);
Debug.Log("Distance:" + dis);
}
// Update is called once per frame
void Update()
{
}
}(Debug.Log: Record the message to Unity Console )
Bind the script to the main camera in the scene Main Camera, And then again Inspector In the view objTrans1、objTrans2 Designated as 2 individual cube
function 、CTRL+SHIFT+C Call up the console
· Slow motion
Vector3.Lerp Method
The slow motion effect of moving an object from its current position to another position in the coordinate system
public class MoveToTarget : MonoBehaviour
{
public Transform endTrans;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.Lerp(transform.position, endTrans.position, Time.deltaTime);
}
}Time.deltaTime Express Update() Time taken to complete the previous frame on the method
Bind the script to Cube On , And then in it Inspector In view endTrans Designated as Cube(1)
Homogeneous coordinates
Put the original three-dimensional vector (x,y,z) Use four-dimensional vector (wx,wy,wz,w) To express
Purpose of introduction :
Better distinguish between vectors and points ;
The translation is uniformly expressed by matrix multiplication 、 rotate 、 Zoom transform
When the weight w=0 Time can be used to represent the point at infinity
Four yuan number
It contains a scalar component and a three-dimensional vector component , Four yuan number Q Write it down as Q=[w,(x,y,z)]
In the game object Transform In the component , Variable Transform.rotation Is the rotation of the object in the global coordinate system ; Variable Transform.localRotation Is the rotation of the object in the local coordinate system of the parent object
application : Control the rotation of objects through quaternions
Coordinate transformation principle
Conversion between world coordinates and local coordinates : Change global and local coordinates
ransform Component's Transform.TransformPoint Method can convert coordinate points from local coordinate system to global coordinate system ,Transform.InverseTransformPoint Method can convert coordinate points from global coordinate system to local coordinate system
Conversion between world coordinates and screen coordinates : Set world coordinates
stay Unity in , Because the screen picture is made by the camera (Camera) Presented , Therefore, the conversion between world coordinates and screen coordinates needs to take the camera as the medium
· How to set the world coordinate position of an object by clicking the mouse
newly build C# Script
public class ScreenToWorldPoint : MonoBehaviour
{
Vector3 screenPos;
// Start is called before the first frame update
void Start()
{
screenPos = Camera.main.WorldToScreenPoint(transform.position);
// Print coordinate information
Debug.Log(" Screen coordinates :" + screenPos);
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
// Get the position coordinates of the mouse
Vector3 mousePos = Input.mousePosition;
// Set the screen coordinates of the object z The axis value is assigned to the mouse coordinate z Axis
mousePos.z = screenPos.z;
// Change the screen coordinates into world coordinates
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
// Set object position
transform.position = worldPos;
}
}
}Bind script to Cube On the cube
When the run starts , The console will output this Cube The world coordinates of are converted into the coordinates of the screen ; When the mouse pointer is Game View , When left clicking , You can see Cube The position of will follow the position of the mouse pointer
After the world coordinates are converted into screen coordinates , The screen coordinates are z The axis is larger than 0, It means that the coordinate is in front of the camera , conversely , It means that the coordinate is behind the camera
边栏推荐
- Unit editor details
- Want to draw a picture that belongs to you? AI painting, you can also
- 取色器实战(Qt含源码)
- 【周周有奖】云原生编程挑战赛“边缘容器”赛道邀你来战!
- Confusing knowledge points of software designer examination
- How do we do full link grayscale on the database?
- 什么是低代码?哪些平台适合业务人员?用来开发系统靠不靠谱?
- The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
- C # basic 3-value type and reference type, packing and unpacking
- C foundation 2-encapsulation, inheritance, polymorphism
猜你喜欢

Unity3d tutorial notes - unity initial 02

Random talk on GIS data (VI) - projection coordinate system

Pl515 SOT23-5 single / Dual Port USB charging protocol port controller Parkson electronic agent

Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

Integrating database Ecology: using eventbridge to build CDC applications

EasyNLP中文文图生成模型带你秒变艺术家

How do we do full link grayscale on the database?

Explain the mobile control implementation of unity in detail

Unity foundation 6-rotation
随机推荐
Fragment中使用ViewPager滑动浏览页面
Space game Lesson 12: shield
Lvs+keepalived high availability deployment practical application
Three deletion strategies and eviction algorithm of redis
C foundation 8-reflection and dependency injection
Looking at SQL optimization from the whole process of one query
C # basic 1-events and commissions
Record an error in runtime. Getruntime().Exec ("command")
MySQL修改端口号(修改mysql的端口号会有问题吗)
Redis 3.0 source code analysis - data structure and object SDS list Dict
记一次Runtime.getRuntime().exce(“command“)报错
Oracle library access is slow. Why?
Job CE
Observer mode, object pool
有奖征文 | 2022 云原生编程挑战赛征稿活动开启
Unity foundation 2 editor expansion
Unit editor details
How can enterprises successfully complete cloud migration?
Network layer performance test
Prize essay solicitation | 2022 cloud native programming challenge draft activity opens