修复normalized
This commit is contained in:
@@ -54,7 +54,7 @@ module es {
|
||||
/**
|
||||
* 该刚体的速度
|
||||
*/
|
||||
public velocity: Vector2 = es.Vector2.zero;
|
||||
public velocity: Vector2 = Vector2.zero;
|
||||
|
||||
/**
|
||||
* 质量为0的刚体被认为是不可移动的。改变速度和碰撞对它们没有影响
|
||||
@@ -151,14 +151,17 @@ module es {
|
||||
}
|
||||
|
||||
if (this.shouldUseGravity)
|
||||
this.velocity = Vector2.add(this.velocity, Vector2.multiplyScaler(Physics.gravity, Time.deltaTime));
|
||||
|
||||
this.velocity.addEqual(Physics.gravity.scale(Time.deltaTime));
|
||||
this.entity.position = Vector2.add(this.entity.position, Vector2.multiplyScaler(this.velocity, Time.deltaTime));
|
||||
|
||||
let collisionResult = new CollisionResult();
|
||||
|
||||
// 捞取我们在新的位置上可能会碰撞到的任何东西
|
||||
let neighbors = Physics.boxcastBroadphaseExcludingSelfNonRect(this._collider, this._collider.collidesWithLayers.value);
|
||||
for (let neighbor of neighbors) {
|
||||
let neighbors = Physics.boxcastBroadphaseExcludingSelf(this._collider, this._collider.bounds, this._collider.collidesWithLayers.value);
|
||||
for (const neighbor of neighbors) {
|
||||
if (!neighbor)
|
||||
continue;
|
||||
|
||||
// 如果邻近的对撞机是同一个实体,则忽略它
|
||||
if (neighbor.entity.equals(this.entity)) {
|
||||
continue;
|
||||
@@ -173,9 +176,8 @@ module es {
|
||||
} else {
|
||||
// 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的
|
||||
this.entity.position = Vector2.subtract(this.entity.position, collisionResult.minimumTranslationVector);
|
||||
let relativeVelocity = this.velocity.clone();
|
||||
this.calculateResponseVelocity(relativeVelocity, collisionResult.minimumTranslationVector, relativeVelocity);
|
||||
this.velocity.add(relativeVelocity);
|
||||
const relativeVelocity = this.calculateResponseVelocity(this.velocity, collisionResult.minimumTranslationVector);
|
||||
this.velocity.addEqual(relativeVelocity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -188,12 +190,12 @@ module es {
|
||||
*/
|
||||
public processOverlap(other: ArcadeRigidbody, minimumTranslationVector: Vector2) {
|
||||
if (this.isImmovable) {
|
||||
other.entity.position = Vector2.add(other.entity.position, minimumTranslationVector);
|
||||
other.entity.position = other.entity.position.add(minimumTranslationVector);
|
||||
} else if (other.isImmovable) {
|
||||
other.entity.position = Vector2.subtract(other.entity.position, minimumTranslationVector);
|
||||
this.entity.position = this.entity.position.sub(minimumTranslationVector);
|
||||
} else {
|
||||
this.entity.position = Vector2.subtract(this.entity.position, Vector2.multiplyScaler(minimumTranslationVector, 0.5));
|
||||
other.entity.position = Vector2.add(other.entity.position, Vector2.multiplyScaler(minimumTranslationVector, 0.5));
|
||||
this.entity.position = this.entity.position.sub(minimumTranslationVector.scale(0.5));
|
||||
other.entity.position = other.entity.position.add(minimumTranslationVector.scale(0.5));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,15 +210,16 @@ module es {
|
||||
// 然后,响应的一部分会根据质量加到每个物体上
|
||||
let relativeVelocity = Vector2.subtract(this.velocity, other.velocity);
|
||||
|
||||
this.calculateResponseVelocity(relativeVelocity, minimumTranslationVector, relativeVelocity);
|
||||
relativeVelocity = this.calculateResponseVelocity(relativeVelocity, minimumTranslationVector);
|
||||
|
||||
// 现在,我们使用质量来线性缩放两个刚体上的响应
|
||||
let totalinverseMass = this._inverseMass + other._inverseMass;
|
||||
let ourResponseFraction = this._inverseMass / totalinverseMass;
|
||||
let otherResponseFraction = other._inverseMass / totalinverseMass;
|
||||
const totalinverseMass = this._inverseMass + other._inverseMass;
|
||||
const ourResponseFraction = this._inverseMass / totalinverseMass;
|
||||
const otherResponseFraction = other._inverseMass / totalinverseMass;
|
||||
|
||||
this.velocity = Vector2.add(this.velocity, Vector2.multiplyScaler(relativeVelocity, ourResponseFraction));
|
||||
other.velocity = Vector2.subtract(other.velocity, Vector2.multiplyScaler(relativeVelocity, otherResponseFraction));
|
||||
|
||||
this.velocity = Vector2.add(this.velocity, relativeVelocity.scale(ourResponseFraction));
|
||||
other.velocity = Vector2.subtract(other.velocity, relativeVelocity.scale(otherResponseFraction));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,11 +228,10 @@ module es {
|
||||
* @param minimumTranslationVector
|
||||
* @param responseVelocity
|
||||
*/
|
||||
public calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2, responseVelocity: Vector2 = es.Vector2.zero) {
|
||||
public calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2) {
|
||||
// 首先,我们得到反方向的归一化MTV:表面法线
|
||||
let inverseMTV = Vector2.multiplyScaler(minimumTranslationVector, -1);
|
||||
let normal = Vector2.normalize(inverseMTV);
|
||||
|
||||
// 速度是沿碰撞法线和碰撞平面分解的。
|
||||
// 弹性将影响沿法线的响应(法线速度分量),摩擦力将影响速度的切向分量(切向速度分量)
|
||||
let n = Vector2.dot(relativeVelocity, normal);
|
||||
@@ -246,10 +248,10 @@ module es {
|
||||
coefficientOfFriction = 1.01;
|
||||
|
||||
// 弹性影响速度的法向分量,摩擦力影响速度的切向分量
|
||||
let r = Vector2.multiplyScaler(normalVelocityComponent, -(1 + this._elasticity))
|
||||
.subtract(Vector2.multiplyScaler(tangentialVelocityComponent, coefficientOfFriction));
|
||||
responseVelocity.x = r.x;
|
||||
responseVelocity.y = r.y;
|
||||
return normalVelocityComponent
|
||||
.scale(1 + this._elasticity)
|
||||
.sub(tangentialVelocityComponent.scale(coefficientOfFriction))
|
||||
.scale(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -294,9 +294,8 @@ module es {
|
||||
initialRayOriginY - i * this._verticalDistanceBetweenRays
|
||||
);
|
||||
|
||||
// if we are grounded we will include oneWayPlatforms
|
||||
// only on the first ray (the bottom one). this will allow us to
|
||||
// walk up sloped oneWayPlatforms
|
||||
// 如果我们接地,我们将只在第一条射线(底部)上包含 oneWayPlatforms。
|
||||
// 允许我们走上倾斜的 oneWayPlatforms
|
||||
if (
|
||||
i === 0 &&
|
||||
this.supportSlopedOneWayPlatforms &&
|
||||
|
||||
@@ -94,8 +94,8 @@ module es {
|
||||
public setLocalOffset(offset: Vector2): Collider {
|
||||
if (!this._localOffset.equals(offset)) {
|
||||
this.unregisterColliderWithPhysicsSystem();
|
||||
this._localOffset = offset;
|
||||
this._localOffsetLength = this._localOffset.length();
|
||||
this._localOffset.setTo(offset.x, offset.y);
|
||||
this._localOffsetLength = this._localOffset.magnitude();
|
||||
this._isPositionDirty = true;
|
||||
this.registerColliderWithPhysicsSystem();
|
||||
}
|
||||
@@ -213,15 +213,15 @@ module es {
|
||||
*/
|
||||
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult = new CollisionResult()): boolean {
|
||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||
let oldPosition = this.entity.position.clone();
|
||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||
const oldPosition = this.entity.position;
|
||||
this.entity.position = this.entity.position.add(motion);
|
||||
|
||||
let didCollide = this.shape.collidesWithShape(collider.shape, result);
|
||||
const didCollide = this.shape.collidesWithShape(collider.shape, result);
|
||||
if (didCollide)
|
||||
result.collider = collider;
|
||||
|
||||
// 将图形位置返回到检查前的位置
|
||||
this.entity.position = oldPosition.clone();
|
||||
this.entity.position = oldPosition;
|
||||
|
||||
return didCollide;
|
||||
}
|
||||
@@ -237,6 +237,7 @@ module es {
|
||||
return true;
|
||||
}
|
||||
|
||||
result.collider = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,9 @@ module es {
|
||||
// 第一点和最后一点决不能相同。我们想要一个开放的多边形
|
||||
let isPolygonClosed = points[0] == points[points.length - 1];
|
||||
|
||||
let linqPoints = new es.List(points);
|
||||
// 最后一个移除
|
||||
if (isPolygonClosed)
|
||||
linqPoints.remove(linqPoints.last());
|
||||
points = points.slice(0, points.length - 1);
|
||||
|
||||
let center = Polygon.findPolygonCenter(points);
|
||||
this.setLocalOffset(center);
|
||||
|
||||
@@ -60,7 +60,7 @@ module es {
|
||||
let _internalcollisionResult: CollisionResult = new CollisionResult();
|
||||
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
||||
// 如果碰撞 则退回之前的移动量
|
||||
motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
||||
motion.sub(_internalcollisionResult.minimumTranslationVector);
|
||||
|
||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||
if (_internalcollisionResult.collider != null) {
|
||||
|
||||
Reference in New Issue
Block a user