新增mover移动器组件 用于处理itriggerListener接口碰撞信息

This commit is contained in:
yhh
2020-06-16 11:22:37 +08:00
parent 75301f7776
commit 8b21edc65f
22 changed files with 671 additions and 46 deletions

View File

@@ -14,7 +14,7 @@ class ColliderTriggerHelper {
* 实体被移动后应该调用更新。它会处理碰撞器重叠的任何itriggerlistener。
*/
public update() {
let colliders = this._entity.getComponents("Collider");
let colliders = this._entity.getComponents(Collider);
for (let i = 0; i < colliders.length; i++) {
let collider = colliders[i];

View File

@@ -11,6 +11,10 @@ class Physics {
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
}
public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers){
return this._spatialHash.aabbBroadphase(rect, collider, layerMask);
}
public static addCollider(collider: Collider){
Physics._spatialHash.register(collider);
}

View File

@@ -1,4 +1,5 @@
class CollisionResult {
public collider: Collider;
public minimumTranslationVector: Vector2;
public normal: Vector2;
public point: Vector2;

View File

@@ -42,13 +42,29 @@ class Polygon extends Shape {
this.points = points;
this.recalculateCenterAndEdgeNormals();
this._originalPoints = new Array(points.length);
this._originalPoints = points;
this._originalPoints = new Array(this.points.length);
this.points.forEach(point => this._originalPoints.push(point));
}
public collidesWithShape(other: Shape){
if (other instanceof Polygon)
return ShapeCollisions.polygonToPolygon(this, other);
let result = new CollisionResult();
if (other instanceof Polygon){
result = ShapeCollisions.polygonToPolygon(this, other);
return result;
}
if (other instanceof Circle){
result = ShapeCollisions.circleToPolygon(other, this);
if (result){
result.invertResult();
return result;
}
return null;
}
throw new Error(`overlaps of Polygon to ${other} are not supported`);
}
public recalculateCenterAndEdgeNormals() {
@@ -133,6 +149,11 @@ class Polygon extends Shape {
return isInside;
}
/**
* 建立一个对称的多边形(六边形八角形n角形)并返回点
* @param vertCount
* @param radius
*/
public static buildSymmertricalPolygon(vertCount: number, radius: number) {
let verts = new Array(vertCount);
@@ -146,5 +167,42 @@ class Polygon extends Shape {
public recalculateBounds(collider: Collider) {
this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotationWithTransform){
let hasUnitScale = true;
let tempMat: Matrix2D;
let combinedMatrix = Matrix2D.createTranslation(-this._polygonCenter.x, -this._polygonCenter.y);
if (collider.entity.transform.scale != Vector2.one){
tempMat = Matrix2D.createScale(collider.entity.transform.scale.x, collider.entity.transform.scale.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
hasUnitScale = false;
let scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.transform.scale);
this.center = scaledOffset;
}
if (collider.entity.transform.rotation != 0){
tempMat = Matrix2D.createRotation(collider.entity.transform.rotation);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.transform.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle);
}
tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.transform.rotation == 0;
if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true;
}
this.position = Vector2.add(collider.entity.transform.position, this.center);
this.bounds = Rectangle.rectEncompassingPoints(this.points);
this.bounds.location = Vector2.add(this.bounds.location, this.position);
}
}

View File

@@ -6,4 +6,5 @@ abstract class Shape {
public abstract recalculateBounds(collider: Collider);
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
public abstract overlaps(other: Shape);
public abstract collidesWithShape(other: Shape): CollisionResult;
}