Three-Dimensional Mathematics (1)

Video tutorial: https://www.bilibili.com/video/BV12s411g7gU?p=155

Table of contents

vector

Vector operations (1)

Trigonometric functions

Vector operations (2)

Vector Common Properties and Methods


vector

A list of numbers, indicating the directional displacement in each dimension; it is also a physical quantity with size and direction, the size and the modulus length of the vector, and the direction is the direction of the vector in space, which can represent the position and direction of the object

vector form

 

the size (modulus) of the vector

the square root of the sum of the squares of the vector components 

  

API : 

Vector3.magnitude : modulus length

Vector3..sqrMagnitude : the square of the modulus

the direction of the vector 

Obtaining the vector direction is also called "normalized vector", or "normalized vector", that is, the unit vector of the vector (a vector with a size of 1).

Geometric meaning: lengthen or shorten the vector so that the modulus length is equal to 1

  

API:

Vector3.normalized : get the unit vector of this vector

Vector3 vector2=vector1.normalized;//vector2为vector1的单位向量

Vector3.Normalize : Sets the vector itself as a unit vector

vector1.Normalize();//将vector1自身设置为单位向量
private void Update()
{
    Demo01();
    Demo02();
}

//模长
private void Demo01()
{
    Vector3 pos = this.transform.position;

    float m01 = Mathf.Sqrt(Mathf.Pow(pos.x, 2) + Mathf.Pow(pos.y, 2) + Mathf.Pow(pos.z, 2));
    float m02 = pos.sqrMagnitude;
    float m03 = Vector3.Distance(Vector3.zero, pos);

    Debug.LogFormat("{0}--{1}--{2}", m01, m02, m03);
    
    Debug.DrawLine(Vector3.zero, pos);
}

//方向
private void Demo02()
{
    Vector3 pos = this.transform.position;
    Vector3 n01 = pos / pos.magnitude;
    Vector3 n02 = pos.normalized;

    Debug.DrawLine(Vector3.zero, n02, Color.red);
}

Vector operations (1)

Vector subtraction : equal to the addition and subtraction of each component, used to calculate the distance and relative direction between two points

Geometric meaning: vector a is subtracted from vector b, and the result is understood as a vector starting from the end point of b and ending at the end point of a. direction from b to a

 

Vector addition : equal to the sum of each component, applied to object movement 

Geometric meaning: add vector a to vector b, translate to make the start point of b coincide with the end point of a, and the result is a vector with the start point of a as the start point and the end point of b as the end point

Vector and scalar multiplication and division 

Multiplication: Multiply each component of the vector by a scalar; k[xy,z]= [xk,yk,zk]

Division: each component of the vector is divided by a scalar; [x,y,z]/k = [x/k,y/k,z/k]

Geometric meaning: scaling vector length  

ps: When you want to scale a certain vector to a specific length, you can first obtain the direction vector of the vector, and then multiply/divide it by the scale factor


public Transform t1, t2, t3;

private void Update()
{
    Demo03();
}

//向量运算
private void Demo03()
{
    //t1相对于t2的位置
    //其大小为两点间距离
    Vector3 relativeDirection = t1.position - t2.position;

    //t3沿relativeDirection方向移动
    if (Input.GetKeyDown(KeyCode.A))
    //获取方向向量,避免两物体间距离对速度造成影响
    //t3.Translate(relativeDirection.normalized * 0.5f);
    t3.position+=relativeDirection.normalized;

    Debug.DrawLine(Vector3.zero, relativeDirection);
}

Trigonometric functions

measure of angle

PI=180 degrees 1 radian=180 degrees/PI 1 angle=PI/180 degrees

Angle --> radian: radian = number of angles * PI/180 

API: Radian = number of degrees * Mathf.Deg2Rad

Radian --> Angle: Angle = radian number * 180/PI

API: angle = number of radians * Mathf.Rad2Deg

private void Demo01()
{
    //角度-->弧度:弧度=角度数*PI/180
    float d1 = 60;
    float r1 = d1 * Mathf.PI / 180;
    float r2 = d1 * Mathf.Deg2Rad;
    print("角度-->弧度:" + d1 + "-->" + r1 + "/" + r2);
}

private void Demo02()
{
    //弧度-->角度:角度=弧度数*180/PI
    float r1 = 3;
    float d1 = r1 * 180 / Mathf.PI;
    float d2 = r1 * Mathf.Rad2Deg;
    print("弧度-->角度:" + r1 + "-->" + d1 + "/" + d2);
}

Trigonometric functions 

Established the relationship between the ratio of angle and side length in right triangle

Sine: sin x = a/c

Cosine: cos x = b/c

Tangent: tan x = a/b 

API ( in radians ):

Sine: Mathf.Sin

Cosine: Mathf.Cos

正切:Mathf.Tan

inverse trigonometric functions

The general term for functions such as arcsine, arccosine, and arctangent; it can be used to calculate angles based on the length of both sides

Arcsine: arcsin a/c = x

Inverse cosine: arccos b/c = x

arc tangent: arctan a/b = x;

API ( in radians ):

Arcsine: Mathf.Asin

Arc cosine: Mathf.Acos

Arc tangent: Mathf.Atan

private void Demo03()
{
    //已知角度x,边长b,求边长a
    float x = 50, b = 20;
    float a = Mathf.Tan(x * Mathf.Deg2Rad);

    //已知边长a,边长b,求角度angle
    float angle = Mathf.Atan(a / b) * Mathf.Rad2Deg;

    print(angle);
}

