修复因Vector2.add/substract/divide/mutiply导致的计算错误

This commit is contained in:
yhh
2020-12-03 17:58:25 +08:00
parent 89cfd5388f
commit 4665d1d0ea
16 changed files with 83 additions and 122 deletions

View File

@@ -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();

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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);

View File

@@ -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];