框架优化

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;
}
}