private void Demo04()
{
    //将自身坐标系转换到世界坐标系中
    //Vector3 worldPos = transform.TransformPoint(0, 0, 10);

    //计算物体前方30度,10m远的坐标
    float x = Mathf.Sin(30 * Mathf.Deg2Rad) * 10;
    float z = Mathf.Cos(30 * Mathf.Deg2Rad) * 10;
    Vector3 worldPos = transform.TransformPoint(x, 0, z);
    print(worldPos);
}

Transform.TransformPoint(Vector3 position) : Transform position from local space to world space

Vector operations (2)

point multiply

Also known as "dot product" or "inner product" 

  

Geometric meaning: the unit vector of two vectors is multiplied and then multiplied by the cosine of the angle between them 

a-b=|a|·|b| cos<a,b>

Application: Calculate the angle between two vectors; for a standardized vector, the result of the dot product is equal to the cosine of the angle between the two vectors 

API: 

Vector3.Dot : The dot product of two vectors. The dot product is a floating-point value equal to multiplying the magnitudes of the two vectors together and then multiplying by the cosine of the angle between the vectors

public Transform t1, t2, t3;
//点乘
//计算两向量夹角的cos值
float dot = Vector3.Dot(t1.position.normalized,t2.position.normalized);
float angle= Mathf.Acos(dot)*Mathf.Rad2Deg;

ps: Point multiplication can calculate the cos value of the angle between two vectors, but it cannot determine the quadrant of the angle; if you want to determine the quadrant of the angle, you should combine the cross product of the vector

For a standardized vector, if the two directions are the same, the result of the dot product is 1; otherwise, the result of the dot product is -1; the result of being perpendicular to each other is 0

cross product 

Also known as "cross product" or "outer product"

Geometric meaning: the result is the vertical vector of the surface formed by the two vectors, and the modulus length is the product of the modulus length of the two vectors multiplied by the sine of the included angle

Application: 1. Create a vector perpendicular to the plane; 2. Determine the relative position of two vectors.

API:  

Vector3.Cross : the cross product of two vectors, the cross product of two vectors generates a third vector, which is perpendicular to the two input vectors

//点乘
//计算两向量夹角的cos值
float dot = Vector3.Dot(t1.position.normalized,t2.position.normalized);
float 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);

ps: Orient the resulting vector according to the "left-hand rule"

The modulus length and angle of the vector obtained by cross product: 0~90 degrees

Vector Common Properties and Methods

static property 

1.Vector3.up-->new Vector3(0,1,0)

2.Vector3.down-->new vector3(0,-1,0)

3.Vector3.left-->new Vector3(-1,0,0)

4.Vector3.right-->new Vector3(1,0,0)

5.Vector3.forward-->new Vector3(0,0,1)

6.Vector3.back-->new Vector3(0,0,-1)

static method

1.Vector3.Lerp&Vector3.LerpUnclamped

 Vector3.Lerp

//将物体移动到(0,0,10)
//先快后慢 不能到达目标点(无限接近)
this.transform.position = Vector3.Lerp(this.transform.position, targetPos, 0.1f);

 Vector3.LerpUnclamped

parameter settings 

Click on the Curve property to edit the curve 

public AnimationCurve curve;
private float x = 0;
//持续时间
public float duration;

x += Time.deltaTime / duration;
//自然运动 起始点固定 终点固定 比例根据曲线变化
this.transform.position = Vector3.LerpUnclamped(Vector3.zero, targetPos, curve.Evaluate(x));

2. Vector3.MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta) : Calculate the position between the point specified by current and the point specified by target, and the moving distance does not exceed the distance specified by maxDistanceDelta

current start position of the move
target target location of the move
maxDistanceDelta The distance moved per call
//将物体移动到(0,0,10)
//匀速移动,可到达目标点
this.transform.position = Vector3.MoveTowards(this.transform.position, targetPos, 1);

3. Vector3.SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed= Mathf.Infinity, float deltaTime= Time.deltaTime) : gradually change a vector to the desired target over time

current current location
target what to try to achieve
currentVelocity current velocity, this value is modified by the function on each call
smoothTime Approximate time required to reach the goal. The smaller the value, the faster the goal will be reached
maxSpeed Option to allow limiting max speed
deltaTime The time since the last call to this function. Time.deltaTime by default

4. Vector3.Angle(Vector3 from, Vector3 to) : returns the angle between two vectors in degrees

from source vector to measure angular difference
to target vector for measuring angular difference

5. Vector3.ProjectOnPlane(Vector3 vector, Vector3 planeNormal) : Projects a vector onto a plane defined by a normal (the normal is normal to the plane)

planeNormal direction from vector to plane
vector the position of the vector above the plane
public Transform t1;
public Vector3 planeNorm;
private Vector3 targetPos = new Vector3(0, 0, 10);

private void Update()
{
    Vector3 res = Vector3.ProjectOnPlane(t1.position, planeNorm);
    
    Debug.DrawLine(Vector3.zero, t1.position);
    Debug.DrawLine(Vector3.zero, res, Color.red);
}

parameter settings 

 

run 

 

6.Vector3.Reflect( Vector3  inDirection,  Vector3  inNormal): Reflect a vector from the plane defined by the normal

The inNormal vector defines a plane (the normal of the plane is a vector perpendicular to its surface), the inDirection vector is regarded as a directional arrow entering the plane, and the return value is a vector with the same size as inDirection and the direction of its reflection direction

Guess you like

Origin blog.csdn.net/qq_53401568/article/details/128539227