移动部分类模块至es

优化框架
This commit is contained in:
YHH
2020-07-22 23:30:31 +08:00
parent 5b8f414a45
commit 15c0844e29
18 changed files with 1245 additions and 674 deletions

View File

@@ -1,81 +1,88 @@
///<reference path="./Polygon.ts" />
/**
* 多边形的特殊情况。在进行SAT碰撞检查时我们只需要检查2个轴而不是8个轴
*/
class Box extends Polygon {
constructor(width: number, height: number){
super(Box.buildBox(width, height), true);
this.width = width;
this.height = height;
}
module es {
/**
* 在一个盒子的形状中建立多边形需要的点的帮助方法
* @param width
* @param height
* 多边形的特殊情况。在进行SAT碰撞检查时我们只需要检查2个轴而不是8个轴
*/
private static buildBox(width: number, height: number): Vector2[]{
// 我们在(0,0)的中心周围创建点
let halfWidth = width / 2;
let halfHeight = height / 2;
let verts = new Array(4);
verts[0] = new Vector2(-halfWidth, -halfHeight);
verts[1] = new Vector2(halfWidth, -halfHeight);
verts[2] = new Vector2(halfWidth, halfHeight);
verts[3] = new Vector2(-halfWidth, halfHeight);
export class Box extends Polygon {
public width: number;
public height: number;
return verts;
}
public overlaps(other: Shape){
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
if (other instanceof Box)
return this.bounds.intersects(other.bounds);
if (other instanceof Circle)
return Collisions.isRectToCircle(this.bounds, other.position, other.radius);
return super.overlaps(other);
}
public collidesWithShape(other: Shape){
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
if (other instanceof Box){
return ShapeCollisions.boxToBox(this, other);
constructor(width: number, height: number){
super(Box.buildBox(width, height), true);
this.width = width;
this.height = height;
}
// TODO: 让 minkowski 运行于 cricleToBox
/**
* 在一个盒子的形状中建立多边形需要的点的帮助方法
* @param width
* @param height
*/
private static buildBox(width: number, height: number): Vector2[]{
// 我们在(0,0)的中心周围创建点
let halfWidth = width / 2;
let halfHeight = height / 2;
let verts = new Array(4);
verts[0] = new Vector2(-halfWidth, -halfHeight);
verts[1] = new Vector2(halfWidth, -halfHeight);
verts[2] = new Vector2(halfWidth, halfHeight);
verts[3] = new Vector2(-halfWidth, halfHeight);
return super.collidesWithShape(other);
}
return verts;
}
/**
* 更新框点,重新计算中心,设置宽度/高度
* @param width
* @param height
*/
public updateBox(width: number, height: number){
this.width = width;
this.height = height;
/**
* 更新框点,重新计算中心,设置宽度/高度
* @param width
* @param height
*/
public updateBox(width: number, height: number){
this.width = width;
this.height = height;
// 我们在(0,0)的中心周围创建点
let halfWidth = width / 2;
let halfHeight = height / 2;
// 我们在(0,0)的中心周围创建点
let halfWidth = width / 2;
let halfHeight = height / 2;
this.points[0] = new Vector2(-halfWidth, -halfHeight);
this.points[1] = new Vector2(halfWidth, -halfHeight);
this.points[2] = new Vector2(halfWidth, halfHeight);
this.points[3] = new Vector2(-halfWidth, halfHeight);
this.points[0] = new Vector2(-halfWidth, -halfHeight);
this.points[1] = new Vector2(halfWidth, -halfHeight);
this.points[2] = new Vector2(halfWidth, halfHeight);
this.points[3] = new Vector2(-halfWidth, halfHeight);
for (let i = 0; i < this.points.length; i ++)
this._originalPoints[i] = this.points[i];
}
for (let i = 0; i < this.points.length; i ++)
this._originalPoints[i] = this.points[i];
}
/**
*
* @param point
*/
public containsPoint(point: Vector2){
return this.bounds.contains(point.x, point.y);
public overlaps(other: Shape){
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
if (other instanceof Box)
return this.bounds.intersects(other.bounds);
if (other instanceof Circle)
return Collisions.isRectToCircle(this.bounds, other.position, other.radius);
return super.overlaps(other);
}
public collidesWithShape(other: Shape){
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
if (other instanceof Box){
return ShapeCollisions.boxToBox(this, other);
}
// TODO: 让 minkowski 运行于 cricleToBox
return super.collidesWithShape(other);
}
/**
*
* @param point
*/
public containsPoint(point: Vector2){
return this.bounds.contains(point.x, point.y);
}
}
}

View File

@@ -1,215 +1,250 @@
///<reference path="./Shape.ts" />
/**
* 多边形
*/
class Polygon extends Shape {
/** 组成多边形的点。它们应该是CW和凸的。 */
public points: Vector2[];
private _polygonCenter: Vector2;
private _areEdgeNormalsDirty = true;
protected _originalPoints: Vector2[];
public center = new Vector2();
/**
* 多边形坐标
* 此为内部字段 可访问
*/
public position: Vector2 = Vector2.zero;
public get bounds(){
return new Rectangle(this.position.x, this.position.y, 0, 0);
}
public _edgeNormals: Vector2[];
public get edgeNormals(){
if (this._areEdgeNormalsDirty)
this.buildEdgeNormals();
return this._edgeNormals;
}
public isBox: boolean;
constructor(points: Vector2[], isBox?: boolean){
super();
this.setPoints(points);
this.isBox = isBox;
}
private buildEdgeNormals(){
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 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 collidesWithShape(other: Shape){
let result = new CollisionResult();
if (other instanceof Polygon){
return ShapeCollisions.polygonToPolygon(this, other);
}
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() {
this._polygonCenter = Polygon.findPolygonCenter(this.points);
this._areEdgeNormalsDirty = true;
}
public overlaps(other: Shape){
let result: CollisionResult;
if (other instanceof Polygon)
return ShapeCollisions.polygonToPolygon(this, other);
if (other instanceof Circle){
result = ShapeCollisions.circleToPolygon(other, this);
if (result){
result.invertResult();
return true;
}
return false;
}
throw new Error(`overlaps of Pologon to ${other} are not supported`);
}
module es {
/**
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
* @param points
* 多边形
*/
public static findPolygonCenter(points: Vector2[]) {
let x = 0, y = 0;
export class Polygon extends Shape {
/**
* 组成多边形的点
* 保持顺时针与凸边形
*/
public points: Vector2[];
for (let i = 0; i < points.length; i++) {
x += points[i].x;
y += points[i].y;
/**
* 边缘法线用于SAT碰撞检测。缓存它们用于避免squareRoots
* box只有两个边缘 因为其他两边是平行的
*/
public get edgeNormals(){
if (this._areEdgeNormalsDirty)
this.buildEdgeNormals();
return this._edgeNormals;
}
return new Vector2(x / points.length, y / points.length);
}
public _areEdgeNormalsDirty = true;
public _edgeNormals: Vector2[];
/**
* 多边形的原始数据
*/
public _originalPoints: Vector2[];
public _polygonCenter: Vector2;
/**
* 用于优化未旋转box碰撞
*/
public isBox: boolean;
public isUnrotated: boolean = true;
/**
* 重定位多边形的点
* @param points
*/
public static recenterPolygonVerts(points: Vector2[]){
let center = this.findPolygonCenter(points);
for (let i = 0; i < points.length; i ++)
points[i] = Vector2.subtract(points[i], center);
}
/**
* 从点构造一个多边形
* 多边形应该以顺时针方式指定 不能重复第一个/最后一个点它们以0 0为中心
* @param points
* @param isBox
*/
constructor(points: Vector2[], isBox?: boolean){
super();
/**
* 迭代多边形的所有边,并得到任意边上离点最近的点。
* 通过最近点的平方距离和它所在的边的法线返回。
* 点应该在多边形的空间中(点-多边形.位置)
* @param points
* @param point
*/
public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { closestPoint, distanceSquared, edgeNormal } {
let distanceSquared = Number.MAX_VALUE;
let edgeNormal = new Vector2(0, 0);
let closestPoint = new Vector2(0, 0);
this.setPoints(points);
this.isBox = isBox;
}
let tempDistanceSquared;
for (let i = 0; i < points.length; i++) {
let j = i + 1;
if (j == points.length)
j = 0;
/**
* 重置点并重新计算中心和边缘法线
* @param points
*/
public setPoints(points: Vector2[]) {
this.points = points;
this.recalculateCenterAndEdgeNormals();
let closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
tempDistanceSquared = Vector2.distanceSquared(point, closest);
if (tempDistanceSquared < distanceSquared) {
distanceSquared = tempDistanceSquared;
closestPoint = closest;
// 求直线的法线
let line = Vector2.subtract(points[j], points[i]);
edgeNormal.x = -line.y;
edgeNormal.y = line.x;
this._originalPoints = [];
for (let i = 0; i < this.points.length; i ++){
this._originalPoints.push(this.points[i]);
}
}
edgeNormal = Vector2.normalize(edgeNormal);
/**
* 重新计算多边形中心
* 如果点数改变必须调用该方法
*/
public recalculateCenterAndEdgeNormals() {
this._polygonCenter = Polygon.findPolygonCenter(this.points);
this._areEdgeNormalsDirty = true;
}
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal };
}
/**
* 建立多边形边缘法线
* 它们仅由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);
public recalculateBounds(collider: Collider){
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心我们会从那开始
}
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];
public pointCollidesWithShape(point: Vector2): CollisionResult {
return ShapeCollisions.pointToPoly(point, this);
}
/**
* 本质上,这个算法所做的就是从一个点发射一条射线。
* 如果它与奇数条多边形边相交,我们就知道它在多边形内部。
* @param point
*/
public containsPoint(point: Vector2) {
// 将点归一化到多边形坐标空间中
point = Vector2.subtract(point, this.position);
let isInside = false;
for (let i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
if (((this.points[i].y > point.y) != (this.points[j].y > point.y)) &&
(point.x < (this.points[j].x - this.points[i].x) * (point.y - this.points[i].y) / (this.points[j].y - this.points[i].y) +
this.points[i].x)) {
isInside = !isInside;
let perp = Vector2Ext.perpendicular(p1, p2);
perp = Vector2.normalize(perp);
this._edgeNormals[i] = perp;
}
}
return isInside;
}
/**
* 建立一个对称的多边形(六边形八角形n角形)并返回点
* @param vertCount
* @param radius
*/
public static buildSymmertricalPolygon(vertCount: number, radius: number) {
let verts = new Array(vertCount);
/**
* 建立一个对称的多边形(六边形八角形n角形)并返回点
* @param vertCount
* @param radius
*/
public static buildSymmetricalPolygon(vertCount: number, radius: number) {
let verts = new Array(vertCount);
for (let i = 0; i < vertCount; i++) {
let a = 2 * Math.PI * (i / vertCount);
verts[i] = new Vector2(Math.cos(a), Math.sin(a) * radius);
for (let i = 0; i < vertCount; i++) {
let a = 2 * Math.PI * (i / vertCount);
verts[i] = new Vector2(Math.cos(a), Math.sin(a) * radius);
}
return verts;
}
return verts;
/**
* 重定位多边形的点
* @param points
*/
public static recenterPolygonVerts(points: Vector2[]){
let center = this.findPolygonCenter(points);
for (let i = 0; i < points.length; i ++)
points[i] = Vector2.subtract(points[i], center);
}
/**
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
* @param points
*/
public static findPolygonCenter(points: Vector2[]) {
let x = 0, y = 0;
for (let i = 0; i < points.length; i++) {
x += points[i].x;
y += points[i].y;
}
return new Vector2(x / points.length, y / points.length);
}
/**
* 迭代多边形的所有边,并得到任意边上离点最近的点。
* 通过最近点的平方距离和它所在的边的法线返回。
* 点应该在多边形的空间中(点-多边形.位置)
* @param points
* @param point
*/
public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { closestPoint, distanceSquared, edgeNormal } {
let distanceSquared = Number.MAX_VALUE;
let edgeNormal = new Vector2(0, 0);
let closestPoint = new Vector2(0, 0);
let tempDistanceSquared;
for (let i = 0; i < points.length; i++) {
let j = i + 1;
if (j == points.length)
j = 0;
let closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
tempDistanceSquared = Vector2.distanceSquared(point, closest);
if (tempDistanceSquared < distanceSquared) {
distanceSquared = tempDistanceSquared;
closestPoint = closest;
// 求直线的法线
let line = Vector2.subtract(points[j], points[i]);
edgeNormal.x = -line.y;
edgeNormal.y = line.x;
}
}
edgeNormal = Vector2.normalize(edgeNormal);
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal };
}
public recalculateBounds(collider: Collider){
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心我们会从那开始
this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotateWithTransform){
this.isUnrotated = collider.entity.transform.rotation == 0;
}
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);
}
public overlaps(other: Shape){
let result: CollisionResult;
if (other instanceof Polygon)
return ShapeCollisions.polygonToPolygon(this, other);
if (other instanceof Circle){
result = ShapeCollisions.circleToPolygon(other, this);
if (result){
result.invertResult();
return true;
}
return false;
}
throw new Error(`overlaps of Pologon to ${other} are not supported`);
}
public collidesWithShape(other: Shape){
let result = new CollisionResult();
if (other instanceof Polygon){
return ShapeCollisions.polygonToPolygon(this, other);
}
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`);
}
/**
* 本质上,这个算法所做的就是从一个点发射一条射线。
* 如果它与奇数条多边形边相交,我们就知道它在多边形内部。
* @param point
*/
public containsPoint(point: Vector2) {
// 将点归一化到多边形坐标空间中
point = Vector2.subtract(point, this.position);
let isInside = false;
for (let i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
if (((this.points[i].y > point.y) != (this.points[j].y > point.y)) &&
(point.x < (this.points[j].x - this.points[i].x) * (point.y - this.points[i].y) / (this.points[j].y - this.points[i].y) +
this.points[i].x)) {
isInside = !isInside;
}
}
return isInside;
}
public pointCollidesWithShape(point: Vector2): CollisionResult {
return ShapeCollisions.pointToPoly(point, this);
}
}
}

View File

@@ -1,21 +1,23 @@
abstract class Shape {
/** 缓存的形状边界 内部字段 */
public bounds: Rectangle;
/**
* 这不是中心。这个值不一定是物体的中心。对撞机更准确
* 应用任何转换旋转的localOffset
* 内部字段
*/
public center: Vector2;
/**
* 有一个单独的位置字段可以让我们改变形状的位置来进行碰撞检查而不是改变entity.position。
* 触发碰撞器/边界/散列更新的位置。
* 内部字段
*/
public position: Vector2;
module es {
export abstract class Shape {
/**
* 有一个单独的位置字段可以让我们改变形状的位置来进行碰撞检查而不是改变entity.position。
* 触发碰撞器/边界/散列更新的位置
* 内部字段
*/
public position: Vector2;
/**
* 这不是中心。这个值不一定是物体的中心。对撞机更准确。
* 应用任何转换旋转的localOffset
* 内部字段
*/
public center: Vector2;
/** 缓存的形状边界 内部字段 */
public bounds: Rectangle;
public abstract recalculateBounds(collider: Collider);
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
public abstract overlaps(other: Shape);
public abstract collidesWithShape(other: Shape): CollisionResult;
}
public abstract recalculateBounds(collider: Collider);
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
public abstract overlaps(other: Shape);
public abstract collidesWithShape(other: Shape): CollisionResult;
}
}