修复ArcadeRigidbody不触发OnEntityTransform事件

This commit is contained in:
yhh
2021-05-28 08:41:38 +08:00
parent 561a44b26b
commit 88f9779dd0
7 changed files with 55 additions and 52 deletions
+29 -25
View File
@@ -1704,6 +1704,10 @@ var es;
})(DirtyType = es.DirtyType || (es.DirtyType = {})); })(DirtyType = es.DirtyType || (es.DirtyType = {}));
var Transform = /** @class */ (function () { var Transform = /** @class */ (function () {
function Transform(entity) { function Transform(entity) {
/**
* 值会根据位置旋转和比例自动重新计算
*/
this._localTransform = es.Matrix2D.identity;
/** /**
* 值将自动从本地和父矩阵重新计算 * 值将自动从本地和父矩阵重新计算
*/ */
@@ -2090,8 +2094,8 @@ var es;
this._scaleMatrix = es.Matrix2D.createScale(this._localScale.x, this._localScale.y); this._scaleMatrix = es.Matrix2D.createScale(this._localScale.x, this._localScale.y);
this._localScaleDirty = false; this._localScaleDirty = false;
} }
this._localTransform = this._scaleMatrix.multiply(this._rotationMatrix); es.Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix, this._localTransform);
this._localTransform = this._localTransform.multiply(this._translationMatrix); es.Matrix2D.multiply(this._localTransform, this._translationMatrix, this._localTransform);
if (this.parent == null) { if (this.parent == null) {
this._worldTransform = this._localTransform; this._worldTransform = this._localTransform;
this._rotation = this._localRotation; this._rotation = this._localRotation;
@@ -2101,7 +2105,7 @@ var es;
this._localDirty = false; this._localDirty = false;
} }
if (this.parent != null) { if (this.parent != null) {
this._worldTransform = this._localTransform.multiply(this.parent._worldTransform); es.Matrix2D.multiply(this._localTransform, this.parent._worldTransform, this._worldTransform);
this._rotation = this._localRotation + this.parent._rotation; this._rotation = this._localRotation + this.parent._rotation;
this._scale = es.Vector2.multiply(this.parent._scale, this._localScale); this._scale = es.Vector2.multiply(this.parent._scale, this._localScale);
this._worldInverseDirty = true; this._worldInverseDirty = true;
@@ -2135,8 +2139,8 @@ var es;
* @param transform * @param transform
*/ */
Transform.prototype.copyFrom = function (transform) { Transform.prototype.copyFrom = function (transform) {
this._position = transform.position; this._position = transform.position.clone();
this._localPosition = transform._localPosition; this._localPosition = transform._localPosition.clone();
this._rotation = transform._rotation; this._rotation = transform._rotation;
this._localRotation = transform._localRotation; this._localRotation = transform._localRotation;
this._scale = transform._scale; this._scale = transform._scale;
@@ -2384,7 +2388,7 @@ var es;
*/ */
ArcadeRigidbody.prototype.addImpulse = function (force) { ArcadeRigidbody.prototype.addImpulse = function (force) {
if (!this.isImmovable) { if (!this.isImmovable) {
this.velocity.add(es.Vector2.multiplyScaler(force, 100000) this.velocity = es.Vector2.add(this.velocity, es.Vector2.multiplyScaler(force, 100000)
.multiplyScaler(this._inverseMass * es.Time.deltaTime * es.Time.deltaTime)); .multiplyScaler(this._inverseMass * es.Time.deltaTime * es.Time.deltaTime));
} }
}; };
@@ -2406,8 +2410,8 @@ var es;
return; return;
} }
if (this.shouldUseGravity) if (this.shouldUseGravity)
this.velocity.add(es.Vector2.multiplyScaler(es.Physics.gravity, es.Time.deltaTime)); this.velocity = es.Vector2.add(this.velocity, es.Vector2.multiplyScaler(es.Physics.gravity, es.Time.deltaTime));
this.entity.position = this.entity.position.add(es.Vector2.multiplyScaler(this.velocity, es.Time.deltaTime)); this.entity.position = es.Vector2.add(this.entity.position, es.Vector2.multiplyScaler(this.velocity, es.Time.deltaTime));
var collisionResult = new es.CollisionResult(); var collisionResult = new es.CollisionResult();
// 捞取我们在新的位置上可能会碰撞到的任何东西 // 捞取我们在新的位置上可能会碰撞到的任何东西
var neighbors = es.Physics.boxcastBroadphaseExcludingSelfNonRect(this._collider, this._collider.collidesWithLayers.value); var neighbors = es.Physics.boxcastBroadphaseExcludingSelfNonRect(this._collider, this._collider.collidesWithLayers.value);
@@ -2427,7 +2431,7 @@ var es;
} }
else { else {
// 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的 // 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的
this.entity.position = this.entity.position.subtract(collisionResult.minimumTranslationVector); this.entity.position = es.Vector2.subtract(this.entity.position, collisionResult.minimumTranslationVector);
var relativeVelocity = this.velocity.clone(); var relativeVelocity = this.velocity.clone();
this.calculateResponseVelocity(relativeVelocity, collisionResult.minimumTranslationVector, relativeVelocity); this.calculateResponseVelocity(relativeVelocity, collisionResult.minimumTranslationVector, relativeVelocity);
this.velocity.add(relativeVelocity); this.velocity.add(relativeVelocity);
@@ -2450,14 +2454,14 @@ var es;
*/ */
ArcadeRigidbody.prototype.processOverlap = function (other, minimumTranslationVector) { ArcadeRigidbody.prototype.processOverlap = function (other, minimumTranslationVector) {
if (this.isImmovable) { if (this.isImmovable) {
other.entity.position = other.entity.position.add(minimumTranslationVector); other.entity.position = es.Vector2.add(other.entity.position, minimumTranslationVector);
} }
else if (other.isImmovable) { else if (other.isImmovable) {
this.entity.position = this.entity.position.subtract(minimumTranslationVector); other.entity.position = es.Vector2.subtract(other.entity.position, minimumTranslationVector);
} }
else { else {
this.entity.position = this.entity.position.subtract(es.Vector2.multiplyScaler(minimumTranslationVector, 0.5)); this.entity.position = es.Vector2.subtract(this.entity.position, es.Vector2.multiplyScaler(minimumTranslationVector, 0.5));
other.entity.position = other.entity.position.add(es.Vector2.multiplyScaler(minimumTranslationVector, 0.5)); other.entity.position = es.Vector2.add(other.entity.position, es.Vector2.multiplyScaler(minimumTranslationVector, 0.5));
} }
}; };
/** /**
@@ -2475,8 +2479,8 @@ var es;
var totalinverseMass = this._inverseMass + other._inverseMass; var totalinverseMass = this._inverseMass + other._inverseMass;
var ourResponseFraction = this._inverseMass / totalinverseMass; var ourResponseFraction = this._inverseMass / totalinverseMass;
var otherResponseFraction = other._inverseMass / totalinverseMass; var otherResponseFraction = other._inverseMass / totalinverseMass;
this.velocity.add(es.Vector2.multiplyScaler(relativeVelocity, ourResponseFraction)); this.velocity = es.Vector2.add(this.velocity, es.Vector2.multiplyScaler(relativeVelocity, ourResponseFraction));
other.velocity.subtract(es.Vector2.multiplyScaler(relativeVelocity, otherResponseFraction)); other.velocity = es.Vector2.subtract(other.velocity, es.Vector2.multiplyScaler(relativeVelocity, otherResponseFraction));
}; };
/** /**
* 给定两个物体和MTV之间的相对速度本方法修改相对速度使其成为碰撞响应 * 给定两个物体和MTV之间的相对速度本方法修改相对速度使其成为碰撞响应
@@ -2769,10 +2773,10 @@ var es;
}); });
Object.defineProperty(Collider.prototype, "bounds", { Object.defineProperty(Collider.prototype, "bounds", {
get: function () { get: function () {
// if (this._isPositionDirty || this._isRotationDirty) { if (this._isPositionDirty || this._isRotationDirty) {
this.shape.recalculateBounds(this); this.shape.recalculateBounds(this);
// this._isPositionDirty = this._isRotationDirty = false; this._isPositionDirty = this._isRotationDirty = false;
// } }
return this.shape.bounds; return this.shape.bounds;
}, },
enumerable: true, enumerable: true,
@@ -2916,7 +2920,7 @@ var es;
if (didCollide) if (didCollide)
result.collider = collider; result.collider = collider;
// 将图形位置返回到检查前的位置 // 将图形位置返回到检查前的位置
this.entity.position = oldPosition; this.entity.position = oldPosition.clone();
return didCollide; return didCollide;
}; };
/** /**
@@ -2969,7 +2973,7 @@ var es;
finally { if (e_4) throw e_4.error; } finally { if (e_4) throw e_4.error; }
} }
// 将形状位置返回到检查之前的位置 // 将形状位置返回到检查之前的位置
this.shape.position = oldPosition; this.shape.position = oldPosition.clone();
return didCollide; return didCollide;
}; };
/** /**
@@ -7837,7 +7841,7 @@ var es;
*/ */
Physics.boxcastBroadphaseExcludingSelfNonRect = function (collider, layerMask) { Physics.boxcastBroadphaseExcludingSelfNonRect = function (collider, layerMask) {
if (layerMask === void 0) { layerMask = this.allLayers; } if (layerMask === void 0) { layerMask = this.allLayers; }
var bounds = collider.bounds.clone(); var bounds = collider.bounds;
return this._spatialHash.aabbBroadphase(bounds, collider, layerMask); return this._spatialHash.aabbBroadphase(bounds, collider, layerMask);
}; };
/** /**
@@ -7849,7 +7853,7 @@ var es;
*/ */
Physics.boxcastBroadphaseExcludingSelfDelta = function (collider, deltaX, deltaY, layerMask) { Physics.boxcastBroadphaseExcludingSelfDelta = function (collider, deltaX, deltaY, layerMask) {
if (layerMask === void 0) { layerMask = Physics.allLayers; } if (layerMask === void 0) { layerMask = Physics.allLayers; }
var colliderBounds = collider.bounds.clone(); var colliderBounds = collider.bounds;
var sweptBounds = colliderBounds.getSweptBroadphaseBounds(deltaX, deltaY); var sweptBounds = colliderBounds.getSweptBroadphaseBounds(deltaX, deltaY);
return this._spatialHash.aabbBroadphase(sweptBounds, collider, layerMask); return this._spatialHash.aabbBroadphase(sweptBounds, collider, layerMask);
}; };
@@ -8163,7 +8167,7 @@ var es;
SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) { SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) {
var e_8, _a; var e_8, _a;
this._overlapTestBox.updateBox(rect.width, rect.height); this._overlapTestBox.updateBox(rect.width, rect.height);
this._overlapTestBox.position = rect.location; this._overlapTestBox.position = rect.location.clone();
var resultCounter = 0; var resultCounter = 0;
var potentials = this.aabbBroadphase(rect, null, layerMask); var potentials = this.aabbBroadphase(rect, null, layerMask);
try { try {
@@ -8212,7 +8216,7 @@ var es;
var e_9, _a; var e_9, _a;
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius; this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter.clone();
var resultCounter = 0; var resultCounter = 0;
var potentials = this.aabbBroadphase(bounds, null, layerMask); var potentials = this.aabbBroadphase(bounds, null, layerMask);
try { try {
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -127,7 +127,7 @@ module es {
*/ */
public addImpulse(force: Vector2) { public addImpulse(force: Vector2) {
if (!this.isImmovable) { if (!this.isImmovable) {
this.velocity.add(Vector2.multiplyScaler(force, 100000) this.velocity = Vector2.add(this.velocity, Vector2.multiplyScaler(force, 100000)
.multiplyScaler(this._inverseMass * Time.deltaTime * Time.deltaTime)); .multiplyScaler(this._inverseMass * Time.deltaTime * Time.deltaTime));
} }
} }
@@ -151,9 +151,9 @@ module es {
} }
if (this.shouldUseGravity) if (this.shouldUseGravity)
this.velocity.add(Vector2.multiplyScaler(Physics.gravity, Time.deltaTime)); this.velocity = Vector2.add(this.velocity, Vector2.multiplyScaler(Physics.gravity, Time.deltaTime));
this.entity.position = this.entity.position.add(Vector2.multiplyScaler(this.velocity, Time.deltaTime)); this.entity.position = Vector2.add(this.entity.position, Vector2.multiplyScaler(this.velocity, Time.deltaTime));
let collisionResult = new CollisionResult(); let collisionResult = new CollisionResult();
// 捞取我们在新的位置上可能会碰撞到的任何东西 // 捞取我们在新的位置上可能会碰撞到的任何东西
@@ -172,7 +172,7 @@ module es {
this.processCollision(neighborRigidbody, collisionResult.minimumTranslationVector); this.processCollision(neighborRigidbody, collisionResult.minimumTranslationVector);
} else { } else {
// 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的 // 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的
this.entity.position = this.entity.position.subtract(collisionResult.minimumTranslationVector); this.entity.position = Vector2.subtract(this.entity.position, collisionResult.minimumTranslationVector);
let relativeVelocity = this.velocity.clone(); let relativeVelocity = this.velocity.clone();
this.calculateResponseVelocity(relativeVelocity, collisionResult.minimumTranslationVector, relativeVelocity); this.calculateResponseVelocity(relativeVelocity, collisionResult.minimumTranslationVector, relativeVelocity);
this.velocity.add(relativeVelocity); this.velocity.add(relativeVelocity);
@@ -188,12 +188,12 @@ module es {
*/ */
public processOverlap(other: ArcadeRigidbody, minimumTranslationVector: Vector2) { public processOverlap(other: ArcadeRigidbody, minimumTranslationVector: Vector2) {
if (this.isImmovable) { if (this.isImmovable) {
other.entity.position = other.entity.position.add(minimumTranslationVector); other.entity.position = Vector2.add(other.entity.position, minimumTranslationVector);
} else if (other.isImmovable) { } else if (other.isImmovable) {
this.entity.position = this.entity.position.subtract(minimumTranslationVector); other.entity.position = Vector2.subtract(other.entity.position, minimumTranslationVector);
} else { } else {
this.entity.position = this.entity.position.subtract(Vector2.multiplyScaler(minimumTranslationVector, 0.5)); this.entity.position = Vector2.subtract(this.entity.position, Vector2.multiplyScaler(minimumTranslationVector, 0.5));
other.entity.position = other.entity.position.add(Vector2.multiplyScaler(minimumTranslationVector, 0.5)); other.entity.position = Vector2.add(other.entity.position, Vector2.multiplyScaler(minimumTranslationVector, 0.5));
} }
} }
@@ -215,8 +215,8 @@ module es {
let ourResponseFraction = this._inverseMass / totalinverseMass; let ourResponseFraction = this._inverseMass / totalinverseMass;
let otherResponseFraction = other._inverseMass / totalinverseMass; let otherResponseFraction = other._inverseMass / totalinverseMass;
this.velocity.add(Vector2.multiplyScaler(relativeVelocity, ourResponseFraction)); this.velocity = Vector2.add(this.velocity, Vector2.multiplyScaler(relativeVelocity, ourResponseFraction));
other.velocity.subtract(Vector2.multiplyScaler(relativeVelocity, otherResponseFraction)); other.velocity = Vector2.subtract(other.velocity, Vector2.multiplyScaler(relativeVelocity, otherResponseFraction));
} }
/** /**
@@ -59,10 +59,10 @@ module es {
} }
public get bounds(): Rectangle { public get bounds(): Rectangle {
// if (this._isPositionDirty || this._isRotationDirty) { if (this._isPositionDirty || this._isRotationDirty) {
this.shape.recalculateBounds(this); this.shape.recalculateBounds(this);
// this._isPositionDirty = this._isRotationDirty = false; this._isPositionDirty = this._isRotationDirty = false;
// } }
return this.shape.bounds; return this.shape.bounds;
} }
@@ -221,7 +221,7 @@ module es {
result.collider = collider; result.collider = collider;
// 将图形位置返回到检查前的位置 // 将图形位置返回到检查前的位置
this.entity.position = oldPosition; this.entity.position = oldPosition.clone();
return didCollide; return didCollide;
} }
@@ -270,7 +270,7 @@ module es {
} }
// 将形状位置返回到检查之前的位置 // 将形状位置返回到检查之前的位置
this.shape.position = oldPosition; this.shape.position = oldPosition.clone();
return didCollide; return didCollide;
} }
+6 -7
View File
@@ -28,7 +28,7 @@ module es {
/** /**
* *
*/ */
public _localTransform: Matrix2D; public _localTransform: Matrix2D = Matrix2D.identity;
/** /**
* *
*/ */
@@ -426,8 +426,8 @@ module es {
this._localScaleDirty = false; this._localScaleDirty = false;
} }
this._localTransform = this._scaleMatrix.multiply(this._rotationMatrix); es.Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix, this._localTransform);
this._localTransform = this._localTransform.multiply(this._translationMatrix); es.Matrix2D.multiply(this._localTransform, this._translationMatrix, this._localTransform);
if (this.parent == null) { if (this.parent == null) {
this._worldTransform = this._localTransform; this._worldTransform = this._localTransform;
@@ -440,8 +440,7 @@ module es {
} }
if (this.parent != null) { if (this.parent != null) {
this._worldTransform = this._localTransform.multiply(this.parent._worldTransform); es.Matrix2D.multiply(this._localTransform, this.parent._worldTransform, this._worldTransform);
this._rotation = this._localRotation + this.parent._rotation; this._rotation = this._localRotation + this.parent._rotation;
this._scale = Vector2.multiply(this.parent._scale, this._localScale); this._scale = Vector2.multiply(this.parent._scale, this._localScale);
this._worldInverseDirty = true; this._worldInverseDirty = true;
@@ -480,8 +479,8 @@ module es {
* @param transform * @param transform
*/ */
public copyFrom(transform: Transform) { public copyFrom(transform: Transform) {
this._position = transform.position; this._position = transform.position.clone();
this._localPosition = transform._localPosition; this._localPosition = transform._localPosition.clone();
this._rotation = transform._rotation; this._rotation = transform._rotation;
this._localRotation = transform._localRotation; this._localRotation = transform._localRotation;
this._scale = transform._scale; this._scale = transform._scale;
+2 -2
View File
@@ -100,7 +100,7 @@ module es {
* @param layerMask * @param layerMask
*/ */
public static boxcastBroadphaseExcludingSelfNonRect(collider: Collider, layerMask = this.allLayers) { public static boxcastBroadphaseExcludingSelfNonRect(collider: Collider, layerMask = this.allLayers) {
let bounds = collider.bounds.clone(); let bounds = collider.bounds;
return this._spatialHash.aabbBroadphase(bounds, collider, layerMask); return this._spatialHash.aabbBroadphase(bounds, collider, layerMask);
} }
@@ -112,7 +112,7 @@ module es {
* @param layerMask * @param layerMask
*/ */
public static boxcastBroadphaseExcludingSelfDelta(collider: Collider, deltaX: number, deltaY: number, layerMask: number = Physics.allLayers) { public static boxcastBroadphaseExcludingSelfDelta(collider: Collider, deltaX: number, deltaY: number, layerMask: number = Physics.allLayers) {
let colliderBounds = collider.bounds.clone(); let colliderBounds = collider.bounds;
let sweptBounds = colliderBounds.getSweptBroadphaseBounds(deltaX, deltaY); let sweptBounds = colliderBounds.getSweptBroadphaseBounds(deltaX, deltaY);
return this._spatialHash.aabbBroadphase(sweptBounds, collider, layerMask); return this._spatialHash.aabbBroadphase(sweptBounds, collider, layerMask);
} }
+2 -2
View File
@@ -225,7 +225,7 @@ module es {
*/ */
public overlapRectangle(rect: Rectangle, results: Collider[], layerMask: number) { public overlapRectangle(rect: Rectangle, results: Collider[], layerMask: number) {
this._overlapTestBox.updateBox(rect.width, rect.height); this._overlapTestBox.updateBox(rect.width, rect.height);
this._overlapTestBox.position = rect.location; this._overlapTestBox.position = rect.location.clone();
let resultCounter = 0; let resultCounter = 0;
let potentials = this.aabbBroadphase(rect, null, layerMask); let potentials = this.aabbBroadphase(rect, null, layerMask);
@@ -265,7 +265,7 @@ module es {
let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius; this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter.clone();
let resultCounter = 0; let resultCounter = 0;
let potentials = this.aabbBroadphase(bounds, null, layerMask); let potentials = this.aabbBroadphase(bounds, null, layerMask);