优化vector2构造函数

This commit is contained in:
yhh
2021-05-24 17:20:27 +08:00
parent 9c7703eb2d
commit da3ab02a8d
20 changed files with 177 additions and 109 deletions

View File

@@ -122,9 +122,10 @@ module es {
* @param type
*/
public static getGlobalManager<T extends es.GlobalManager>(type: new (...args) => T): T {
for (let i = 0; i < this._instance._globalManagers.length; i++) {
if (this._instance._globalManagers[i] instanceof type)
return this._instance._globalManagers[i] as T;
for (let i = 0, s = Core._instance._globalManagers.length; i < s; ++ i) {
let manager = Core._instance._globalManagers[i];
if (manager instanceof type)
return manager;
}
return null;
}

View File

@@ -54,7 +54,7 @@ module es {
/**
* 该刚体的速度
*/
public velocity: Vector2 = new Vector2();
public velocity: Vector2 = es.Vector2.zero;
/**
* 质量为0的刚体被认为是不可移动的。改变速度和碰撞对它们没有影响
@@ -213,7 +213,7 @@ module es {
* @param minimumTranslationVector
* @param responseVelocity
*/
public calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2, responseVelocity: Vector2 = new Vector2()) {
public calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2, responseVelocity: Vector2 = es.Vector2.zero) {
// 首先我们得到反方向的归一化MTV表面法线
let inverseMTV = Vector2.multiplyScaler(minimumTranslationVector, -1);
let normal = Vector2.normalize(inverseMTV);

View File

@@ -245,7 +245,7 @@ module es {
if (scale instanceof Vector2) {
this.transform.setScale(scale);
} else {
this.transform.setScale(new Vector2(scale));
this.transform.setScale(new Vector2(scale, scale));
}
return this;
@@ -257,7 +257,7 @@ module es {
if (scale instanceof Vector2) {
this.transform.setLocalScale(scale);
} else {
this.transform.setLocalScale(new Vector2(scale));
this.transform.setLocalScale(new Vector2(scale, scale));
}
return this;

View File

@@ -77,16 +77,16 @@ module es {
for (let i = 0, s = this._components.length; i < s; ++ i) {
this.handleRemove(this._components[i]);
}
this.componentsByType.clear();
this.componentsToAddByType.clear();
this._components.length = 0;
this._updatableComponents.length = 0;
this._componentsToAdd = {};
this._componentsToRemove = {};
this._componentsToAddList.length = 0;
this._componentsToRemoveList.length = 0;
}
this.componentsByType.clear();
this.componentsToAddByType.clear();
this._components.length = 0;
this._updatableComponents.length = 0;
this._componentsToAdd = {};
this._componentsToRemove = {};
this._componentsToAddList.length = 0;
this._componentsToRemoveList.length = 0;
}
public deregisterAllComponents() {

View File

@@ -96,16 +96,16 @@ module es {
private static recursiveGetOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
end: Vector2, points: Vector2[], distanceTolerance: number) {
// 计算线段的所有中点
let pt12 = Vector2.divide(Vector2.add(start, firstCtrlPoint), new Vector2(2));
let pt23 = Vector2.divide(Vector2.add(firstCtrlPoint, secondCtrlPoint), new Vector2(2));
let pt34 = Vector2.divide(Vector2.add(secondCtrlPoint, end), new Vector2(2));
let pt12 = Vector2.divideScaler(Vector2.add(start, firstCtrlPoint), 2);
let pt23 = Vector2.divideScaler(Vector2.add(firstCtrlPoint, secondCtrlPoint), 2);
let pt34 = Vector2.divideScaler(Vector2.add(secondCtrlPoint, end), 2);
// 计算新半直线的中点
let pt123 = Vector2.divide(Vector2.add(pt12, pt23), new Vector2(2));
let pt234 = Vector2.divide(Vector2.add(pt23, pt34), new Vector2(2));
let pt123 = Vector2.divideScaler(Vector2.add(pt12, pt23), 2);
let pt234 = Vector2.divideScaler(Vector2.add(pt23, pt34), 2);
// 最后再细分最后两个中点。如果我们满足我们的距离公差,这将是我们使用的最后一点。
let pt1234 = Vector2.divide(Vector2.add(pt123, pt234), new Vector2(2));
let pt1234 = Vector2.divideScaler(Vector2.add(pt123, pt234), 2);
// 试着用一条直线来近似整个三次曲线
let deltaLine = Vector2.subtract(end, start);

View File

@@ -228,6 +228,28 @@ module es {
return this;
}
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D, result: Matrix2D) {
let m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21);
let m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
let m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
let m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
let m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
let m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
result.m11 = m11;
result.m12 = m12;
result.m21 = m21;
result.m22 = m22;
result.m31 = m31;
result.m32 = m32;
return result;
}
public determinant() {
return this.m11 * this.m22 - this.m12 * this.m21;
}

View File

@@ -304,7 +304,7 @@ module es {
*/
public getClosestPointOnRectangleToPoint(point: Vector2) {
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
let res = new Vector2();
let res = es.Vector2.zero;
res.x = MathHelper.clamp(point.x, this.left, this.right);
res.y = MathHelper.clamp(point.y, this.top, this.bottom);
@@ -319,7 +319,7 @@ module es {
*/
public getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2 {
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
let res = new Vector2();
let res = es.Vector2.zero;
res.x = MathHelper.clamp(point.x, this.left, this.right);
res.y = MathHelper.clamp(point.y, this.top, this.bottom);

View File

@@ -9,9 +9,9 @@ module es {
* @param x 二维空间中的x坐标
* @param y 二维空间的y坐标
*/
constructor(x?: number, y?: number) {
this.x = x ? x : 0;
this.y = y != undefined ? y : this.x;
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
}
public static get zero() {
@@ -54,13 +54,20 @@ module es {
return result;
}
public static divideScaler(value1: Vector2, value2: number) {
let result: Vector2 = Vector2.zero;
result.x = value1.x / value2;
result.y = value1.y / value2;
return result;
}
/**
*
* @param value1
* @param value2
*/
public static multiply(value1: Vector2, value2: Vector2) {
let result: Vector2 = new Vector2(0, 0);
let result: Vector2 = es.Vector2.zero;
result.x = value1.x * value2.x;
result.y = value1.y * value2.y;
return result;
@@ -73,7 +80,7 @@ module es {
* @returns
*/
public static multiplyScaler(value1: Vector2, value2: number) {
let result = new Vector2(0, 0);
let result = es.Vector2.zero;
result.x = value1.x * value2;
result.y = value1.x * value2;
return result;
@@ -85,7 +92,7 @@ module es {
* @param value2
*/
public static subtract(value1: Vector2, value2: Vector2) {
let result: Vector2 = new Vector2(0, 0);
let result: Vector2 = es.Vector2.zero;
result.x = value1.x - value2.x;
result.y = value1.y - value2.y;
return result;
@@ -218,7 +225,7 @@ module es {
* @returns
*/
public static reflect(vector: Vector2, normal: Vector2) {
let result: Vector2 = new Vector2();
let result: Vector2 = es.Vector2.zero;
let val = 2 * ((vector.x * normal.x) + (vector.y * normal.y));
result.x = vector.x - (normal.x * val);
result.y = vector.y - (normal.y * val);
@@ -257,6 +264,12 @@ module es {
return this;
}
public divideScaler(value: number): Vector2 {
this.x /= value;
this.y /= value;
return this;
}
/**
*
* @param value

View File

@@ -33,7 +33,7 @@ module es {
return true;
}
public static lineToLineIntersection(a1: Vector2, a2: Vector2, b1: Vector2, b2: Vector2, intersection: Vector2 = new Vector2()): boolean {
public static lineToLineIntersection(a1: Vector2, a2: Vector2, b1: Vector2, b2: Vector2, intersection: Vector2 = es.Vector2.zero): boolean {
intersection.x = 0;
intersection.y = 0;

View File

@@ -11,7 +11,7 @@ module es {
return false;
// 求交点
let point = Vector2.add(ray.start, Vector2.multiply(ray.direction, new Vector2(time.value)));
let point = Vector2.add(ray.start, Vector2.multiplyScaler(ray.direction, time.value));
// 计算交点p位于b的哪个最小面和最大面之外。注意u和v不能有相同的位集它们之间必须至少有一个位集。
let u, v = 0;
@@ -49,7 +49,7 @@ module es {
* @param n
*/
public static corner(b: Rectangle, n: number){
let p = new Vector2();
let p = es.Vector2.zero;
p.x = (n & 1) == 0 ? b.right : b.left;
p.y = (n & 1) == 0 ? b.bottom : b.top;
return p;

View File

@@ -34,7 +34,7 @@ module es {
if (mtv.equals(Vector2.zero))
return false;
hit.normal = new Vector2(-mtv.x);
hit.normal = new Vector2(-mtv.x, -mtv.y);
hit.normal.normalize();
hit.distance = 0;
hit.fraction = 0;
@@ -42,14 +42,14 @@ module es {
return true;
} else {
// 射线投射移动矢量
let ray = new Ray2D(Vector2.zero, new Vector2(-movement.x));
let ray = new Ray2D(Vector2.zero, new Vector2(-movement.x, -movement.y));
let fraction = new Ref(0);
if (minkowskiDiff.rayIntersects(ray, fraction) && fraction.value <= 1) {
hit.fraction = fraction.value;
hit.distance = movement.length() * fraction.value;
hit.normal = new Vector2(-movement.x, -movement.y);
hit.normal.normalize();
hit.centroid = Vector2.add(first.bounds.center, Vector2.multiply(movement, new Vector2(fraction.value)));
hit.centroid = Vector2.add(first.bounds.center, Vector2.multiplyScaler(movement, fraction.value));
return true;
}

View File

@@ -7,8 +7,8 @@ module es {
if (collided) {
result.normal = Vector2.normalize(Vector2.subtract(first.position, second.position));
let depth = sumOfRadii - Math.sqrt(distanceSquared);
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth), result.normal);
result.point = Vector2.add(second.position, Vector2.multiply(result.normal, new Vector2(second.radius)));
result.minimumTranslationVector = Vector2.multiplyScaler(result.normal, -depth);
result.point = Vector2.add(second.position, Vector2.multiplyScaler(result.normal, second.radius));
// 这可以得到实际的碰撞点,可能有用也可能没用,所以我们暂时把它留在这里
// let collisionPointX = ((first.position.x * second.radius) + (second.position.x * first.radius)) / sumOfRadii;
@@ -35,7 +35,7 @@ module es {
result.point = closestPointOnBounds.clone();
// 计算MTV。找出安全的、非碰撞的位置并从中得到MTV
let safePlace = Vector2.add(closestPointOnBounds, Vector2.multiply(result.normal, new Vector2(circle.radius)));
let safePlace = Vector2.add(closestPointOnBounds, Vector2.multiplyScaler(result.normal, circle.radius));
result.minimumTranslationVector = Vector2.subtract(circle.position, safePlace);
return true;
@@ -45,14 +45,14 @@ module es {
// 看框上的点距圆的半径是否小于圆的半径
if (sqrDistance == 0) {
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(circle.radius));
result.minimumTranslationVector = Vector2.multiplyScaler(result.normal, circle.radius);
} else if (sqrDistance <= circle.radius * circle.radius) {
result.normal = Vector2.subtract(circle.position, closestPointOnBounds);
let depth = result.normal.length() - circle.radius;
result.point = closestPointOnBounds;
Vector2Ext.normalize(result.normal);
result.minimumTranslationVector = Vector2.multiply(new Vector2(depth), result.normal);
result.minimumTranslationVector = Vector2.multiplyScaler(result.normal, depth);
return true;
}
@@ -85,7 +85,7 @@ module es {
mtv = new Vector2(result.normal.x * circle.radius, result.normal.y * circle.radius);
} else {
let distance = Math.sqrt(distanceSquared.value);
mtv = Vector2.subtract(new Vector2(-1), Vector2.subtract(poly2Circle, closestPoint))
mtv = Vector2.multiplyScaler(Vector2.subtract(poly2Circle, closestPoint), -1)
.multiply(new Vector2((circle.radius - distance) / distance));
}
}
@@ -102,7 +102,7 @@ module es {
let t = Vector2.dot(w, v) / Vector2.dot(v, v);
t = MathHelper.clamp(t, 0, 1);
return Vector2.add(lineA, Vector2.multiply(v, new Vector2(t)));
return Vector2.add(lineA, Vector2.multiplyScaler(v, t));
}
}
}

View File

@@ -56,7 +56,7 @@ module es {
if (u < 0 || u > 1)
return false;
intersection = Vector2.add(a1, Vector2.multiply(new Vector2(t), b));
intersection = Vector2.add(a1, Vector2.multiplyScaler(b, t));
return true;
}
@@ -64,7 +64,7 @@ module es {
public static lineToCircle(start: Vector2, end: Vector2, s: Circle, hit: RaycastHit): boolean{
// 计算这里的长度并分别对d进行标准化因为如果我们命中了我们需要它来得到分数
let lineLength = Vector2.distance(start, end);
let d = Vector2.divide(Vector2.subtract(end, start), new Vector2(lineLength));
let d = Vector2.divideScaler(Vector2.subtract(end, start), lineLength);
let m = Vector2.subtract(start, s.position);
let b = Vector2.dot(m, d);
let c = Vector2.dot(m, m) - s.radius * s.radius;
@@ -85,7 +85,7 @@ module es {
if (hit.fraction < 0)
hit.fraction = 0;
hit.point = Vector2.add(start, Vector2.multiply(new Vector2(hit.fraction), d));
hit.point = Vector2.add(start, Vector2.multiplyScaler(d, hit.fraction));
hit.distance = Vector2.distance(start, hit.point);
hit.normal = Vector2.normalize(Vector2.subtract(hit.point, s.position));
hit.fraction = hit.distance / lineLength;

View File

@@ -7,8 +7,8 @@ module es {
if (collided) {
result.normal = Vector2.normalize(Vector2.subtract(point, circle.position));
let depth = sumOfRadii - Math.sqrt(distanceSquared);
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth, -depth), result.normal);
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
result.minimumTranslationVector = Vector2.multiplyScaler(result.normal, -depth);
result.point = Vector2.add(circle.position, Vector2.multiplyScaler(result.normal, circle.radius));
return true;
}

View File

@@ -12,7 +12,7 @@ module es {
let firstEdges = first.edgeNormals.slice();
let secondEdges = second.edgeNormals.slice();
let minIntervalDistance = Number.POSITIVE_INFINITY;
let translationAxis = new Vector2();
let translationAxis = es.Vector2.zero;
let polygonOffset = Vector2.subtract(first.position, second.position);
let axis: Vector2;

View File

@@ -250,7 +250,7 @@ module es {
*/
public static getClosestPointOnRectangleToPoint(rect: Rectangle, point: Vector2) {
// 对于每个轴,如果该点在盒子外面,则将在盒子上,否则不理会它
let res = new Vector2();
let res = es.Vector2.zero;
res.x = MathHelper.clamp(point.x, rect.left, rect.right)
res.y = MathHelper.clamp(point.y, rect.top, rect.bottom);
@@ -264,7 +264,7 @@ module es {
*/
public static getClosestPointOnRectangleBorderToPoint(rect: Rectangle, point: Vector2) {
// 对于每个轴,如果该点在盒子外面,则将在盒子上,否则不理会它
let res = new Vector2();
let res = es.Vector2.zero;
res.x = MathHelper.clamp(point.x, rect.left, rect.right)
res.y = MathHelper.clamp(point.y, rect.top, rect.bottom);

View File

@@ -71,7 +71,7 @@ module es {
* @param d
* @param intersection
*/
public static getRayIntersection(a: Vector2, b: Vector2, c: Vector2, d: Vector2, intersection: Vector2 = new Vector2()) {
public static getRayIntersection(a: Vector2, b: Vector2, c: Vector2, d: Vector2, intersection: Vector2 = es.Vector2.zero) {
let dy1 = b.y - a.y;
let dx1 = b.x - a.x;
let dy2 = d.y - c.y;
@@ -99,7 +99,7 @@ module es {
public static normalize(vec: Vector2) {
let magnitude = Math.sqrt((vec.x * vec.x) + (vec.y * vec.y));
if (magnitude > MathHelper.Epsilon) {
vec.divide(new Vector2(magnitude));
vec.divideScaler(magnitude);
} else {
vec.x = vec.y = 0;
}
@@ -131,7 +131,7 @@ module es {
* @param matrix
* @param result
*/
public static transformR(position: Vector2, matrix: Matrix2D, result: Vector2 = new Vector2()) {
public static transformR(position: Vector2, matrix: Matrix2D, result: Vector2 = es.Vector2.zero) {
let x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
let y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
result.x = x;