完善mathHelper类

This commit is contained in:
yhh
2021-04-30 15:10:11 +08:00
parent 0748652a8d
commit b3c85e00f9
4 changed files with 524 additions and 1 deletions

View File

@@ -2343,6 +2343,37 @@ declare module es {
*/
static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number;
static lerp(from: number, to: number, t: number): number;
/**
* 使度数的角度在a和b之间
* 用于处理360度环绕
* @param a
* @param b
* @param t
* @returns
*/
static lerpAngle(a: number, b: number, t: number): number;
/**
* 使弧度的角度在a和b之间
* @param a
* @param b
* @param t
* @returns
*/
static lerpAngleRadians(a: number, b: number, t: number): number;
/**
* 循环t使其不大于长度且不小于0
* @param t
* @param length
* @returns
*/
static pingPong(t: number, length: number): number;
/**
* 如果value> = threshold返回其符号否则返回0
* @param value
* @param threshold
* @returns
*/
static signThreshold(value: number, threshold: number): number;
static inverseLerp(from: number, to: number, t: number): number;
static clamp(value: number, min: number, max: number): number;
static snap(value: number, increment: number): number;
@@ -2390,18 +2421,107 @@ declare module es {
* @param shift
*/
static approach(start: number, end: number, shift: number): number;
/**
* 通过偏移量钳位结果并选择最短路径,将起始角度向终止角度移动,起始值可以小于或大于终止值。
* 示例1开始是30结束是100移位是25结果为55
* 示例2开始是340结束是30移位是25结果是5365换为5
* @param start
* @param end
* @param shift
* @returns
*/
static approachAngle(start: number, end: number, shift: number): number;
/**
* 通过将偏移量(全部以弧度为单位)夹住结果并选择最短路径,起始角度朝向终止角度。
* 起始值可以小于或大于终止值。
* 此方法的工作方式与“角度”方法非常相似唯一的区别是使用弧度代替度并以2 * Pi代替360。
* @param start
* @param end
* @param shift
* @returns
*/
static approachAngleRadians(start: number, end: number, shift: number): number;
/**
* 使用可接受的检查公差检查两个值是否大致相同
* @param value1
* @param value2
* @param tolerance
* @returns
*/
static approximately(value1: number, value2: number, tolerance?: number): boolean;
/**
* 计算两个给定角之间的最短差值(度数)
* @param current
* @param target
*/
static deltaAngle(current: number, target: number): number;
/**
* 计算以弧度为单位的两个给定角度之间的最短差
* @param current
* @param target
* @returns
*/
static deltaAngleRadians(current: number, target: number): number;
/**
* 循环t使其永远不大于长度永远不小于0
* @param t
* @param length
*/
static repeat(t: number, length: number): number;
/**
* 将值绕一圈移动的助手
* @param position
* @param speed
* @returns
*/
static rotateAround(position: Vector2, speed: number): Vector2;
/**
* 旋转是相对于当前位置而不是总旋转。
* 例如如果您当前处于90度并且想要旋转到135度则可以使用45度而不是135度的角度
* @param point
* @param center
* @param angleIndegrees
*/
static rotateAround2(point: Vector2, center: Vector2, angleIndegrees: number): Vector2;
/**
* 根据圆的中心,半径和角度在圆的圆周上得到一个点。 0度是3点钟方向
* @param circleCenter
* @param radius
* @param angleInDegrees
*/
static pointOnCircle(circleCenter: Vector2, radius: number, angleInDegrees: number): Vector2;
/**
* 根据圆的中心,半径和角度在圆的圆周上得到一个点。 0弧度是3点钟方向
* @param circleCenter
* @param radius
* @param angleInRadians
* @returns
*/
static pointOnCircleRadians(circleCenter: Vector2, radius: number, angleInRadians: number): Vector2;
/**
* lissajou曲线
* @param xFrequency
* @param yFrequency
* @param xMagnitude
* @param yMagnitude
* @param phase
* @returns
*/
static lissajou(xFrequency?: number, yFrequency?: number, xMagnitude?: number, yMagnitude?: number, phase?: number): Vector2;
/**
* lissajou曲线的阻尼形式其振荡随时间在0和最大幅度之间。
* 为获得最佳效果阻尼应在0到1之间。
* 振荡间隔是动画循环的一半完成的时间(以秒为单位)。
* @param xFrequency
* @param yFrequency
* @param xMagnitude
* @param yMagnitude
* @param phase
* @param damping
* @param oscillationInterval
* @returns
*/
static lissajouDamped(xFrequency?: number, yFrequency?: number, xMagnitude?: number, yMagnitude?: number, phase?: number, damping?: number, oscillationInterval?: number): Vector2;
}
}
declare module es {

View File

@@ -5744,6 +5744,55 @@ var es;
MathHelper.lerp = function (from, to, t) {
return from + (to - from) * this.clamp01(t);
};
/**
* 使度数的角度在a和b之间
* 用于处理360度环绕
* @param a
* @param b
* @param t
* @returns
*/
MathHelper.lerpAngle = function (a, b, t) {
var num = this.repeat(b - a, 360);
if (num > 180)
num -= 360;
return a + num * this.clamp01(t);
};
/**
* 使弧度的角度在a和b之间
* @param a
* @param b
* @param t
* @returns
*/
MathHelper.lerpAngleRadians = function (a, b, t) {
var num = this.repeat(b - a, Math.PI * 2);
if (num > Math.PI)
num -= Math.PI * 2;
return a + num * this.clamp01(t);
};
/**
* 循环t使其不大于长度且不小于0
* @param t
* @param length
* @returns
*/
MathHelper.pingPong = function (t, length) {
t = this.repeat(t, length * 2);
return length - Math.abs(t - length);
};
/**
* 如果value> = threshold返回其符号否则返回0
* @param value
* @param threshold
* @returns
*/
MathHelper.signThreshold = function (value, threshold) {
if (Math.abs(value) >= threshold)
return Math.sign(value);
else
return 0;
};
MathHelper.inverseLerp = function (from, to, t) {
if (from < to) {
if (t < from)
@@ -5842,6 +5891,47 @@ var es;
return Math.min(start + shift, end);
return Math.max(start - shift, end);
};
/**
* 通过偏移量钳位结果并选择最短路径,将起始角度向终止角度移动,起始值可以小于或大于终止值。
* 示例1开始是30结束是100移位是25结果为55
* 示例2开始是340结束是30移位是25结果是5365换为5
* @param start
* @param end
* @param shift
* @returns
*/
MathHelper.approachAngle = function (start, end, shift) {
var deltaAngle = this.deltaAngle(start, end);
if (-shift < deltaAngle && deltaAngle < shift)
return end;
return this.repeat(this.approach(start, start + deltaAngle, shift), 360);
};
/**
* 通过将偏移量(全部以弧度为单位)夹住结果并选择最短路径,起始角度朝向终止角度。
* 起始值可以小于或大于终止值。
* 此方法的工作方式与“角度”方法非常相似唯一的区别是使用弧度代替度并以2 * Pi代替360。
* @param start
* @param end
* @param shift
* @returns
*/
MathHelper.approachAngleRadians = function (start, end, shift) {
var deltaAngleRadians = this.deltaAngleRadians(start, end);
if (-shift < deltaAngleRadians && deltaAngleRadians < shift)
return end;
return this.repeat(this.approach(start, start + deltaAngleRadians, shift), Math.PI * 2);
};
/**
* 使用可接受的检查公差检查两个值是否大致相同
* @param value1
* @param value2
* @param tolerance
* @returns
*/
MathHelper.approximately = function (value1, value2, tolerance) {
if (tolerance === void 0) { tolerance = this.Epsilon; }
return Math.abs(value1 - value2) <= tolerance;
};
/**
* 计算两个给定角之间的最短差值(度数)
* @param current
@@ -5853,6 +5943,18 @@ var es;
num -= 360;
return num;
};
/**
* 计算以弧度为单位的两个给定角度之间的最短差
* @param current
* @param target
* @returns
*/
MathHelper.deltaAngleRadians = function (current, target) {
var num = this.repeat(target - current, 2 * Math.PI);
if (num > Math.PI)
num -= 2 * Math.PI;
return num;
};
/**
* 循环t使其永远不大于长度永远不小于0
* @param t
@@ -5861,6 +5963,99 @@ var es;
MathHelper.repeat = function (t, length) {
return t - Math.floor(t / length) * length;
};
/**
* 将值绕一圈移动的助手
* @param position
* @param speed
* @returns
*/
MathHelper.rotateAround = function (position, speed) {
var time = es.Time.totalTime * speed;
var x = Math.cos(time);
var y = Math.sin(time);
return new es.Vector2(position.x + x, position.y + y);
};
/**
* 旋转是相对于当前位置而不是总旋转。
* 例如如果您当前处于90度并且想要旋转到135度则可以使用45度而不是135度的角度
* @param point
* @param center
* @param angleIndegrees
*/
MathHelper.rotateAround2 = function (point, center, angleIndegrees) {
angleIndegrees = this.toRadians(angleIndegrees);
var cos = Math.cos(angleIndegrees);
var sin = Math.sin(angleIndegrees);
var rotatedX = cos * (point.x - center.x) - sin * (point.y - center.y) + center.x;
var rotatedY = sin * (point.x - center.x) + cos * (point.y - center.y) + center.y;
return new es.Vector2(rotatedX, rotatedY);
};
/**
* 根据圆的中心,半径和角度在圆的圆周上得到一个点。 0度是3点钟方向
* @param circleCenter
* @param radius
* @param angleInDegrees
*/
MathHelper.pointOnCircle = function (circleCenter, radius, angleInDegrees) {
var radians = this.toRadians(angleInDegrees);
return new es.Vector2(Math.cos(radians) * radius + circleCenter.x, Math.sin(radians) * radius + circleCenter.y);
};
/**
* 根据圆的中心,半径和角度在圆的圆周上得到一个点。 0弧度是3点钟方向
* @param circleCenter
* @param radius
* @param angleInRadians
* @returns
*/
MathHelper.pointOnCircleRadians = function (circleCenter, radius, angleInRadians) {
return new es.Vector2(Math.cos(angleInRadians) * radius + circleCenter.x, Math.sin(angleInRadians) * radius + circleCenter.y);
};
/**
* lissajou曲线
* @param xFrequency
* @param yFrequency
* @param xMagnitude
* @param yMagnitude
* @param phase
* @returns
*/
MathHelper.lissajou = function (xFrequency, yFrequency, xMagnitude, yMagnitude, phase) {
if (xFrequency === void 0) { xFrequency = 2; }
if (yFrequency === void 0) { yFrequency = 3; }
if (xMagnitude === void 0) { xMagnitude = 1; }
if (yMagnitude === void 0) { yMagnitude = 1; }
if (phase === void 0) { phase = 0; }
var x = Math.sin(es.Time.totalTime * xFrequency + phase) * xMagnitude;
var y = Math.cos(es.Time.totalTime * yFrequency) * yMagnitude;
return new es.Vector2(x, y);
};
/**
* lissajou曲线的阻尼形式其振荡随时间在0和最大幅度之间。
* 为获得最佳效果阻尼应在0到1之间。
* 振荡间隔是动画循环的一半完成的时间(以秒为单位)。
* @param xFrequency
* @param yFrequency
* @param xMagnitude
* @param yMagnitude
* @param phase
* @param damping
* @param oscillationInterval
* @returns
*/
MathHelper.lissajouDamped = function (xFrequency, yFrequency, xMagnitude, yMagnitude, phase, damping, oscillationInterval) {
if (xFrequency === void 0) { xFrequency = 2; }
if (yFrequency === void 0) { yFrequency = 3; }
if (xMagnitude === void 0) { xMagnitude = 1; }
if (yMagnitude === void 0) { yMagnitude = 1; }
if (phase === void 0) { phase = 0.5; }
if (damping === void 0) { damping = 0; }
if (oscillationInterval === void 0) { oscillationInterval = 5; }
var wrappedTime = this.pingPong(es.Time.totalTime, oscillationInterval);
var damped = Math.pow(Math.E, -damping * wrappedTime);
var x = damped * Math.sin(es.Time.totalTime * xFrequency + phase) * xMagnitude;
var y = damped * Math.cos(es.Time.totalTime * yFrequency) * yMagnitude;
return new es.Vector2(x, y);
};
MathHelper.Epsilon = 0.00001;
MathHelper.Rad2Deg = 57.29578;
MathHelper.Deg2Rad = 0.0174532924;

File diff suppressed because one or more lines are too long

View File

@@ -40,6 +40,61 @@ module es {
return from + (to - from) * this.clamp01(t);
}
/**
* 使度数的角度在a和b之间
* 用于处理360度环绕
* @param a
* @param b
* @param t
* @returns
*/
public static lerpAngle(a: number, b: number, t: number) {
let num = this.repeat(b - a, 360);
if (num > 180)
num -= 360;
return a + num * this.clamp01(t);
}
/**
* 使弧度的角度在a和b之间
* @param a
* @param b
* @param t
* @returns
*/
public static lerpAngleRadians(a: number, b: number, t: number) {
let num = this.repeat(b - a, Math.PI * 2);
if (num > Math.PI)
num -= Math.PI * 2;
return a + num * this.clamp01(t);
}
/**
* 循环t使其不大于长度且不小于0
* @param t
* @param length
* @returns
*/
public static pingPong(t: number, length: number) {
t = this.repeat(t, length * 2);
return length - Math.abs(t - length);
}
/**
* 如果value> = threshold返回其符号否则返回0
* @param value
* @param threshold
* @returns
*/
public static signThreshold(value: number, threshold: number) {
if (Math.abs(value) >= threshold)
return Math.sign(value);
else
return 0;
}
public static inverseLerp(from: number, to: number, t: number) {
if (from < to) {
if (t < from)
@@ -156,6 +211,51 @@ module es {
return Math.max(start - shift, end);
}
/**
* 通过偏移量钳位结果并选择最短路径,将起始角度向终止角度移动,起始值可以小于或大于终止值。
* 示例1开始是30结束是100移位是25结果为55
* 示例2开始是340结束是30移位是25结果是5365换为5
* @param start
* @param end
* @param shift
* @returns
*/
public static approachAngle(start: number, end: number, shift: number) {
let deltaAngle = this.deltaAngle(start, end);
if (-shift < deltaAngle && deltaAngle < shift)
return end;
return this.repeat(this.approach(start, start + deltaAngle, shift), 360);
}
/**
* 通过将偏移量(全部以弧度为单位)夹住结果并选择最短路径,起始角度朝向终止角度。
* 起始值可以小于或大于终止值。
* 此方法的工作方式与“角度”方法非常相似唯一的区别是使用弧度代替度并以2 * Pi代替360。
* @param start
* @param end
* @param shift
* @returns
*/
public static approachAngleRadians(start: number, end: number, shift: number) {
let deltaAngleRadians = this.deltaAngleRadians(start, end);
if (-shift < deltaAngleRadians && deltaAngleRadians < shift)
return end;
return this.repeat(this.approach(start, start + deltaAngleRadians, shift), Math.PI * 2);
}
/**
* 使用可接受的检查公差检查两个值是否大致相同
* @param value1
* @param value2
* @param tolerance
* @returns
*/
public static approximately(value1: number, value2: number, tolerance: number = this.Epsilon) {
return Math.abs(value1 - value2) <= tolerance;
}
/**
* 计算两个给定角之间的最短差值(度数)
* @param current
@@ -169,6 +269,20 @@ module es {
return num;
}
/**
* 计算以弧度为单位的两个给定角度之间的最短差
* @param current
* @param target
* @returns
*/
public static deltaAngleRadians(current: number, target: number) {
let num = this.repeat(target - current, 2 * Math.PI);
if (num > Math.PI)
num -= 2 * Math.PI;
return num;
}
/**
* 循环t使其永远不大于长度永远不小于0
* @param t
@@ -178,5 +292,99 @@ module es {
return t - Math.floor(t / length) * length;
}
/**
* 将值绕一圈移动的助手
* @param position
* @param speed
* @returns
*/
public static rotateAround(position: Vector2, speed: number) {
let time = Time.totalTime * speed;
let x = Math.cos(time);
let y = Math.sin(time);
return new Vector2(position.x + x, position.y + y);
}
/**
* 旋转是相对于当前位置而不是总旋转。
* 例如如果您当前处于90度并且想要旋转到135度则可以使用45度而不是135度的角度
* @param point
* @param center
* @param angleIndegrees
*/
public static rotateAround2(point: Vector2, center: Vector2, angleIndegrees: number) {
angleIndegrees = this.toRadians(angleIndegrees);
let cos = Math.cos(angleIndegrees);
let sin = Math.sin(angleIndegrees);
let rotatedX = cos * (point.x - center.x) - sin * (point.y - center.y) + center.x;
let rotatedY = sin * (point.x - center.x) + cos * (point.y - center.y) + center.y;
return new Vector2(rotatedX, rotatedY);
}
/**
* 根据圆的中心,半径和角度在圆的圆周上得到一个点。 0度是3点钟方向
* @param circleCenter
* @param radius
* @param angleInDegrees
*/
public static pointOnCircle(circleCenter: Vector2, radius: number, angleInDegrees: number) {
let radians = this.toRadians(angleInDegrees);
return new Vector2(Math.cos(radians) * radius + circleCenter.x, Math.sin(radians) * radius + circleCenter.y);
}
/**
* 根据圆的中心,半径和角度在圆的圆周上得到一个点。 0弧度是3点钟方向
* @param circleCenter
* @param radius
* @param angleInRadians
* @returns
*/
public static pointOnCircleRadians(circleCenter: Vector2, radius: number, angleInRadians: number) {
return new Vector2(Math.cos(angleInRadians) * radius + circleCenter.x, Math.sin(angleInRadians) * radius + circleCenter.y);
}
/**
* lissajou曲线
* @param xFrequency
* @param yFrequency
* @param xMagnitude
* @param yMagnitude
* @param phase
* @returns
*/
public static lissajou(xFrequency: number = 2, yFrequency: number = 3, xMagnitude: number = 1, yMagnitude: number = 1, phase: number = 0) {
let x = Math.sin(Time.totalTime * xFrequency + phase) * xMagnitude;
let y = Math.cos(Time.totalTime * yFrequency) * yMagnitude;
return new Vector2(x, y);
}
/**
* lissajou曲线的阻尼形式其振荡随时间在0和最大幅度之间。
* 为获得最佳效果阻尼应在0到1之间。
* 振荡间隔是动画循环的一半完成的时间(以秒为单位)。
* @param xFrequency
* @param yFrequency
* @param xMagnitude
* @param yMagnitude
* @param phase
* @param damping
* @param oscillationInterval
* @returns
*/
public static lissajouDamped(xFrequency: number = 2, yFrequency: number = 3, xMagnitude: number = 1,
yMagnitude: number = 1, phase: number = 0.5, damping: number = 0,
oscillationInterval: number = 5) {
let wrappedTime = this.pingPong(Time.totalTime, oscillationInterval);
let damped = Math.pow(Math.E, -damping * wrappedTime);
let x = damped * Math.sin(Time.totalTime * xFrequency + phase) * xMagnitude;
let y = damped * Math.cos(Time.totalTime * yFrequency) * yMagnitude;
return new Vector2(x, y);
}
}
}