框架优化

This commit is contained in:
yhh
2021-07-02 10:11:09 +08:00
parent ea482dab48
commit 3d9c8699e7
31 changed files with 1050 additions and 1105 deletions

View File

@@ -127,8 +127,7 @@ module es {
*/
public addImpulse(force: Vector2) {
if (!this.isImmovable) {
this.velocity = Vector2.add(this.velocity, Vector2.multiplyScaler(force, 100000)
.multiplyScaler(this._inverseMass * Time.deltaTime * Time.deltaTime));
this.velocity.addEqual(force.scale(100000 * (this._inverseMass * (Time.deltaTime * Time.deltaTime))));
}
}
@@ -152,7 +151,7 @@ module es {
if (this.shouldUseGravity)
this.velocity.addEqual(Physics.gravity.scale(Time.deltaTime));
this.entity.position = Vector2.add(this.entity.position, Vector2.multiplyScaler(this.velocity, Time.deltaTime));
this.entity.position = this.entity.position.add(this.velocity.scale(Time.deltaTime));
let collisionResult = new CollisionResult();
@@ -175,7 +174,7 @@ module es {
this.processCollision(neighborRigidbody, collisionResult.minimumTranslationVector);
} else {
// 没有ArcadeRigidbody所以我们假设它是不动的只移动我们自己的
this.entity.position = Vector2.subtract(this.entity.position, collisionResult.minimumTranslationVector);
this.entity.position = this.entity.position.sub(collisionResult.minimumTranslationVector);
const relativeVelocity = this.calculateResponseVelocity(this.velocity, collisionResult.minimumTranslationVector);
this.velocity.addEqual(relativeVelocity);
}
@@ -208,7 +207,7 @@ module es {
// 我们计算两个相撞物体的响应。
// 计算的基础是沿碰撞表面法线反射的物体的相对速度。
// 然后,响应的一部分会根据质量加到每个物体上
let relativeVelocity = Vector2.subtract(this.velocity, other.velocity);
let relativeVelocity = this.velocity.sub(other.velocity);
relativeVelocity = this.calculateResponseVelocity(relativeVelocity, minimumTranslationVector);
@@ -218,8 +217,8 @@ module es {
const otherResponseFraction = other._inverseMass / totalinverseMass;
this.velocity = Vector2.add(this.velocity, relativeVelocity.scale(ourResponseFraction));
other.velocity = Vector2.subtract(other.velocity, relativeVelocity.scale(otherResponseFraction));
this.velocity = this.velocity.add(relativeVelocity.scale(ourResponseFraction));
other.velocity = other.velocity.sub(relativeVelocity.scale(otherResponseFraction));
}
/**
@@ -230,14 +229,14 @@ module es {
*/
public calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2) {
// 首先我们得到反方向的归一化MTV表面法线
let inverseMTV = Vector2.multiplyScaler(minimumTranslationVector, -1);
let normal = Vector2.normalize(inverseMTV);
let inverseMTV = minimumTranslationVector.scale(-1);
let normal = inverseMTV.normalize();
// 速度是沿碰撞法线和碰撞平面分解的。
// 弹性将影响沿法线的响应(法线速度分量),摩擦力将影响速度的切向分量(切向速度分量)
let n = Vector2.dot(relativeVelocity, normal);
let n = relativeVelocity.dot(normal);
let normalVelocityComponent = Vector2.multiplyScaler(normal, n);
let tangentialVelocityComponent = Vector2.subtract(relativeVelocity, normalVelocityComponent);
let normalVelocityComponent = normal.scale(n);
let tangentialVelocityComponent = relativeVelocity.sub(normalVelocityComponent);
if (n > 0)
normalVelocityComponent = Vector2.zero;

View File

@@ -301,16 +301,16 @@ module es {
this.supportSlopedOneWayPlatforms &&
this.collisionState.wasGroundedLastFrame
) {
this._raycastHit = Physics.linecastIgnoreCollider(
this._raycastHit = Physics.linecast(
ray,
ray.add(rayDirection.multiplyScaler(rayDistance)),
ray.add(rayDirection.scaleEqual(rayDistance)),
this.platformMask,
this.ignoredColliders
);
} else {
this._raycastHit = Physics.linecastIgnoreCollider(
this._raycastHit = Physics.linecast(
ray,
ray.add(rayDirection.multiplyScaler(rayDistance)),
ray.add(rayDirection.scaleEqual(rayDistance)),
this.platformMask & ~this.oneWayPlatformMask,
this.ignoredColliders
);
@@ -380,9 +380,9 @@ module es {
initialRayOriginX + i * this._horizontalDistanceBetweenRays,
initialRayOriginY
);
this._raycastHit = Physics.linecastIgnoreCollider(
this._raycastHit = Physics.linecast(
rayStart,
rayStart.add(rayDirection.multiplyScaler(rayDistance)),
rayStart.add(rayDirection.scaleEqual(rayDistance)),
mask,
this.ignoredColliders
);
@@ -439,9 +439,9 @@ module es {
this._raycastOrigins.bottomLeft.y
);
this._raycastHit = Physics.linecastIgnoreCollider(
this._raycastHit = Physics.linecast(
slopeRay,
slopeRay.add(rayDirection.multiplyScaler(slopeCheckRayDistance)),
slopeRay.add(rayDirection.scaleEqual(slopeCheckRayDistance)),
this.platformMask,
this.ignoredColliders
);
@@ -502,14 +502,14 @@ module es {
this.supportSlopedOneWayPlatforms &&
this.collisionState.wasGroundedLastFrame
) {
raycastHit = Physics.linecastIgnoreCollider(
raycastHit = Physics.linecast(
ray,
ray.add(deltaMovement),
this.platformMask,
this.ignoredColliders
);
} else {
raycastHit = Physics.linecastIgnoreCollider(
raycastHit = Physics.linecast(
ray,
ray.add(deltaMovement),
this.platformMask & ~this.oneWayPlatformMask,

View File

@@ -1,5 +1,7 @@
module es {
export abstract class Collider extends Component {
public static readonly lateSortOrder = 999;
public castSortOrder: number = 0;
/**
* 对撞机的基本形状
*/
@@ -132,12 +134,12 @@ module es {
if (this instanceof CircleCollider) {
this.radius = Math.max(width, height) * 0.5;
this.localOffset = Vector2.subtract(renderableBounds.center, this.entity.transform.position);
this.localOffset = renderableBounds.center.sub(this.entity.transform.position);
} else if (this instanceof BoxCollider) {
this.width = width;
this.height = height;
this.localOffset = Vector2.subtract(renderableBounds.center, this.entity.transform.position);
this.localOffset = renderableBounds.center.sub(this.entity.transform.position);
}
}
}
@@ -264,8 +266,8 @@ module es {
continue;
if (this.collidesWithNonMotion(neighbor, result)) {
motion = Vector2.subtract(motion, result.minimumTranslationVector);
this.shape.position = Vector2.subtract(this.shape.position, result.minimumTranslationVector);
motion = motion.sub(result.minimumTranslationVector);
this.shape.position = this.shape.position.sub(result.minimumTranslationVector);
didCollide = true;
}
}

View File

@@ -347,9 +347,9 @@ module es {
* @param pos
*/
public lookAt(pos: Vector2) {
let sign = this.position.x > pos.x ? -1 : 1;
let vectorToAlignTo = Vector2.normalize(Vector2.subtract(this.position, pos));
this.rotation = sign * Math.acos(Vector2.dot(vectorToAlignTo, Vector2.unitY));
const sign = this.position.x > pos.x ? -1 : 1;
const vectorToAlignTo = this.position.sub(pos).normalize();
this.rotation = sign * Math.acos(vectorToAlignTo.dot(Vector2.unitY));
}
/**
@@ -412,22 +412,22 @@ module es {
if (this._localDirty) {
if (this._localPositionDirty) {
this._translationMatrix = Matrix2D.createTranslation(this._localPosition.x, this._localPosition.y);
Matrix2D.createTranslation(this._localPosition.x, this._localPosition.y, this._translationMatrix);
this._localPositionDirty = false;
}
if (this._localRotationDirty) {
this._rotationMatrix = Matrix2D.createRotation(this._localRotation);
Matrix2D.createRotation(this._localRotation, this._rotationMatrix);
this._localRotationDirty = false;
}
if (this._localScaleDirty) {
this._scaleMatrix = Matrix2D.createScale(this._localScale.x, this._localScale.y);
Matrix2D.createScale(this._localScale.x, this._localScale.y, this._scaleMatrix);
this._localScaleDirty = false;
}
es.Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix, this._localTransform);
es.Matrix2D.multiply(this._localTransform, this._translationMatrix, this._localTransform);
Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix, this._localTransform);
Matrix2D.multiply(this._localTransform, this._translationMatrix, this._localTransform);
if (this.parent == null) {
this._worldTransform = this._localTransform;
@@ -440,9 +440,9 @@ module es {
}
if (this.parent != null) {
es.Matrix2D.multiply(this._localTransform, this.parent._worldTransform, this._worldTransform);
Matrix2D.multiply(this._localTransform, this.parent._worldTransform, this._worldTransform);
this._rotation = this._localRotation + this.parent._rotation;
this._scale = Vector2.multiply(this.parent._scale, this._localScale);
this._scale = this.parent._scale.multiply(this._localScale);;
this._worldInverseDirty = true;
}
@@ -457,13 +457,13 @@ module es {
this.hierarchyDirty |= dirtyFlagType;
switch (dirtyFlagType) {
case es.DirtyType.positionDirty:
case DirtyType.positionDirty:
this.entity.onTransformChanged(transform.Component.position);
break;
case es.DirtyType.rotationDirty:
case DirtyType.rotationDirty:
this.entity.onTransformChanged(transform.Component.rotation);
break;
case es.DirtyType.scaleDirty:
case DirtyType.scaleDirty:
this.entity.onTransformChanged(transform.Component.scale);
break;
}