Files
esengine/source/src/Tween/Easing/Lerps.ts

72 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-07-03 12:27:21 +08:00
module es {
/**
* tween类型结构unclamped lerps.unclamped lerps对于超过0-1bounceelastic或其他tweens是必需的
*/
export class Lerps {
public static lerp(from: number, to: number, t: number);
public static lerp(from: Rectangle, to: Rectangle, t: number);
public static lerp(from: Vector2, to: Vector2, t: number);
public static lerp(from: any, to: any, t: number) {
if (typeof(from) == "number" && typeof(to) == "number") {
return from + (to - from) * t;
}
if (from instanceof Rectangle && to instanceof Rectangle) {
return new Rectangle(
(from.x + (to.x - from.x) * t),
(from.y + (to.x - from.y) * t),
(from.width + (to.width - from.width) * t),
(from.height + (to.height - from.height) * t)
);
}
if (from instanceof Vector2 && to instanceof Vector2) {
return new Vector2(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t);
}
}
public static angleLerp(from: Vector2, to: Vector2, t: number) {
// 我们计算这个lerp的最短角差
let toMinusFrom = new Vector2(MathHelper.deltaAngle(from.x, to.x), MathHelper.deltaAngle(from.y, to.y));
return new Vector2(from.x + toMinusFrom.x * t, from.y + toMinusFrom.y * t);
}
public static ease(easeType: EaseType, from: Rectangle, to: Rectangle, t: number, duration: number);
public static ease(easeType: EaseType, from: Vector2, to: Vector2, t: number, duration: number);
public static ease(easeType: EaseType, from: number, to: number, t: number, duration: number);
public static ease(easeType: EaseType, from: any, to: any, t: number, duration: number) {
if (typeof(from) == 'number' && typeof(to) == "number") {
return this.lerp(from, to, EaseHelper.ease(easeType, t, duration));
}
if (from instanceof Vector2 && to instanceof Vector2) {
return this.lerp(from, to, EaseHelper.ease(easeType, t, duration));
}
if (from instanceof Rectangle && to instanceof Rectangle) {
return this.lerp(from, to, EaseHelper.ease(easeType, t, duration));
}
}
public static easeAngle(easeType: EaseType, from: Vector2, to: Vector2, t: number, duration: number) {
return this.angleLerp(from, to, EaseHelper.ease(easeType, t, duration));
}
/**
* 使
* http://allenchou.net/2015/04/game-math-more-on-numeric-springing/
* @param currentValue
* @param targetValue
* @param velocity Velocity的引用targetValue0
* @param dampingRatio 0.01-1
* @param angularFrequency 2pi(/)1Hz.35
*/
public static fastSpring(currentValue: Vector2, targetValue: Vector2, velocity: Vector2,
dampingRatio: number, angularFrequency: number) {
velocity.add(velocity.scale(-2 * Time.deltaTime * dampingRatio * angularFrequency)
.add(targetValue.sub(currentValue).scale(Time.deltaTime * angularFrequency * angularFrequency)));
currentValue.add(velocity.scale(Time.deltaTime));
return currentValue;
}
}
}