Colve

Code change world. Game change life.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Mathf类是Unity中的数学类,属于结构体类型,只有静态属性和静态方法,即不可实例化。在使用时直接调用其静态属性或静态方法。

 
Mathf类静态属性
Deg2Rad属性 : 从角度到弧度常量
public const float Deg2Rad = 0.0174533f;
功能说明 此属性用来计算数学计算中从角度到弧度转变的常量值,其值为(2 * Mathf.PI) / 360 - 0.01745329,属性只读。
提示 Rag2Deg与此属性功能相反,是从弧度到角度的转换常量,其值为57.2958f。
 
Infinity属性:正无穷大
public const float Infinity = 1.0f / 0.0f;
此属性用来表示在数学计算中的正无穷大,只读。其计算规则及使用说明如下。
1.Mathf.Infinity ÷ x = Mathf.Infinity,其中x为一个具体数值。
2.Mathf.Infinity ÷ Mathf.Infinity = NaN,即计算结果不是数值。
3.Mathf.Infinity 只是在Unity中的一个正无穷大数值的表示,不代表任何具体数值,不能用在具体的数值计算中。
 
Mathf类静态方法
Clamp方法:返回有限范围值
public static float Clamp(float value,float min,float max):
public static int Clamp(int value, int min, int max);
参数min为返回值的最小值,max为最大值。
功能说明 此方法用来返回有范围限制的value值,当value∈[min,max]时返回value值,当value < min时返回min值,value > max时返回max值。
提示 方法Clamp01与此方法类似,不同的是Clamp取值范围为[0,1],只有一个参数。
 
ClosestPowerOfTwo方法:返回2的某次幂
public static int ClosetPowerOfTow(int value);
功能说明 此方法用来返回最接近参数值value的2的某次幂,当value属于中间值时取较大值,当value小于0时,返回0。
 
DeltaAngle方法:最小增量角度
public static float DeltaAngle(float current, float target);
其中参数current为当前角度,参数target为目标角度。
功能说明 此方法用来返回从参数值current到target的最小增量角度值。计算方法为,首先将current和target按照360度为一周换算到区间(-180 , 180]中,设current和target换算后的值分别对应c和t,他们在坐标轴的夹角为e( 0 <= e <= 180),则若e经过顺时针旋转e度到达t,则返回值为e。
 
InverseLerp方法:计算比例值
public static float InverseLerp(float from, float to, float value);
其中参数from为起始值,参数to为终点至,参数value为参考值。
功能说明 此方法用来返回value值再从参数from到to中的比例值。设f = Mathf.Invese Lerp(from, to, v),其中f,from,to 和 v 都是float类型,m = v - from / to - from,则若 m∈[0.0f,1.0f],则f = m,若m < 0,则f = 0;若m > 1则f = 1。
 
Lerp方法:线性插值
public static float Lerp(float from, float to, float t);
参数from为起始值,to为结束值,t为插值系数。
功能说明 此方法的功能是用来返回一个从from到to范围的线性插值。返回值的计算方法为(to - from) * t' + from,其中
1.参数t的有效取值范围为[0,1],当t < 0时t' = 0, 当t > 1时,有效值t' = 1;
2.参数from和to为任意的float数值,from和to之间没有任何约束关系。
 
LerpAngle方法:角度插值
public static float LerpAngle(float a , float b ,float t);
参数a为起始角度,b为结束角度,t为插值参数
功能说明 此方法的返回值是用来返回从角度a到b之间的一个插值。此方法功能与Lerp(from:float,to:float,t:float)类型,只是此方法主要用来对角度之间进行插值,其规则如下:
1.a和b可以为任意的float类型数值,a可以大于b,也可小于b,它们之间没有任何约束关系。
2.插值系数t的有效取值范围为[0,1],当t < 0时其有效值t' = 0,当t > 1时其有效值t' = 1。
3.插值计算之前需要先对a,b进行规范化,以确定需要插值的大小,对a、b规范会规则如下
        a' = 360 * k + a,其中k为证书,苛求k的值使得a'∈[0,360];
设对a、b进行规范化后的值分别为a'、b',它们的角度值插值为c,c∈[0,180]。当a'沿着顺时针方向旋转c度与b'重合时,则插值计算方式为f = a -c * t',其中f即为方法的返回值,t'为t的有效值;当a'逆时针旋转c度与b'重合时,插值计算方式为:f = a + c * t'。f即为方法返回值,t'即为t的有效值;
 
