3D数学
3D数学
1、数学计算公共类Mathf
1、Mathf和Math
Math是c#中封装好的用于【数学计算的工具类】位于system命名空间中
public static class Math{
}
Mathf是Unity中封装好的用于【数学计算的工具结构体】位于UnityEngine命名空间中
public struct Mathf{
}
他们都是提供来用于进行【数学相关计算】的
2、区别
一个是静态类,一个是结构体
Mathf比Math有更多的数学计算方法
3、Mathf中的常用方法(一般计算一次)
void Start()
{
1、π PI
Mathf.PI
2、绝对值 Abs
3、向上取整 CeilToInt
print(Mathf.CeilToInt(1.3f)); //2
4、向下取整 FloorToInt
5、钳制函数 Clamp (在参数二,和参数三的范围里取值)
print(Mathf.Clamp(10, 11, 22)); //11
print(Mathf.Clamp(23, 11, 22)); //22
print(Mathf.Clamp(16, 11, 22)); //16
6、获取最大值 Max
7、获取最小值 Min
8、一个数的n次方 Pow
print(Mathf.Pow(2, 3)); //2的3次方
9、四舍五入 RoundToInt
10、返回一个数的平方根 Sqrt
11、判断一个数是否是2的n次方 IsPowerOfTwo
print(Mathf.IsPowerOfTwo(1)); //true
12、判断正负数 Sign
print(Mathf.Sign(8)); //1
print(Mathf.Sign(-8)); //-1
}
4、Mathf中的常用方法(一般不停计算)
float start = 0;
float result = 0;
float time = 0;
void Update()
{
插值运算 Lerp
Lerp函数公式
result = Mathf.Lerp(start,end,t);
t为插值系数,取值范围为0~1,计算方法:
result = start + (end - start) * t
插值运算用法1
每帧改变start的值:变化速度先快后慢,位置无限接近,但是不会得到end位置
//此处相当于log函数
start = Mathf.Lerp(start, 10, Time.deltaTime);
插值运算用法2
每帧改变t的值:变化速度匀速,每帧接近,当t>=1时,得到结果
//此处相当于正比例函数
time += Time.deltaTime;
result = Mathf.Lerp(result, 10, time);
}
练习 A物体跟随B物体移动
using UnityEngine;
public class FollowCube : MonoBehaviour
{
public Transform B;
public float moveSpeed = 1;
private Vector3 pos;
private Vector3 bNowPos;
private Vector3 startPos;
private float time;
void Update()
{
//先快后慢
//pos = transform.position;
//pos.x = Mathf.Lerp(pos.x, B.position.x, Time.deltaTime * moveSpeed);
//pos.y = Mathf.Lerp(pos.y, B.position.y, Time.deltaTime * moveSpeed);
//pos.z = Mathf.Lerp(pos.z, B.position.z, Time.deltaTime * moveSpeed);
//transform.position = pos;
//匀速运动
if (bNowPos!= B.transform.position)
{
time = 0;
bNowPos = B.transform.position;
startPos= transform.position;
}
time += Time.deltaTime;
pos.x = Mathf.Lerp(startPos.x, bNowPos.x, time * moveSpeed);
pos.y = Mathf.Lerp(startPos.y, bNowPos.y, time * moveSpeed);
pos.z = Mathf.Lerp(startPos.z, bNowPos.z, time * moveSpeed);
transform.position = pos;
}
}
2、三角函数
1、角度和弧度
1.角度和弧度
角度:1°
弧度:1 (radian)
圆一周的弧度:2π (radian)
2.角度和弧度的关系
π rad = 180°
则:
1 rad ≈ 57.3°
1° ≈ 0.01745 rad
角度 = 弧度 * 57.3
float rad = 1;
float angle = rad * Mathf.Rad2Deg;
弧度 = 角度 * 0.01745
angle = 1;
rad = angle * Mathf.Deg2Rad;
2、三角函数
Mathf中,只使用弧度,所以要角度转弧度(Deg2Rad)
sin30°
Mathf.Sin(30 * Mathf.Deg2Rad);
3、反三角函数
通过反三角函数计算正弦值或余弦值对应的弧度值
弧度 = Mathf.Asin(正弦值)
弧度 = Mathf.Acos(余弦值)
float radian = Mathf.Asin(0.5f); //先将值转化为弧度
float degree = radian * Mathf.Rad2Deg; //再将弧度转角度
print(degree);
练习 物体曲线移动
using UnityEngine;
public class SinMove : MonoBehaviour
{
public float moveSpeed = 1; //前后移动
public float changeSpeed = 2; //左右移动
public float changeSize = 5; //左右幅度
private float time = 0;
void Update()
{
time += Time.deltaTime * changeSpeed;
//前后移动
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
//左右移动
transform.Translate(Vector3.right * changeSize * Time.deltaTime * Mathf.Sin(time));
}
}
3、Unity中的坐标系
1、世界坐标系
原点:世界的中心点
轴向:世界坐标系的三个轴向是固定的
transform.position;
transform.rotation;
transform.eulerAngles; //欧拉角
transform.lossyScale; //缩放
2、物体坐标系
原点:物体的中心点(建模时决定)
轴向:
物体的方右为x轴正方向
物体的上方为y轴正方向
物体的前方为z轴正方向
//相对父对象的坐标
transform.localPosition;
transform.localRotation;
transform.localEulerAngles;
transform.localScale;
3、屏幕坐标系
原点:屏幕左下角
轴向:
向右为x轴正方向
向上为y轴正方向
最大宽高:
Input.mousePosition;
Screen.width;
Screen.height;
4、视口坐标系
原点:屏幕左下角
轴向:
向右为x轴正方向
向上为y轴正方向
特点:
左下角(0,0)
右上角(1,1)
和屏幕坐标类似,将坐标单位化
摄像机上的【视口范围】属于视口 Viewport Rect
5、坐标转换
世界转本地
transform.InverseTransformDirection;
transform.InverseTransformPoint;
transform.InverseTransformVector;
本地转世界
transform.TransformDirection