修复因Vector2.add/substract/divide/mutiply导致的计算错误
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
///<reference path="./Collider.ts" />
|
||||
module es {
|
||||
export class BoxCollider extends Collider {
|
||||
/**
|
||||
* 零参数构造函数要求RenderableComponent在实体上,这样碰撞器可以在实体被添加到场景时调整自身的大小。
|
||||
*/
|
||||
constructor(x: number, y: number, width: number, height: number) {
|
||||
super();
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ module es {
|
||||
/**
|
||||
* 如果这个碰撞器是一个触发器,它将不会引起碰撞,但它仍然会触发事件
|
||||
*/
|
||||
public isTrigger: boolean;
|
||||
public isTrigger: boolean = false;
|
||||
/**
|
||||
* 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法
|
||||
*/
|
||||
@@ -26,6 +26,7 @@ module es {
|
||||
* 存储这个允许我们始终能够安全地从物理系统中移除对撞机,即使它在试图移除它之前已经被移动了。
|
||||
*/
|
||||
public registeredPhysicsBounds: Rectangle = new Rectangle();
|
||||
|
||||
public _localOffsetLength: number;
|
||||
public _isPositionDirty: boolean = true;
|
||||
public _isRotationDirty: boolean = true;
|
||||
@@ -183,8 +184,8 @@ module es {
|
||||
*/
|
||||
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean {
|
||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||
let oldPosition = this.entity.position;
|
||||
this.entity.position.add(motion);
|
||||
let oldPosition = this.entity.position.clone();
|
||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||
|
||||
let didCollide = this.shape.collidesWithShape(collider.shape, result);
|
||||
if (didCollide)
|
||||
|
||||
@@ -19,7 +19,7 @@ module es {
|
||||
* @param collisionResult
|
||||
*/
|
||||
public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean {
|
||||
if (!this.entity.getComponent(Collider) || !this._triggerHelper) {
|
||||
if (this.entity.getComponent(Collider) == null || this._triggerHelper == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ module es {
|
||||
continue;
|
||||
|
||||
// 获取我们在新位置可能发生碰撞的任何东西
|
||||
let bounds = collider.bounds;
|
||||
let bounds = collider.bounds.clone();
|
||||
bounds.x += motion.x;
|
||||
bounds.y += motion.y;
|
||||
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers.value);
|
||||
@@ -47,7 +47,7 @@ module es {
|
||||
let _internalcollisionResult: CollisionResult = new CollisionResult();
|
||||
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
||||
// 如果碰撞 则退回之前的移动量
|
||||
motion = motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
||||
motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
||||
|
||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||
if (_internalcollisionResult.collider != null) {
|
||||
|
||||
@@ -24,7 +24,7 @@ module es {
|
||||
let didCollide = false;
|
||||
|
||||
// 获取我们在新位置可能发生碰撞的任何东西
|
||||
this.entity.position.add(motion);
|
||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||
|
||||
// 获取任何可能在新位置发生碰撞的东西
|
||||
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers.value);
|
||||
|
||||
@@ -28,20 +28,19 @@ module es {
|
||||
/**
|
||||
* 值会根据位置、旋转和比例自动重新计算
|
||||
*/
|
||||
public _localTransform: Matrix2D = Matrix2D.identity;
|
||||
public _localTransform: Matrix2D;
|
||||
/**
|
||||
* 值将自动从本地和父矩阵重新计算。
|
||||
*/
|
||||
public _worldTransform = Matrix2D.identity;
|
||||
public _rotationMatrix: Matrix2D = Matrix2D.identity;
|
||||
public _translationMatrix: Matrix2D = Matrix2D.identity;
|
||||
public _scaleMatrix: Matrix2D = Matrix2D.identity;
|
||||
public _children: Transform[];
|
||||
public _scaleMatrix: Matrix2D;
|
||||
public _children: Transform[] = [];
|
||||
|
||||
constructor(entity: Entity) {
|
||||
this.entity = entity;
|
||||
this.scale = this._localScale = Vector2.one;
|
||||
this._children = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,7 +139,7 @@ module es {
|
||||
public get position(): Vector2 {
|
||||
this.updateTransform();
|
||||
if (this._positionDirty) {
|
||||
if (!this.parent) {
|
||||
if (this.parent == null) {
|
||||
this._position = this._localPosition;
|
||||
} else {
|
||||
this.parent.updateTransform();
|
||||
@@ -290,7 +289,7 @@ module es {
|
||||
return this;
|
||||
|
||||
this._position = position;
|
||||
if (this.parent) {
|
||||
if (this.parent != null) {
|
||||
this.localPosition = Vector2.transform(this._position, this._worldToLocalTransform);
|
||||
} else {
|
||||
this.localPosition = position;
|
||||
@@ -404,7 +403,7 @@ module es {
|
||||
|
||||
public updateTransform() {
|
||||
if (this.hierarchyDirty != DirtyType.clean) {
|
||||
if (this.parent)
|
||||
if (this.parent != null)
|
||||
this.parent.updateTransform();
|
||||
|
||||
if (this._localDirty) {
|
||||
@@ -426,7 +425,7 @@ module es {
|
||||
this._localTransform = this._scaleMatrix.multiply(this._rotationMatrix);
|
||||
this._localTransform = this._localTransform.multiply(this._translationMatrix);
|
||||
|
||||
if (!this.parent) {
|
||||
if (this.parent == null) {
|
||||
this._worldTransform = this._localTransform;
|
||||
this._rotation = this._localRotation;
|
||||
this._scale = this._localScale;
|
||||
@@ -436,7 +435,7 @@ module es {
|
||||
this._localDirty = false;
|
||||
}
|
||||
|
||||
if (this.parent) {
|
||||
if (this.parent != null) {
|
||||
this._worldTransform = this._localTransform.multiply(this.parent._worldTransform);
|
||||
|
||||
this._rotation = this._localRotation + this.parent._rotation;
|
||||
@@ -466,9 +465,6 @@ module es {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!this._children)
|
||||
this._children = [];
|
||||
|
||||
// 告诉子项发生了变换
|
||||
for (let i = 0; i < this._children.length; i++)
|
||||
this._children[i].setDirty(dirtyFlagType);
|
||||
|
||||
@@ -9,7 +9,7 @@ module es {
|
||||
private _bits: number[];
|
||||
|
||||
constructor(nbits: number = 64) {
|
||||
let length = nbits >> 6 >>> 0;
|
||||
let length = nbits >> 6;
|
||||
if ((nbits & BitSet.LONG_MASK) != 0)
|
||||
length++;
|
||||
|
||||
@@ -34,7 +34,7 @@ module es {
|
||||
}
|
||||
|
||||
public cardinality(): number {
|
||||
let card = 0 >>> 0;
|
||||
let card = 0;
|
||||
for (let i = this._bits.length - 1; i >= 0; i--) {
|
||||
let a = this._bits[i];
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
module es {
|
||||
export class Rectangle implements IEquatable<Rectangle> {
|
||||
static emptyRectangle: Rectangle = new Rectangle();
|
||||
|
||||
/**
|
||||
* 该矩形的左上角的x坐标
|
||||
*/
|
||||
@@ -26,7 +24,7 @@ module es {
|
||||
* 返回X=0, Y=0, Width=0, Height=0的矩形
|
||||
*/
|
||||
public static get empty(): Rectangle {
|
||||
return this.emptyRectangle;
|
||||
return new Rectangle();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,8 +318,6 @@ module es {
|
||||
* @returns 矩形边框上离点最近的点
|
||||
*/
|
||||
public getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2 {
|
||||
edgeNormal = Vector2.zero;
|
||||
|
||||
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
|
||||
let res = new Vector2();
|
||||
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
||||
@@ -405,8 +401,8 @@ module es {
|
||||
* @param value2
|
||||
*/
|
||||
public static overlap(value1: Rectangle, value2: Rectangle): Rectangle {
|
||||
let x = Math.max(Math.max(value1.x, value2.x), 0);
|
||||
let y = Math.max(Math.max(value1.y, value2.y), 0);
|
||||
let x = Math.max(value1.x, value2.x, 0);
|
||||
let y = Math.max(value1.y, value2.y, 0);
|
||||
return new Rectangle(x, y,
|
||||
Math.max(Math.min(value1.right, value2.right) - x, 0),
|
||||
Math.max(Math.min(value1.bottom, value2.bottom) - y, 0));
|
||||
@@ -555,5 +551,9 @@ module es {
|
||||
public getHashCode(): number{
|
||||
return (this.x ^ this.y ^ this.width ^ this.height);
|
||||
}
|
||||
|
||||
public clone(): Rectangle {
|
||||
return new Rectangle(this.x, this.y, this.width, this.height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ module es {
|
||||
* @param value2
|
||||
*/
|
||||
public static add(value1: Vector2, value2: Vector2) {
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
let result: Vector2 = Vector2.zero;
|
||||
result.x = value1.x + value2.x;
|
||||
result.y = value1.y + value2.y;
|
||||
return result;
|
||||
@@ -48,7 +48,7 @@ module es {
|
||||
* @param value2
|
||||
*/
|
||||
public static divide(value1: Vector2, value2: Vector2) {
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
let result: Vector2 = Vector2.zero;
|
||||
result.x = value1.x / value2.x;
|
||||
result.y = value1.y / value2.y;
|
||||
return result;
|
||||
@@ -258,5 +258,9 @@ module es {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public clone(): Vector2 {
|
||||
return new Vector2(this.x, this.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
module es {
|
||||
export class Vector3 {
|
||||
public x: number;
|
||||
public y: number;
|
||||
public z: number;
|
||||
|
||||
constructor(x: number, y: number, z: number) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,7 +116,7 @@ module es {
|
||||
public static recenterPolygonVerts(points: Vector2[]) {
|
||||
let center = this.findPolygonCenter(points);
|
||||
for (let i = 0; i < points.length; i++)
|
||||
points[i].subtract(center);
|
||||
points[i] = Vector2.subtract(points[i], center);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,7 +212,7 @@ module es {
|
||||
|
||||
public recalculateBounds(collider: Collider) {
|
||||
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
||||
this.center = collider.localOffset;
|
||||
this.center = collider.localOffset.clone();
|
||||
|
||||
if (collider.shouldColliderScaleAndRotateWithTransform) {
|
||||
let hasUnitScale = true;
|
||||
@@ -234,7 +234,7 @@ module es {
|
||||
|
||||
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动
|
||||
// 我们的偏移使角度为0我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
|
||||
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
|
||||
let offsetAngle = Math.atan2(collider.localOffset.y * collider.entity.transform.scale.y, collider.localOffset.x * collider.entity.transform.scale.x) * MathHelper.Rad2Deg;
|
||||
let offsetLength = hasUnitScale ? collider._localOffsetLength :
|
||||
Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
|
||||
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength,
|
||||
@@ -256,7 +256,7 @@ module es {
|
||||
|
||||
this.position = Vector2.add(collider.entity.transform.position, this.center);
|
||||
this.bounds = Rectangle.rectEncompassingPoints(this.points);
|
||||
this.bounds.location.add(this.position);
|
||||
this.bounds.location = Vector2.add(this.bounds.location, this.position);
|
||||
}
|
||||
|
||||
public overlaps(other: Shape) {
|
||||
|
||||
@@ -369,7 +369,7 @@ module es {
|
||||
if (u < 0 || u > 1)
|
||||
return false;
|
||||
|
||||
intersection = intersection.add(a1).add(Vector2.multiply(new Vector2(t), b));
|
||||
intersection = Vector2.add(a1, Vector2.multiply(new Vector2(t), b));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ module es {
|
||||
* @param collider
|
||||
*/
|
||||
public register(collider: Collider) {
|
||||
let bounds = collider.bounds;
|
||||
let bounds = collider.bounds.clone();
|
||||
collider.registeredPhysicsBounds = bounds;
|
||||
let p1 = this.cellCoords(bounds.x, bounds.y);
|
||||
let p2 = this.cellCoords(bounds.right, bounds.bottom);
|
||||
@@ -63,7 +63,7 @@ module es {
|
||||
* @param collider
|
||||
*/
|
||||
public remove(collider: Collider) {
|
||||
let bounds = collider.registeredPhysicsBounds;
|
||||
let bounds = collider.registeredPhysicsBounds.clone();
|
||||
let p1 = this.cellCoords(bounds.x, bounds.y);
|
||||
let p2 = this.cellCoords(bounds.right, bounds.bottom);
|
||||
|
||||
@@ -106,7 +106,7 @@ module es {
|
||||
for (let x = p1.x; x <= p2.x; x++) {
|
||||
for (let y = p1.y; y <= p2.y; y++) {
|
||||
let cell = this.cellAtPosition(x, y);
|
||||
if (!cell)
|
||||
if (cell == null)
|
||||
continue;
|
||||
|
||||
// 当cell不为空。循环并取回所有碰撞器
|
||||
@@ -350,7 +350,7 @@ module es {
|
||||
// TODO: rayIntersects的性能够吗?需要测试它。Collisions.rectToLine可能更快
|
||||
// TODO: 如果边界检查返回更多数据,我们就不需要为BoxCollider检查做任何事情
|
||||
// 在做形状测试之前先做一个边界检查
|
||||
let colliderBounds = potential.bounds;
|
||||
let colliderBounds = potential.bounds.clone();
|
||||
if (colliderBounds.rayIntersects(this._ray, fraction) && fraction.value <= 1){
|
||||
if (potential.shape.collidesWithLine(this._ray.start, this._ray.end, this._tempHit)) {
|
||||
// 检查一下,我们应该排除这些射线,射线cast是否在碰撞器中开始
|
||||
|
||||
@@ -29,7 +29,7 @@ module es {
|
||||
result.x = Math.min(first.x, rect.x);
|
||||
result.y = Math.min(first.y, rect.y);
|
||||
result.width = Math.max(first.right, rect.right) - result.x;
|
||||
result.height = Math.max(first.bottom, result.bottom) - result.y;
|
||||
result.height = Math.max(first.bottom, rect.bottom) - result.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user