MoveTowards方法:选择性插值
public static float MoveToeards(float current, float target, float maxDelta);
功能说明 此方法的功能是返回一个从current到target之间的插值,返回值受maxDelta值得约束。设a、b和d分别为3个float类型的数值,且方法MoveTowards(a,b,d)的返回值为c,则返回值c的计算方式如下。
1.若a < b:当 a + d < b时,c = a + d;当 a + d > b时,c = b;
2.若a > b:当 a - d > b时,c = a - d;当 a + d < b时,c = b;
 
MoveTowardsAngle方法:角度的选择性插值
public static float MoveTowardsAngle(float current,float target,float maxDelta);
功能说明 此方法的作用是返回一个从当前角度curent向目标角度target旋转的插值,每帧旋转角度不超过maxDelta度。此方法与方法MoveTowards方法功能类似,此方法主要用于角度的旋转变换,maxDelta值越大,旋转速度相对越快。
 
PingPong方法:往复运动
public static float PingPong(float t,float length);
功能说明 此方法用于模拟乒乓球的往复运动。设f = Mathf.PingPong(t , m),其中f、t和l均为float类型数值。
1.若m > 0,则: 当| t | % 21 <= 1时; f = | t | % m。当| t | % 21 > m 时; f = | t | % m。
2.若m < 0,则: 当| t | % 21 <= 1时; f = 2m + | t | % 2m。当| t | % 21 > m时; f = - | t | % | 2m |。
 
Repeat方法:取模运算
public static float Repeat(float t,float length);
功能说明 此方法的作用类似于浮点数的取模运算。设f = Mathf.Repeat(t, m),其中f、t和为float类型数值,则:
1.当 t > 0 时, m > 0 时 , f = t % m。
2.当 t < 0 时, m < 0 时 , f = - ( -t % -m) 。
3.当 t > 0 时, m < 0 时 , f = m + t % -m。
4.当 t < 0 时, m > 0 时 , f = -( t + ( - t ) % m) 。
5.当 m = 0时,f = NaN。
 
Round方法;浮点数的整数值
public static float Round(float f);
功能说明 此方法的作用是返回离f最近的整形浮点值。设a和b分别为浮点数f的整数部分和小数部分,即f = a + b,则Round( f )的计算规则如下。
1.当| b | < 0.5时,无论f为正数还是负数,返回值为a。
2.当| b | > 0.5时,若f为正数,则返回值为a + 1;若f为负数,则返回值为a-1。
3.当| b | = 0.5时,若a为偶数,则返回值为a;若a为奇数,则当f < 0时返回值为a-1,当f > 0时返回值为a + 1。
 
SmoothDamp方法:模拟阻尼运动
1.public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime);
2.public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed);
3.public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime);
参数curent为起始值,target为目标值,currentVelocity为当前帧速度,ref类型;参数smoothTime为预计平花时间,参数maxSpeed为当前帧最大速度值,默认为Mathf.Infinity;参数deltaTime为平滑时间,值越大返回值也相对越大,一般用Time.deltaTime计算。
功能说明 此方法的功能是模拟平滑阻尼运动,并返回模拟插值。smooth:float,预计平花时间,物体月靠近目标,加速度的绝对值越小。实际到达目标的时间往往要比预计时间大很多,建议smoothTime的取值范围为(0.0f,1.0f),若想控制物体到达目标的时间可以通过控制maxSpeed到达目的。maxSpeed:float = Mathf.Infinity,每帧返回值的最大值,默认值为Mathf.Infinity。
 
SmoothDampAngle方法:模拟阻尼旋转
1.public static float SmoothDampAngle(float current, float target, ef float curretVelocity, float smoothTime);
2.public static float SmoothDampAngle(float current, float target, ef float curretVelocity, float smoothTime,float maxSpeed);
3.public static float SmoothDampAngle(float current, float target, ef float curretVelocity, float smoothTime,float maxSpeed,float deltaTime);
参数current为初始值,参数target为目标值,参数currentVelocity为当前帧速度,ref类型,参数smoothTime为预计平滑时间,参数maxSpeed为当前帧最大速度值,默认值为Mathf.Infinity,参数deltaTime为平花时间,值越大返回值也相对越大,一般用Time.deltaTime计算。
功能说明 此方法的功能是模拟角度的平滑阻尼旋转,并返回模拟插值。
 
SmoothStep方法:平滑插值
public static float SmoothStep(float from,float to,float t);
参数from为起始值,参数to为结束值,参数t为插值系数。
功能说明 此方法的功能是返回一个从from到to的平滑插值。
1.参数from和to是两个恩义的float类型的数值,他们之间没有任何大小约束关系,from可以大于to也可以小于to。
2.插值系数t的有效范围为[0.0f,1.0f],当t < 0时其有效值t'为0.0f,当t > 1时其有效值t'为1.0f。
posted on 2016-03-01 01:44  Colve  阅读(2485)  评论(0编辑  收藏  举报