新增角色控制器

This commit is contained in:
yhh
2021-06-22 14:41:21 +08:00
parent fe308d35f2
commit e5dfb20aa2
13 changed files with 2074 additions and 31 deletions

View File

@@ -129,6 +129,10 @@ module es {
return from + (to - from) * this.clamp01(t);
}
public static betterLerp(a: number, b: number, t: number, epsilon: number): number {
return Math.abs(a - b) < epsilon ? b : MathHelper.lerp(a, b, t);
}
/**
* 使度数的角度在a和b之间
* 用于处理360度环绕
@@ -604,5 +608,68 @@ module es {
return !Number.isFinite(x);
}
public static smoothDamp(current: number, target: number, currentVelocity: number, smoothTime: number, maxSpeed: number, deltaTime: number): { value: number; currentVelocity: number } {
smoothTime = Math.max(0.0001, smoothTime);
const num: number = 2 / smoothTime;
const num2: number = num * deltaTime;
const num3: number =
1 /
(1 + (num2 + (0.48 * (num2 * num2) + 0.235 * (num2 * (num2 * num2)))));
let num4: number = current - target;
const num5: number = target;
const num6: number = maxSpeed * smoothTime;
num4 = this.clamp(num4, num6 * -1, num6);
target = current - num4;
const num7: number = (currentVelocity + num * num4) * deltaTime;
currentVelocity = (currentVelocity - num * num7) * num3;
let num8: number = target + (num4 + num7) * num3;
if (num5 - current > 0 === num8 > num5) {
num8 = num5;
currentVelocity = (num8 - num5) / deltaTime;
}
return { value: num8, currentVelocity };
}
public static smoothDampVector(current: Vector2, target: Vector2, currentVelocity: Vector2, smoothTime: number, maxSpeed: number, deltaTime: number): Vector2 {
const v = Vector2.zero;
const resX = this.smoothDamp(
current.x,
target.x,
currentVelocity.x,
smoothTime,
maxSpeed,
deltaTime
);
v.x = resX.value;
currentVelocity.x = resX.currentVelocity;
const resY = this.smoothDamp(
current.y,
target.y,
currentVelocity.y,
smoothTime,
maxSpeed,
deltaTime
);
v.y = resY.value;
currentVelocity.y = resY.currentVelocity;
return v;
}
/**
* 将值(在 leftMin - leftMax 范围内)映射到 rightMin - rightMax 范围内的值
* @param value
* @param leftMin
* @param leftMax
* @param rightMin
* @param rightMax
* @returns
*/
public static mapMinMax(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax): number {
return rightMin + ((MathHelper.clamp(value, leftMin, leftMax) - leftMin) * (rightMax - rightMin)) / (leftMax - leftMin);
}
}
}

View File

@@ -30,6 +30,22 @@ module es {
return new Vector2(0, 1);
}
public static get up() {
return new Vector2(0, -1);
}
public static get down() {
return new Vector2(0, 1);
}
public static get left() {
return new Vector2(-1, 0);
}
public static get right() {
return new Vector2(1, 0);
}
/**
*
* @param value1
@@ -200,7 +216,7 @@ module es {
* @param from
* @param to
*/
public static angle(from: Vector2, to: Vector2): number{
public static angle(from: Vector2, to: Vector2): number {
from = Vector2.normalize(from);
to = Vector2.normalize(to);
return Math.acos(MathHelper.clamp(Vector2.dot(from, to), -1, 1)) * MathHelper.Rad2Deg;
@@ -348,10 +364,10 @@ module es {
* @returns 如果实例相同true 否则false
*/
public equals(other: Vector2 | object): boolean {
if (other instanceof Vector2){
if (other instanceof Vector2) {
return other.x == this.x && other.y == this.y;
}
return false;
}
@@ -390,11 +406,20 @@ module es {
* @param amount
* @returns
*/
public static hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number){
public static hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number) {
return new Vector2(MathHelper.hermite(value1.x, tangent1.x, value2.x, tangent2.x, amount),
MathHelper.hermite(value1.y, tangent1.y, value2.y, tangent2.y, amount));
}
public static unsignedAngle(from: Vector2, to: Vector2, round: boolean = true) {
from.normalize();
to.normalize();
const angle =
Math.acos(MathHelper.clamp(Vector2.dot(from, to), -1, 1)) * MathHelper.Rad2Deg;
return round ? Math.round(angle) : angle;
}
public clone(): Vector2 {
return new Vector2(this.x, this.y);
}