reformat code
This commit is contained in:
@@ -7,7 +7,7 @@ module es {
|
||||
public width: number;
|
||||
public height: number;
|
||||
|
||||
constructor(width: number, height: number){
|
||||
constructor(width: number, height: number) {
|
||||
super(Box.buildBox(width, height), true);
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
@@ -18,7 +18,7 @@ module es {
|
||||
* @param width
|
||||
* @param height
|
||||
*/
|
||||
private static buildBox(width: number, height: number): Vector2[]{
|
||||
private static buildBox(width: number, height: number): Vector2[] {
|
||||
// 我们在(0,0)的中心周围创建点
|
||||
let halfWidth = width / 2;
|
||||
let halfHeight = height / 2;
|
||||
@@ -36,7 +36,7 @@ module es {
|
||||
* @param width
|
||||
* @param height
|
||||
*/
|
||||
public updateBox(width: number, height: number){
|
||||
public updateBox(width: number, height: number) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
@@ -49,13 +49,13 @@ module es {
|
||||
this.points[2] = new Vector2(halfWidth, halfHeight);
|
||||
this.points[3] = new Vector2(-halfWidth, halfHeight);
|
||||
|
||||
for (let i = 0; i < this.points.length; i ++)
|
||||
for (let i = 0; i < this.points.length; i++)
|
||||
this._originalPoints[i] = this.points[i];
|
||||
}
|
||||
|
||||
public overlaps(other: Shape){
|
||||
public overlaps(other: Shape) {
|
||||
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
||||
if (this.isUnrotated){
|
||||
if (this.isUnrotated) {
|
||||
if (other instanceof Box)
|
||||
return this.bounds.intersects(other.bounds);
|
||||
|
||||
@@ -66,9 +66,9 @@ module es {
|
||||
return super.overlaps(other);
|
||||
}
|
||||
|
||||
public collidesWithShape(other: Shape, result: CollisionResult): boolean{
|
||||
public collidesWithShape(other: Shape, result: CollisionResult): boolean {
|
||||
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
||||
if (other instanceof Box && (other as Box).isUnrotated){
|
||||
if (other instanceof Box && (other as Box).isUnrotated) {
|
||||
return ShapeCollisions.boxToBox(this, other, result);
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ module es {
|
||||
return super.collidesWithShape(other, result);
|
||||
}
|
||||
|
||||
public containsPoint(point: Vector2){
|
||||
public containsPoint(point: Vector2) {
|
||||
if (this.isUnrotated)
|
||||
return this.bounds.contains(point.x, point.y);
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ module es {
|
||||
public normal: Vector2 = Vector2.zero;
|
||||
public point: Vector2 = Vector2.zero;
|
||||
|
||||
public invertResult(){
|
||||
public invertResult() {
|
||||
this.minimumTranslationVector = Vector2.negate(this.minimumTranslationVector);
|
||||
this.normal = Vector2.negate(this.normal);
|
||||
}
|
||||
|
||||
@@ -9,19 +9,7 @@ module es {
|
||||
* 保持顺时针与凸边形
|
||||
*/
|
||||
public points: Vector2[];
|
||||
|
||||
/**
|
||||
* 边缘法线用于SAT碰撞检测。缓存它们用于避免squareRoots
|
||||
* box只有两个边缘 因为其他两边是平行的
|
||||
*/
|
||||
public get edgeNormals(){
|
||||
if (this._areEdgeNormalsDirty)
|
||||
this.buildEdgeNormals();
|
||||
return this._edgeNormals;
|
||||
}
|
||||
|
||||
public _areEdgeNormalsDirty = true;
|
||||
public _edgeNormals: Vector2[];
|
||||
/**
|
||||
* 多边形的原始数据
|
||||
*/
|
||||
@@ -39,61 +27,25 @@ module es {
|
||||
* @param points
|
||||
* @param isBox
|
||||
*/
|
||||
constructor(points: Vector2[], isBox?: boolean){
|
||||
constructor(points: Vector2[], isBox?: boolean) {
|
||||
super();
|
||||
|
||||
this.setPoints(points);
|
||||
this.isBox = isBox;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置点并重新计算中心和边缘法线
|
||||
* @param points
|
||||
*/
|
||||
public setPoints(points: Vector2[]) {
|
||||
this.points = points;
|
||||
this.recalculateCenterAndEdgeNormals();
|
||||
|
||||
this._originalPoints = [];
|
||||
for (let i = 0; i < this.points.length; i ++){
|
||||
this._originalPoints.push(this.points[i]);
|
||||
}
|
||||
}
|
||||
public _edgeNormals: Vector2[];
|
||||
|
||||
/**
|
||||
* 重新计算多边形中心
|
||||
* 如果点数改变必须调用该方法
|
||||
* 边缘法线用于SAT碰撞检测。缓存它们用于避免squareRoots
|
||||
* box只有两个边缘 因为其他两边是平行的
|
||||
*/
|
||||
public recalculateCenterAndEdgeNormals() {
|
||||
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
||||
this._areEdgeNormalsDirty = true;
|
||||
public get edgeNormals() {
|
||||
if (this._areEdgeNormalsDirty)
|
||||
this.buildEdgeNormals();
|
||||
return this._edgeNormals;
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立多边形边缘法线
|
||||
* 它们仅由edgeNormals getter惰性创建和更新
|
||||
*/
|
||||
public buildEdgeNormals(){
|
||||
// 对于box 我们只需要两条边,因为另外两条边是平行的
|
||||
let totalEdges = this.isBox ? 2 : this.points.length;
|
||||
if (this._edgeNormals == null || this._edgeNormals.length != totalEdges)
|
||||
this._edgeNormals = new Array(totalEdges);
|
||||
|
||||
let p2: Vector2;
|
||||
for (let i = 0; i < totalEdges; i ++){
|
||||
let p1 = this.points[i];
|
||||
if (i + 1 >= this.points.length)
|
||||
p2 = this.points[0];
|
||||
else
|
||||
p2 = this.points[i + 1];
|
||||
|
||||
let perp = Vector2Ext.perpendicular(p1, p2);
|
||||
perp = Vector2.normalize(perp);
|
||||
this._edgeNormals[i] = perp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 建立一个对称的多边形(六边形,八角形,n角形)并返回点
|
||||
* @param vertCount
|
||||
@@ -114,9 +66,9 @@ module es {
|
||||
* 重定位多边形的点
|
||||
* @param points
|
||||
*/
|
||||
public static recenterPolygonVerts(points: Vector2[]){
|
||||
public static recenterPolygonVerts(points: Vector2[]) {
|
||||
let center = this.findPolygonCenter(points);
|
||||
for (let i = 0; i < points.length; i ++)
|
||||
for (let i = 0; i < points.length; i++)
|
||||
points[i] = Vector2.subtract(points[i], center);
|
||||
}
|
||||
|
||||
@@ -173,16 +125,63 @@ module es {
|
||||
return closestPoint;
|
||||
}
|
||||
|
||||
public recalculateBounds(collider: Collider){
|
||||
/**
|
||||
* 重置点并重新计算中心和边缘法线
|
||||
* @param points
|
||||
*/
|
||||
public setPoints(points: Vector2[]) {
|
||||
this.points = points;
|
||||
this.recalculateCenterAndEdgeNormals();
|
||||
|
||||
this._originalPoints = [];
|
||||
for (let i = 0; i < this.points.length; i++) {
|
||||
this._originalPoints.push(this.points[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新计算多边形中心
|
||||
* 如果点数改变必须调用该方法
|
||||
*/
|
||||
public recalculateCenterAndEdgeNormals() {
|
||||
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
||||
this._areEdgeNormalsDirty = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立多边形边缘法线
|
||||
* 它们仅由edgeNormals getter惰性创建和更新
|
||||
*/
|
||||
public buildEdgeNormals() {
|
||||
// 对于box 我们只需要两条边,因为另外两条边是平行的
|
||||
let totalEdges = this.isBox ? 2 : this.points.length;
|
||||
if (this._edgeNormals == null || this._edgeNormals.length != totalEdges)
|
||||
this._edgeNormals = new Array(totalEdges);
|
||||
|
||||
let p2: Vector2;
|
||||
for (let i = 0; i < totalEdges; i++) {
|
||||
let p1 = this.points[i];
|
||||
if (i + 1 >= this.points.length)
|
||||
p2 = this.points[0];
|
||||
else
|
||||
p2 = this.points[i + 1];
|
||||
|
||||
let perp = Vector2Ext.perpendicular(p1, p2);
|
||||
perp = Vector2.normalize(perp);
|
||||
this._edgeNormals[i] = perp;
|
||||
}
|
||||
}
|
||||
|
||||
public recalculateBounds(collider: Collider) {
|
||||
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
||||
this.center = collider.localOffset;
|
||||
|
||||
if (collider.shouldColliderScaleAndRotateWithTransform){
|
||||
if (collider.shouldColliderScaleAndRotateWithTransform) {
|
||||
let hasUnitScale = true;
|
||||
let tempMat: Matrix2D;
|
||||
let combinedMatrix = Matrix2D.create().translate(-this._polygonCenter.x, -this._polygonCenter.y);
|
||||
|
||||
if (collider.entity.transform.scale != Vector2.zero){
|
||||
if (collider.entity.transform.scale != Vector2.zero) {
|
||||
tempMat = Matrix2D.create().scale(collider.entity.transform.scale.x, collider.entity.transform.scale.y);
|
||||
combinedMatrix = combinedMatrix.multiply(tempMat);
|
||||
hasUnitScale = false;
|
||||
@@ -191,7 +190,7 @@ module es {
|
||||
this.center = Vector2.multiply(collider.localOffset, collider.entity.transform.scale);
|
||||
}
|
||||
|
||||
if (collider.entity.transform.rotation != 0){
|
||||
if (collider.entity.transform.rotation != 0) {
|
||||
tempMat = Matrix2D.create().rotate(collider.entity.transform.rotation);
|
||||
combinedMatrix = combinedMatrix.multiply(tempMat);
|
||||
|
||||
@@ -222,13 +221,13 @@ module es {
|
||||
this.bounds.location = this.bounds.location.add(this.position);
|
||||
}
|
||||
|
||||
public overlaps(other: Shape){
|
||||
public overlaps(other: Shape) {
|
||||
let result: CollisionResult = new CollisionResult();
|
||||
if (other instanceof Polygon)
|
||||
return ShapeCollisions.polygonToPolygon(this, other, result);
|
||||
|
||||
if (other instanceof Circle){
|
||||
if (ShapeCollisions.circleToPolygon(other, this, result)){
|
||||
if (other instanceof Circle) {
|
||||
if (ShapeCollisions.circleToPolygon(other, this, result)) {
|
||||
result.invertResult();
|
||||
return true;
|
||||
}
|
||||
@@ -239,13 +238,13 @@ module es {
|
||||
throw new Error(`overlaps of Pologon to ${other} are not supported`);
|
||||
}
|
||||
|
||||
public collidesWithShape(other: Shape, result: CollisionResult): boolean{
|
||||
if (other instanceof Polygon){
|
||||
public collidesWithShape(other: Shape, result: CollisionResult): boolean {
|
||||
if (other instanceof Polygon) {
|
||||
return ShapeCollisions.polygonToPolygon(this, other, result);
|
||||
}
|
||||
|
||||
if (other instanceof Circle){
|
||||
if (ShapeCollisions.circleToPolygon(other, this, result)){
|
||||
if (other instanceof Circle) {
|
||||
if (ShapeCollisions.circleToPolygon(other, this, result)) {
|
||||
result.invertResult();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -16,11 +16,14 @@ module es {
|
||||
public bounds: Rectangle;
|
||||
|
||||
public abstract recalculateBounds(collider: Collider);
|
||||
|
||||
public abstract overlaps(other: Shape): boolean;
|
||||
|
||||
public abstract collidesWithShape(other: Shape, collisionResult: CollisionResult): boolean;
|
||||
|
||||
public abstract pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
|
||||
|
||||
public clone(): Shape{
|
||||
public clone(): Shape {
|
||||
return ObjectUtils.clone<Shape>(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
return { min: min, max: max };
|
||||
return {min: min, max: max};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,11 +245,11 @@ module es {
|
||||
* @param first
|
||||
* @param second
|
||||
*/
|
||||
public static circleToCircle(first: Circle, second: Circle, result: CollisionResult): boolean{
|
||||
public static circleToCircle(first: Circle, second: Circle, result: CollisionResult): boolean {
|
||||
let distanceSquared = Vector2.distanceSquared(first.position, second.position);
|
||||
let sumOfRadii = first.radius + second.radius;
|
||||
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||
if (collided){
|
||||
if (collided) {
|
||||
result.normal = Vector2.normalize(Vector2.subtract(first.position, second.position));
|
||||
let depth = sumOfRadii - Math.sqrt(distanceSquared);
|
||||
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth), result.normal);
|
||||
@@ -267,9 +267,9 @@ module es {
|
||||
* @param second
|
||||
* @param result
|
||||
*/
|
||||
public static boxToBox(first: Box, second: Box, result: CollisionResult): boolean{
|
||||
public static boxToBox(first: Box, second: Box, result: CollisionResult): boolean {
|
||||
let minkowskiDiff = this.minkowskiDifference(first, second);
|
||||
if (minkowskiDiff.contains(0, 0)){
|
||||
if (minkowskiDiff.contains(0, 0)) {
|
||||
// 计算MTV。如果它是零,我们就可以称它为非碰撞
|
||||
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
|
||||
|
||||
@@ -285,7 +285,7 @@ module es {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static minkowskiDifference(first: Box, second: Box){
|
||||
private static minkowskiDifference(first: Box, second: Box) {
|
||||
// 我们需要第一个框的左上角
|
||||
// 碰撞器只会修改运动的位置所以我们需要用位置来计算出运动是什么。
|
||||
let positionOffset = Vector2.subtract(first.position, Vector2.add(first.bounds.location, Vector2.divide(first.bounds.size, new Vector2(2))));
|
||||
|
||||
Reference in New Issue
Block a user