Mathf类常用静态变量、静态函数
- Deg2Rad、Rad2Deg
- 角度和弧度间转化参数
- 等于180/PI
- Epsilon:
- 微小浮点值(只读)
- 零的右极限
- Infinity、NegativeInfinity
- 正无穷和负无穷参数
- 用于射线检测无穷远处举例:
Physics.Raycast(Vector3.zero, Vector3.forward, Mathf.Infinity)
- PI
- 圆周率
- Abs
- 返回绝对值
- Acos、Asin、Atan
- 三角函数反函数
- Ceil
- 返回大于f的最小整数
- Floor
- 返回小于f的最大整数
- Atan2
- 返回二维坐标向量(x,y)与x轴正半轴夹角的角度
- 采用弧度制,值在-PI和PI之间
- 会考虑点落在x轴上的特殊情况
- Clamp
- Mathf.Clamp(x,y,z)将x限制在y和z之间
- 若x小于最小值y 返回y
- x大于最大值z 返回z
- xValue取得xMin和xMax间的简谐振动正弦值
- DeltaAngle
- 返回两个角度在一个周期内的最小差异
- 例如Mathf.DeltaAngle(1080,90)和Mathf.DeltaAngle(360,90)都返回90
- 以角度制为单位
- Lerp和LerpAngle
- 都属于线性插值,原理相似
- Mathf.Lerp(a,b,t) a为初始值、b为期望值、t为阻尼(描述插值点与b的接近程度,0<t<1)
- =a+(b-a)*t
- Sign
- Sign(x)返回x的符号,大于零取正、小于零取负
- SmoothDamp/SmoothDampAngle
-
弧形递增或衰减
-
Mathf.SmoothDamp(float current,float target,ref float currentVelocity,float maxspeed,float Deltatime)
-
-
- sqrt\pow
- 开方和指数运算
向量静态变量与函数
- Vector3.forward\right\back\down\up\left
- 根据左手坐标系定义的轴系单位向量
- (0,0,1)(1,0,0)(0,0,-1)(0,-1,0)(0,1,0)(-1,0,0)
- zero\one
- 零向量和(1,1,1)
- magnitude/normalized
- 求向量模长和单位化方向向量
- sqrMagnitude
- 模长平方用于勾股定理和夹角运算
- Equal
- 近似等于,浮点数的不精确性
- Equal引用类型比较地址中内容是否相同,==判断地址
- Vector3.toString(string format)
- 保留小数位数
Vec=new Vector3(1.234,5.678,9.0000);Debug.log(Vec.toString("F2"));//we got (1.23,5.67,9.00);
- 保留小数位数
- Vector3.Angle
- Vector3.Angle(Vector3 from,Vector3 to)//from源向量 to目标向量
- 返回两个向量的夹角
- 值在0-180之间
- Vector3.ClampMagnitude
- 限制向量的最大模长度
- Vector3.Cross/Dot
- 叉积和点积
- 叉积计算平面法向量
- cross(a,b)/2 cross(a,b)计算平面三角形、平行四边形的面积
- Vector3.Lerp/Slerp Vector3.Lerpunclamp/Slerpunclamp
- 线性插值和球形插值
- Vector3.Lerp(Vector3 a,Vector b,float t)
- Lerpunclamp和Slerpunclamp表示插值可以返回a,b所在直线上的任意一点
- Lerp和Slerp将插值限定在a,b线段上(t<0返回a t>1返回b)
- Vector3.MoveTowards(Vector3 current,Vector3 Target,float maxDistanceDelta)
- //移动开始、目标位置、以帧为计数单位计算移动距离
- 确保与帧速率无关乘以deltaTime参数
- Vector3.OthroNormalize(ref Vector3 normalize,ref Vector3 targent,ref Vector3 binomal)
- 根据第一个向量在三维坐标中建立坐标系并返回剩余两轴的单位向量
- Vector3.Project(Vector3 vector,Vector3 onNormal)
- 返回向量在法向上的投影向量
-
- Vector3.ProjectOnplane(Vector3 vector,Vector Normal)
- 根据法向量Normal确定垂直的平面,求得vector在该平面上得投影向量
- Vector3.Reflect(Vector3 inDiraction,Vector3 inNormal)
- 根据入射向量和镜面法向量求出出射向量
-
- Vector3.RotateTowards((Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)
- //target标识向量旋转方向 maxRadiansDelta旋转的弧度角度
- maxRadiansDelta为负号标识相反方向,超过角度返回target向量
- Vector3.Scale(Vector vec1,Vector vec2)
- 将两个向量的分量分别相乘,结果返回最终分量中
关于四元数
- q=(qx,qy,qz,qw)
- qx,qy,qz定义一个旋转轴,qw描述旋转的角度θ
- 对应角度θ=arccos(qw)*2 对应轴Axis=(qx,qy,qz)/sin(θ/2)
- 取反不改变旋转方向,连乘多次操作
- identity
- 四元数(0,0,0,1)
- //θ=0 Axis=(0,0,0)
- eulerAngles
- 返回设置旋转的欧拉角
- Quaternion.SetFromtoRotation(Vector3 from,Vector3 to)
- 创建从from到to向量的旋转四元数
- Quaternion.LookRotation(Vector3 forward,Vector3 upward)
- 根据局部坐标系中的forward和up两个向量创建一个局部坐标系得旋转
- Quaternion.ToAngleAxis(out float angle,out Vector3 axis)
- 将四元数拆解成旋转角度和旋转轴
- 角度返回角度制参数
- Quaternion.AngleAxis(float angle,Vector3 axis)
- 根据提供的角度和轴创建一个四元数
- 角度采用角度制
- Quaternion.Angle(Quaternion a,Quaternion b)
- 返回两个四元数a和b之间的旋转角度
- Quaternion.Euler(float x,float y,float z)
- 根据欧拉角创建一个对应的四元数
- Quaternion.Inverse(Quaternion rotation)
- 等价于四元数取逆
- 返回四元数的反向
- Quaternion.RotateTowards(Quternion from,Quaternion to,float maxDegreesDelta)
- 将from 四元数朝to旋转maxDegreesDelta的角度步长