完善shapeCollision 支持多边形
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
- [x] 掩码实用类
|
- [x] 掩码实用类
|
||||||
- [x] BreadthFirst 寻路算法
|
- [x] BreadthFirst 寻路算法
|
||||||
- [x] Dijkstra 寻路算法
|
- [x] Dijkstra 寻路算法
|
||||||
|
- [x] 事件处理器
|
||||||
|
|
||||||
## 计划列表
|
## 计划列表
|
||||||
|
|
||||||
@@ -33,14 +34,11 @@
|
|||||||
- [ ] 系统列表
|
- [ ] 系统列表
|
||||||
- [ ] 被动系统
|
- [ ] 被动系统
|
||||||
- [ ] 协调系统
|
- [ ] 协调系统
|
||||||
- [ ] 高性能物理引擎
|
|
||||||
- [ ] FSM 简易状态机
|
|
||||||
- [ ] 数学库
|
- [ ] 数学库
|
||||||
- [ ] 贝塞尔曲线
|
- [ ] 贝塞尔曲线
|
||||||
- [ ] 快速随机数类
|
- [ ] 快速随机数类
|
||||||
- [ ] 浮点助手类
|
- [ ] 浮点助手类
|
||||||
- [ ] 高性能数组
|
- [ ] 高性能数组
|
||||||
- [ ] 事件处理器
|
|
||||||
|
|
||||||
## 相关库
|
## 相关库
|
||||||
|
|
||||||
|
|||||||
53
demo/libs/framework/framework.d.ts
vendored
53
demo/libs/framework/framework.d.ts
vendored
@@ -146,7 +146,7 @@ declare abstract class Component {
|
|||||||
readonly transform: Transform;
|
readonly transform: Transform;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
setEnabled(isEnabled: boolean): this;
|
setEnabled(isEnabled: boolean): this;
|
||||||
abstract initialize(): any;
|
initialize(): void;
|
||||||
onAddedToEntity(): void;
|
onAddedToEntity(): void;
|
||||||
onRemovedFromEntity(): void;
|
onRemovedFromEntity(): void;
|
||||||
onEnabled(): void;
|
onEnabled(): void;
|
||||||
@@ -194,6 +194,7 @@ declare class Entity {
|
|||||||
hasComponent<T extends Component>(type: any): boolean;
|
hasComponent<T extends Component>(type: any): boolean;
|
||||||
getOrCreateComponent<T extends Component>(type: T): T;
|
getOrCreateComponent<T extends Component>(type: T): T;
|
||||||
getComponent<T extends Component>(type: any): T;
|
getComponent<T extends Component>(type: any): T;
|
||||||
|
getComponents<T extends Component>(type: any): T[];
|
||||||
removeComponentForType<T extends Component>(type: any): boolean;
|
removeComponentForType<T extends Component>(type: any): boolean;
|
||||||
removeComponent(component: Component): void;
|
removeComponent(component: Component): void;
|
||||||
removeAllComponents(): void;
|
removeAllComponents(): void;
|
||||||
@@ -376,11 +377,18 @@ declare class SpriteRenderer extends RenderableComponent {
|
|||||||
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
||||||
initialize(): void;
|
initialize(): void;
|
||||||
}
|
}
|
||||||
|
interface ITriggerListener {
|
||||||
|
onTriggerEnter(other: Collider, local: Collider): any;
|
||||||
|
onTriggerExit(other: Collider, local: Collider): any;
|
||||||
|
}
|
||||||
declare abstract class Collider extends Component {
|
declare abstract class Collider extends Component {
|
||||||
shape: Shape;
|
shape: Shape;
|
||||||
physicsLayer: number;
|
physicsLayer: number;
|
||||||
isTrigger: boolean;
|
isTrigger: boolean;
|
||||||
registeredPhysicsBounds: Rectangle;
|
registeredPhysicsBounds: Rectangle;
|
||||||
|
shouldColliderScaleAndRotationWithTransform: boolean;
|
||||||
|
collidesWithLayers: number;
|
||||||
|
_localOffsetLength: number;
|
||||||
protected _isParentEntityAddedToScene: any;
|
protected _isParentEntityAddedToScene: any;
|
||||||
protected _isPositionDirty: boolean;
|
protected _isPositionDirty: boolean;
|
||||||
protected _isRotationDirty: boolean;
|
protected _isRotationDirty: boolean;
|
||||||
@@ -392,7 +400,10 @@ declare abstract class Collider extends Component {
|
|||||||
setLocalOffset(offset: Vector2): void;
|
setLocalOffset(offset: Vector2): void;
|
||||||
registerColliderWithPhysicsSystem(): void;
|
registerColliderWithPhysicsSystem(): void;
|
||||||
unregisterColliderWithPhysicsSystem(): void;
|
unregisterColliderWithPhysicsSystem(): void;
|
||||||
initialize(): void;
|
overlaps(other: Collider): any;
|
||||||
|
onEntityTransformChanged(comp: ComponentTransform): void;
|
||||||
|
onEnabled(): void;
|
||||||
|
onDisabled(): void;
|
||||||
}
|
}
|
||||||
declare class BoxCollider extends Collider {
|
declare class BoxCollider extends Collider {
|
||||||
width: number;
|
width: number;
|
||||||
@@ -460,6 +471,7 @@ declare class ComponentList {
|
|||||||
updateLists(): void;
|
updateLists(): void;
|
||||||
private handleRemove;
|
private handleRemove;
|
||||||
getComponent<T extends Component>(type: any, onlyReturnInitializedComponents: boolean): T;
|
getComponent<T extends Component>(type: any, onlyReturnInitializedComponents: boolean): T;
|
||||||
|
getComponents<T extends Component>(type: any): T[];
|
||||||
update(): void;
|
update(): void;
|
||||||
onEntityTransformChanged(comp: any): void;
|
onEntityTransformChanged(comp: any): void;
|
||||||
}
|
}
|
||||||
@@ -529,6 +541,9 @@ declare class Flags {
|
|||||||
static invertFlags(self: number): number;
|
static invertFlags(self: number): number;
|
||||||
}
|
}
|
||||||
declare class MathHelper {
|
declare class MathHelper {
|
||||||
|
static readonly Epsilon: number;
|
||||||
|
static readonly Rad2Deg: number;
|
||||||
|
static readonly Deg2Rad: number;
|
||||||
static toDegrees(radians: number): number;
|
static toDegrees(radians: number): number;
|
||||||
static toRadians(degrees: number): number;
|
static toRadians(degrees: number): number;
|
||||||
static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number;
|
static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number;
|
||||||
@@ -536,6 +551,7 @@ declare class MathHelper {
|
|||||||
static clamp(value: number, min: number, max: number): number;
|
static clamp(value: number, min: number, max: number): number;
|
||||||
static minOf(a: number, b: number, c: number, d: number): number;
|
static minOf(a: number, b: number, c: number, d: number): number;
|
||||||
static maxOf(a: number, b: number, c: number, d: number): number;
|
static maxOf(a: number, b: number, c: number, d: number): number;
|
||||||
|
static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number): Vector2;
|
||||||
}
|
}
|
||||||
declare class Matrix2D {
|
declare class Matrix2D {
|
||||||
m11: number;
|
m11: number;
|
||||||
@@ -586,6 +602,14 @@ declare class Rectangle {
|
|||||||
declare class Vector2 {
|
declare class Vector2 {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
|
private static readonly unitYVector;
|
||||||
|
private static readonly unitXVector;
|
||||||
|
private static readonly unitVector2;
|
||||||
|
private static readonly zeroVector2;
|
||||||
|
static readonly zero: Vector2;
|
||||||
|
static readonly one: Vector2;
|
||||||
|
static readonly unitX: Vector2;
|
||||||
|
static readonly unitY: Vector2;
|
||||||
constructor(x?: number, y?: number);
|
constructor(x?: number, y?: number);
|
||||||
static add(value1: Vector2, value2: Vector2): Vector2;
|
static add(value1: Vector2, value2: Vector2): Vector2;
|
||||||
static divide(value1: Vector2, value2: Vector2): Vector2;
|
static divide(value1: Vector2, value2: Vector2): Vector2;
|
||||||
@@ -599,6 +623,11 @@ declare class Vector2 {
|
|||||||
static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2;
|
static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2;
|
||||||
static transform(position: Vector2, matrix: Matrix2D): Vector2;
|
static transform(position: Vector2, matrix: Matrix2D): Vector2;
|
||||||
static distance(value1: Vector2, value2: Vector2): number;
|
static distance(value1: Vector2, value2: Vector2): number;
|
||||||
|
static negate(value: Vector2): Vector2;
|
||||||
|
}
|
||||||
|
declare class ColliderTriggerHelper {
|
||||||
|
private _entity;
|
||||||
|
update(): void;
|
||||||
}
|
}
|
||||||
declare enum PointSectors {
|
declare enum PointSectors {
|
||||||
center = 0,
|
center = 0,
|
||||||
@@ -638,6 +667,7 @@ declare abstract class Shape {
|
|||||||
center: Vector2;
|
center: Vector2;
|
||||||
abstract recalculateBounds(collider: Collider): any;
|
abstract recalculateBounds(collider: Collider): any;
|
||||||
abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
|
abstract overlaps(other: Shape): any;
|
||||||
}
|
}
|
||||||
declare class Polygon extends Shape {
|
declare class Polygon extends Shape {
|
||||||
points: Vector2[];
|
points: Vector2[];
|
||||||
@@ -651,8 +681,9 @@ declare class Polygon extends Shape {
|
|||||||
constructor(vertCount: number, radius: number);
|
constructor(vertCount: number, radius: number);
|
||||||
private buildEdgeNormals;
|
private buildEdgeNormals;
|
||||||
setPoints(points: Vector2[]): void;
|
setPoints(points: Vector2[]): void;
|
||||||
collidesWithShape(other: Shape): void;
|
collidesWithShape(other: Shape): CollisionResult;
|
||||||
recalculateCenterAndEdgeNormals(): void;
|
recalculateCenterAndEdgeNormals(): void;
|
||||||
|
overlaps(other: Shape): any;
|
||||||
static findPolygonCenter(points: Vector2[]): Vector2;
|
static findPolygonCenter(points: Vector2[]): Vector2;
|
||||||
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): {
|
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): {
|
||||||
closestPoint: any;
|
closestPoint: any;
|
||||||
@@ -677,20 +708,27 @@ declare class Circle extends Shape {
|
|||||||
pointCollidesWithShape(point: Vector2): CollisionResult;
|
pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
collidesWithShape(other: Shape): CollisionResult;
|
collidesWithShape(other: Shape): CollisionResult;
|
||||||
recalculateBounds(collider: Collider): void;
|
recalculateBounds(collider: Collider): void;
|
||||||
|
overlaps(other: Shape): any;
|
||||||
}
|
}
|
||||||
declare class CollisionResult {
|
declare class CollisionResult {
|
||||||
minimumTranslationVector: Vector2;
|
minimumTranslationVector: Vector2;
|
||||||
normal: Vector2;
|
normal: Vector2;
|
||||||
point: Vector2;
|
point: Vector2;
|
||||||
|
invertResult(): void;
|
||||||
}
|
}
|
||||||
declare class ShapeCollisions {
|
declare class ShapeCollisions {
|
||||||
static polygonToPolygon(first: Polygon, second: Polygon): void;
|
static polygonToPolygon(first: Polygon, second: Polygon): CollisionResult;
|
||||||
static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): void;
|
static intervalDistance(minA: number, maxA: number, minB: number, maxB: any): number;
|
||||||
|
static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): {
|
||||||
|
min: number;
|
||||||
|
max: number;
|
||||||
|
};
|
||||||
static circleToPolygon(circle: Circle, polygon: Polygon): CollisionResult;
|
static circleToPolygon(circle: Circle, polygon: Polygon): CollisionResult;
|
||||||
static circleToRect(circle: Circle, box: Box): CollisionResult;
|
static circleToBox(circle: Circle, box: Box): CollisionResult;
|
||||||
static pointToCicle(point: Vector2, circle: Circle): CollisionResult;
|
static pointToCircle(point: Vector2, circle: Circle): CollisionResult;
|
||||||
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
|
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
|
||||||
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
|
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
|
||||||
|
static circleToCircle(first: Circle, second: Circle): CollisionResult;
|
||||||
}
|
}
|
||||||
declare class SpatialHash {
|
declare class SpatialHash {
|
||||||
gridBounds: Rectangle;
|
gridBounds: Rectangle;
|
||||||
@@ -738,6 +776,7 @@ declare class Vector2Ext {
|
|||||||
static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean;
|
static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean;
|
||||||
static cross(u: Vector2, v: Vector2): number;
|
static cross(u: Vector2, v: Vector2): number;
|
||||||
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
||||||
|
static normalize(vec: Vector2): Vector2;
|
||||||
}
|
}
|
||||||
declare class WebGLUtils {
|
declare class WebGLUtils {
|
||||||
static getWebGL(): WebGLRenderingContext;
|
static getWebGL(): WebGLRenderingContext;
|
||||||
|
|||||||
@@ -771,6 +771,8 @@ var Component = (function () {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
Component.prototype.initialize = function () {
|
||||||
|
};
|
||||||
Component.prototype.onAddedToEntity = function () {
|
Component.prototype.onAddedToEntity = function () {
|
||||||
};
|
};
|
||||||
Component.prototype.onRemovedFromEntity = function () {
|
Component.prototype.onRemovedFromEntity = function () {
|
||||||
@@ -1011,6 +1013,9 @@ var Entity = (function () {
|
|||||||
Entity.prototype.getComponent = function (type) {
|
Entity.prototype.getComponent = function (type) {
|
||||||
return this.components.getComponent(type, false);
|
return this.components.getComponent(type, false);
|
||||||
};
|
};
|
||||||
|
Entity.prototype.getComponents = function (type) {
|
||||||
|
return this.components.getComponents(type);
|
||||||
|
};
|
||||||
Entity.prototype.removeComponentForType = function (type) {
|
Entity.prototype.removeComponentForType = function (type) {
|
||||||
var comp = this.getComponent(type);
|
var comp = this.getComponent(type);
|
||||||
if (comp) {
|
if (comp) {
|
||||||
@@ -1786,6 +1791,8 @@ var Collider = (function (_super) {
|
|||||||
function Collider() {
|
function Collider() {
|
||||||
var _this = _super !== null && _super.apply(this, arguments) || this;
|
var _this = _super !== null && _super.apply(this, arguments) || this;
|
||||||
_this.physicsLayer = 1 << 0;
|
_this.physicsLayer = 1 << 0;
|
||||||
|
_this.shouldColliderScaleAndRotationWithTransform = true;
|
||||||
|
_this.collidesWithLayers = Physics.allLayers;
|
||||||
_this._isPositionDirty = true;
|
_this._isPositionDirty = true;
|
||||||
_this._isRotationDirty = true;
|
_this._isRotationDirty = true;
|
||||||
return _this;
|
return _this;
|
||||||
@@ -1815,6 +1822,7 @@ var Collider = (function (_super) {
|
|||||||
if (this._localOffset != offset) {
|
if (this._localOffset != offset) {
|
||||||
this.unregisterColliderWithPhysicsSystem();
|
this.unregisterColliderWithPhysicsSystem();
|
||||||
this._localOffset = offset;
|
this._localOffset = offset;
|
||||||
|
this._localOffsetLength = this._localOffset.length();
|
||||||
this._isPositionDirty = true;
|
this._isPositionDirty = true;
|
||||||
this.registerColliderWithPhysicsSystem();
|
this.registerColliderWithPhysicsSystem();
|
||||||
}
|
}
|
||||||
@@ -1831,7 +1839,30 @@ var Collider = (function (_super) {
|
|||||||
}
|
}
|
||||||
this._isColliderRegisterd = false;
|
this._isColliderRegisterd = false;
|
||||||
};
|
};
|
||||||
Collider.prototype.initialize = function () {
|
Collider.prototype.overlaps = function (other) {
|
||||||
|
return this.shape.overlaps(other.shape);
|
||||||
|
};
|
||||||
|
Collider.prototype.onEntityTransformChanged = function (comp) {
|
||||||
|
switch (comp) {
|
||||||
|
case ComponentTransform.position:
|
||||||
|
this._isPositionDirty = true;
|
||||||
|
break;
|
||||||
|
case ComponentTransform.scale:
|
||||||
|
this._isPositionDirty = true;
|
||||||
|
break;
|
||||||
|
case ComponentTransform.rotation:
|
||||||
|
this._isRotationDirty = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (this._isColliderRegisterd)
|
||||||
|
Physics.updateCollider(this);
|
||||||
|
};
|
||||||
|
Collider.prototype.onEnabled = function () {
|
||||||
|
this.registerColliderWithPhysicsSystem();
|
||||||
|
this._isPositionDirty = this._isRotationDirty = true;
|
||||||
|
};
|
||||||
|
Collider.prototype.onDisabled = function () {
|
||||||
|
this.unregisterColliderWithPhysicsSystem();
|
||||||
};
|
};
|
||||||
return Collider;
|
return Collider;
|
||||||
}(Component));
|
}(Component));
|
||||||
@@ -2177,6 +2208,20 @@ var ComponentList = (function () {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
ComponentList.prototype.getComponents = function (type) {
|
||||||
|
var components = [];
|
||||||
|
for (var i = 0; i < this._components.length; i++) {
|
||||||
|
var component = this._components[i];
|
||||||
|
if (component instanceof type)
|
||||||
|
components.push(components);
|
||||||
|
}
|
||||||
|
for (var i = 0; i < this._componentsToAdd.length; i++) {
|
||||||
|
var component = this._componentsToAdd[i];
|
||||||
|
if (component instanceof type)
|
||||||
|
components.push(components);
|
||||||
|
}
|
||||||
|
return components;
|
||||||
|
};
|
||||||
ComponentList.prototype.update = function () {
|
ComponentList.prototype.update = function () {
|
||||||
this.updateLists();
|
this.updateLists();
|
||||||
for (var i = 0; i < this._components.length; i++) {
|
for (var i = 0; i < this._components.length; i++) {
|
||||||
@@ -2478,6 +2523,13 @@ var MathHelper = (function () {
|
|||||||
MathHelper.maxOf = function (a, b, c, d) {
|
MathHelper.maxOf = function (a, b, c, d) {
|
||||||
return Math.max(a, Math.max(b, Math.max(c, d)));
|
return Math.max(a, Math.max(b, Math.max(c, d)));
|
||||||
};
|
};
|
||||||
|
MathHelper.pointOnCirlce = function (circleCenter, radius, angleInDegrees) {
|
||||||
|
var radians = MathHelper.toRadians(angleInDegrees);
|
||||||
|
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
||||||
|
};
|
||||||
|
MathHelper.Epsilon = 0.00001;
|
||||||
|
MathHelper.Rad2Deg = 57.29578;
|
||||||
|
MathHelper.Deg2Rad = 0.0174532924;
|
||||||
return MathHelper;
|
return MathHelper;
|
||||||
}());
|
}());
|
||||||
var Matrix2D = (function () {
|
var Matrix2D = (function () {
|
||||||
@@ -2780,6 +2832,34 @@ var Vector2 = (function () {
|
|||||||
this.x = x ? x : 0;
|
this.x = x ? x : 0;
|
||||||
this.y = y ? y : this.x;
|
this.y = y ? y : this.x;
|
||||||
}
|
}
|
||||||
|
Object.defineProperty(Vector2, "zero", {
|
||||||
|
get: function () {
|
||||||
|
return Vector2.zeroVector2;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(Vector2, "one", {
|
||||||
|
get: function () {
|
||||||
|
return Vector2.unitVector2;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(Vector2, "unitX", {
|
||||||
|
get: function () {
|
||||||
|
return Vector2.unitXVector;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(Vector2, "unitY", {
|
||||||
|
get: function () {
|
||||||
|
return Vector2.unitYVector;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
Vector2.add = function (value1, value2) {
|
Vector2.add = function (value1, value2) {
|
||||||
var result = new Vector2(0, 0);
|
var result = new Vector2(0, 0);
|
||||||
result.x = value1.x + value2.x;
|
result.x = value1.x + value2.x;
|
||||||
@@ -2835,8 +2915,37 @@ var Vector2 = (function () {
|
|||||||
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||||
return Math.sqrt((v1 * v1) + (v2 * v2));
|
return Math.sqrt((v1 * v1) + (v2 * v2));
|
||||||
};
|
};
|
||||||
|
Vector2.negate = function (value) {
|
||||||
|
var result = new Vector2();
|
||||||
|
result.x = -value.x;
|
||||||
|
result.y = -value.y;
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
Vector2.unitYVector = new Vector2(0, 1);
|
||||||
|
Vector2.unitXVector = new Vector2(1, 0);
|
||||||
|
Vector2.unitVector2 = new Vector2(1, 1);
|
||||||
|
Vector2.zeroVector2 = new Vector2(0, 0);
|
||||||
return Vector2;
|
return Vector2;
|
||||||
}());
|
}());
|
||||||
|
var ColliderTriggerHelper = (function () {
|
||||||
|
function ColliderTriggerHelper() {
|
||||||
|
}
|
||||||
|
ColliderTriggerHelper.prototype.update = function () {
|
||||||
|
var colliders = this._entity.getComponents(Collider);
|
||||||
|
for (var i = 0; i < colliders.length; i++) {
|
||||||
|
var collider = colliders[i];
|
||||||
|
var neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
||||||
|
for (var i_2 = 0; i_2 < neighbors.length; i_2++) {
|
||||||
|
var neighbor = neighbors[i_2];
|
||||||
|
if (!collider.isTrigger && !neighbor.isTrigger)
|
||||||
|
continue;
|
||||||
|
if (collider.overlaps(neighbor)) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return ColliderTriggerHelper;
|
||||||
|
}());
|
||||||
var PointSectors;
|
var PointSectors;
|
||||||
(function (PointSectors) {
|
(function (PointSectors) {
|
||||||
PointSectors[PointSectors["center"] = 0] = "center";
|
PointSectors[PointSectors["center"] = 0] = "center";
|
||||||
@@ -3041,6 +3150,20 @@ var Polygon = (function (_super) {
|
|||||||
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
||||||
this._areEdgeNormalsDirty = true;
|
this._areEdgeNormalsDirty = true;
|
||||||
};
|
};
|
||||||
|
Polygon.prototype.overlaps = function (other) {
|
||||||
|
var result;
|
||||||
|
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");
|
||||||
|
};
|
||||||
Polygon.findPolygonCenter = function (points) {
|
Polygon.findPolygonCenter = function (points) {
|
||||||
var x = 0, y = 0;
|
var x = 0, y = 0;
|
||||||
for (var i = 0; i < points.length; i++) {
|
for (var i = 0; i < points.length; i++) {
|
||||||
@@ -3132,13 +3255,14 @@ var Circle = (function (_super) {
|
|||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
Circle.prototype.pointCollidesWithShape = function (point) {
|
Circle.prototype.pointCollidesWithShape = function (point) {
|
||||||
return ShapeCollisions.pointToCicle(point, this);
|
return ShapeCollisions.pointToCircle(point, this);
|
||||||
};
|
};
|
||||||
Circle.prototype.collidesWithShape = function (other) {
|
Circle.prototype.collidesWithShape = function (other) {
|
||||||
if (other instanceof Box && other.isUnrotated) {
|
if (other instanceof Box && other.isUnrotated) {
|
||||||
return ShapeCollisions.circleToRect(this, other);
|
return ShapeCollisions.circleToBox(this, other);
|
||||||
}
|
}
|
||||||
if (other instanceof Circle) {
|
if (other instanceof Circle) {
|
||||||
|
return ShapeCollisions.circleToCircle(this, other);
|
||||||
}
|
}
|
||||||
if (other instanceof Polygon) {
|
if (other instanceof Polygon) {
|
||||||
return ShapeCollisions.circleToPolygon(this, other);
|
return ShapeCollisions.circleToPolygon(this, other);
|
||||||
@@ -3147,12 +3271,38 @@ var Circle = (function (_super) {
|
|||||||
};
|
};
|
||||||
Circle.prototype.recalculateBounds = function (collider) {
|
Circle.prototype.recalculateBounds = function (collider) {
|
||||||
this.center = collider.localOffset;
|
this.center = collider.localOffset;
|
||||||
|
if (collider.shouldColliderScaleAndRotationWithTransform) {
|
||||||
|
var scale = collider.entity.transform.scale;
|
||||||
|
var hasUnitScale = scale.x == 1 && scale.y == 1;
|
||||||
|
var maxScale = Math.max(scale.x, scale.y);
|
||||||
|
this.radius = this._originalRadius * maxScale;
|
||||||
|
if (collider.entity.transform.rotation != 0) {
|
||||||
|
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
|
||||||
|
var 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.position = Vector2.add(collider.entity.transform.position, this.center);
|
||||||
|
this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
|
||||||
|
};
|
||||||
|
Circle.prototype.overlaps = function (other) {
|
||||||
|
if (other instanceof Box && other.isUnrotated)
|
||||||
|
return Collisions.isRectToCircle(other.bounds, this.position, this.radius);
|
||||||
|
if (other instanceof Circle)
|
||||||
|
return Collisions.isCircleToCircle(this.position, this.radius, other.position, other.radius);
|
||||||
|
if (other instanceof Polygon)
|
||||||
|
return ShapeCollisions.circleToPolygon(this, other);
|
||||||
|
throw new Error("overlaps of circle to " + other + " are not supported");
|
||||||
};
|
};
|
||||||
return Circle;
|
return Circle;
|
||||||
}(Shape));
|
}(Shape));
|
||||||
var CollisionResult = (function () {
|
var CollisionResult = (function () {
|
||||||
function CollisionResult() {
|
function CollisionResult() {
|
||||||
}
|
}
|
||||||
|
CollisionResult.prototype.invertResult = function () {
|
||||||
|
this.minimumTranslationVector = Vector2.negate(this.minimumTranslationVector);
|
||||||
|
this.normal = Vector2.negate(this.normal);
|
||||||
|
};
|
||||||
return CollisionResult;
|
return CollisionResult;
|
||||||
}());
|
}());
|
||||||
var ShapeCollisions = (function () {
|
var ShapeCollisions = (function () {
|
||||||
@@ -3179,9 +3329,36 @@ var ShapeCollisions = (function () {
|
|||||||
var maxA = 0;
|
var maxA = 0;
|
||||||
var maxB = 0;
|
var maxB = 0;
|
||||||
var intervalDist = 0;
|
var intervalDist = 0;
|
||||||
this.getInterval(axis, first, minA, maxA);
|
var ta = this.getInterval(axis, first, minA, maxA);
|
||||||
this.getInterval(axis, second, minB, maxB);
|
minA = ta.min;
|
||||||
|
minB = ta.max;
|
||||||
|
var tb = this.getInterval(axis, second, minB, maxB);
|
||||||
|
minB = tb.min;
|
||||||
|
maxB = tb.max;
|
||||||
|
var relativeIntervalOffset = Vector2.dot(polygonOffset, axis);
|
||||||
|
minA += relativeIntervalOffset;
|
||||||
|
maxA += relativeIntervalOffset;
|
||||||
|
intervalDist = this.intervalDistance(minA, maxA, minB, maxB);
|
||||||
|
if (intervalDist > 0)
|
||||||
|
isIntersecting = false;
|
||||||
|
if (!isIntersecting)
|
||||||
|
return null;
|
||||||
|
intervalDist = Math.abs(intervalDist);
|
||||||
|
if (intervalDist < minIntervalDistance) {
|
||||||
|
minIntervalDistance = intervalDist;
|
||||||
|
translationAxis = axis;
|
||||||
|
if (Vector2.dot(translationAxis, polygonOffset) < 0)
|
||||||
|
translationAxis = new Vector2(-translationAxis);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
result.normal = translationAxis;
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis), new Vector2(minIntervalDistance));
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
|
||||||
|
if (minA < minB)
|
||||||
|
return minB - maxA;
|
||||||
|
return minA - minB;
|
||||||
};
|
};
|
||||||
ShapeCollisions.getInterval = function (axis, polygon, min, max) {
|
ShapeCollisions.getInterval = function (axis, polygon, min, max) {
|
||||||
var dot = Vector2.dot(polygon.points[0], axis);
|
var dot = Vector2.dot(polygon.points[0], axis);
|
||||||
@@ -3195,6 +3372,7 @@ var ShapeCollisions = (function () {
|
|||||||
max = dot;
|
max = dot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return { min: min, max: max };
|
||||||
};
|
};
|
||||||
ShapeCollisions.circleToPolygon = function (circle, polygon) {
|
ShapeCollisions.circleToPolygon = function (circle, polygon) {
|
||||||
var result = new CollisionResult();
|
var result = new CollisionResult();
|
||||||
@@ -3205,30 +3383,47 @@ var ShapeCollisions = (function () {
|
|||||||
result.normal = gpp.edgeNormal;
|
result.normal = gpp.edgeNormal;
|
||||||
var circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
var circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
||||||
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
||||||
return result;
|
return null;
|
||||||
var mtv;
|
var mtv;
|
||||||
if (circleCenterInsidePoly) {
|
if (circleCenterInsidePoly) {
|
||||||
mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius, Math.sqrt(distanceSquared) - circle.radius));
|
mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (distanceSquared == 0) {
|
if (distanceSquared == 0) {
|
||||||
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius));
|
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var distance = Math.sqrt(distanceSquared);
|
var distance = Math.sqrt(distanceSquared);
|
||||||
|
mtv = Vector2.multiply(new Vector2(-Vector2.subtract(poly2Circle, closestPoint)), new Vector2((circle.radius - distanceSquared) / distance));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result.minimumTranslationVector = mtv;
|
||||||
|
result.point = Vector2.add(closestPoint, polygon.position);
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
ShapeCollisions.circleToRect = function (circle, box) {
|
ShapeCollisions.circleToBox = function (circle, box) {
|
||||||
var result = new CollisionResult();
|
var result = new CollisionResult();
|
||||||
var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
||||||
if (box.containsPoint(circle.position)) {
|
if (box.containsPoint(circle.position)) {
|
||||||
result.point = closestPointOnBounds;
|
result.point = closestPointOnBounds;
|
||||||
var safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius, circle.radius)));
|
var safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius)));
|
||||||
|
result.minimumTranslationVector = Vector2.subtract(circle.position, safePlace);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
var sqrDistance = Vector2.distanceSquared(closestPointOnBounds, circle.position);
|
||||||
|
if (sqrDistance == 0) {
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
||||||
|
}
|
||||||
|
else if (sqrDistance <= circle.radius * circle.radius) {
|
||||||
|
result.normal = Vector2.subtract(circle.position, closestPointOnBounds);
|
||||||
|
var depth = result.normal.length() - circle.radius;
|
||||||
|
result.normal = Vector2Ext.normalize(result.normal);
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(depth), result.normal);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
ShapeCollisions.pointToCicle = function (point, circle) {
|
ShapeCollisions.pointToCircle = function (point, circle) {
|
||||||
var result = new CollisionResult();
|
var result = new CollisionResult();
|
||||||
var distanceSquared = Vector2.distanceSquared(point, circle.position);
|
var distanceSquared = Vector2.distanceSquared(point, circle.position);
|
||||||
var sumOfRadii = 1 + circle.radius;
|
var sumOfRadii = 1 + circle.radius;
|
||||||
@@ -3240,7 +3435,7 @@ var ShapeCollisions = (function () {
|
|||||||
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
|
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
return null;
|
||||||
};
|
};
|
||||||
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
|
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
|
||||||
var v = Vector2.subtract(lineB, lineA);
|
var v = Vector2.subtract(lineB, lineA);
|
||||||
@@ -3261,7 +3456,21 @@ var ShapeCollisions = (function () {
|
|||||||
result.point = Vector2.add(closestPoint, poly.position);
|
result.point = Vector2.add(closestPoint, poly.position);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
return null;
|
||||||
|
};
|
||||||
|
ShapeCollisions.circleToCircle = function (first, second) {
|
||||||
|
var result = new CollisionResult();
|
||||||
|
var distanceSquared = Vector2.distanceSquared(first.position, second.position);
|
||||||
|
var sumOfRadii = first.radius + second.radius;
|
||||||
|
var collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||||
|
if (collided) {
|
||||||
|
result.normal = Vector2.normalize(Vector2.subtract(first.position, second.position));
|
||||||
|
var depth = sumOfRadii - Math.sqrt(distanceSquared);
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth), result.normal);
|
||||||
|
result.point = Vector2.add(second.position, Vector2.multiply(result.normal, new Vector2(second.radius)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
return ShapeCollisions;
|
return ShapeCollisions;
|
||||||
}());
|
}());
|
||||||
@@ -3493,6 +3702,16 @@ var Vector2Ext = (function () {
|
|||||||
Vector2Ext.perpendicular = function (first, second) {
|
Vector2Ext.perpendicular = function (first, second) {
|
||||||
return new Vector2(-1 * (second.y - first.y), second.x - first.x);
|
return new Vector2(-1 * (second.y - first.y), second.x - first.x);
|
||||||
};
|
};
|
||||||
|
Vector2Ext.normalize = function (vec) {
|
||||||
|
var magnitude = Math.sqrt((vec.x * vec.x) + (vec.y * vec.y));
|
||||||
|
if (magnitude > MathHelper.Epsilon) {
|
||||||
|
vec = Vector2.divide(vec, new Vector2(magnitude));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
vec.x = vec.y = 0;
|
||||||
|
}
|
||||||
|
return vec;
|
||||||
|
};
|
||||||
return Vector2Ext;
|
return Vector2Ext;
|
||||||
}());
|
}());
|
||||||
var WebGLUtils = (function () {
|
var WebGLUtils = (function () {
|
||||||
|
|||||||
2
demo/libs/framework/framework.min.js
vendored
2
demo/libs/framework/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -107,6 +107,9 @@ class Main extends eui.UILayer {
|
|||||||
player.addComponent(new SpawnComponent(EnemyType.worm));
|
player.addComponent(new SpawnComponent(EnemyType.worm));
|
||||||
player.addComponent(new BoxCollider());
|
player.addComponent(new BoxCollider());
|
||||||
|
|
||||||
|
let player2 = scene.createEntity("player2");
|
||||||
|
player2.addComponent(new BoxCollider());
|
||||||
|
|
||||||
Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
|
Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
|
||||||
console.log("update emitter");
|
console.log("update emitter");
|
||||||
});
|
});
|
||||||
|
|||||||
53
source/bin/framework.d.ts
vendored
53
source/bin/framework.d.ts
vendored
@@ -146,7 +146,7 @@ declare abstract class Component {
|
|||||||
readonly transform: Transform;
|
readonly transform: Transform;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
setEnabled(isEnabled: boolean): this;
|
setEnabled(isEnabled: boolean): this;
|
||||||
abstract initialize(): any;
|
initialize(): void;
|
||||||
onAddedToEntity(): void;
|
onAddedToEntity(): void;
|
||||||
onRemovedFromEntity(): void;
|
onRemovedFromEntity(): void;
|
||||||
onEnabled(): void;
|
onEnabled(): void;
|
||||||
@@ -194,6 +194,7 @@ declare class Entity {
|
|||||||
hasComponent<T extends Component>(type: any): boolean;
|
hasComponent<T extends Component>(type: any): boolean;
|
||||||
getOrCreateComponent<T extends Component>(type: T): T;
|
getOrCreateComponent<T extends Component>(type: T): T;
|
||||||
getComponent<T extends Component>(type: any): T;
|
getComponent<T extends Component>(type: any): T;
|
||||||
|
getComponents<T extends Component>(type: any): T[];
|
||||||
removeComponentForType<T extends Component>(type: any): boolean;
|
removeComponentForType<T extends Component>(type: any): boolean;
|
||||||
removeComponent(component: Component): void;
|
removeComponent(component: Component): void;
|
||||||
removeAllComponents(): void;
|
removeAllComponents(): void;
|
||||||
@@ -376,11 +377,18 @@ declare class SpriteRenderer extends RenderableComponent {
|
|||||||
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
||||||
initialize(): void;
|
initialize(): void;
|
||||||
}
|
}
|
||||||
|
interface ITriggerListener {
|
||||||
|
onTriggerEnter(other: Collider, local: Collider): any;
|
||||||
|
onTriggerExit(other: Collider, local: Collider): any;
|
||||||
|
}
|
||||||
declare abstract class Collider extends Component {
|
declare abstract class Collider extends Component {
|
||||||
shape: Shape;
|
shape: Shape;
|
||||||
physicsLayer: number;
|
physicsLayer: number;
|
||||||
isTrigger: boolean;
|
isTrigger: boolean;
|
||||||
registeredPhysicsBounds: Rectangle;
|
registeredPhysicsBounds: Rectangle;
|
||||||
|
shouldColliderScaleAndRotationWithTransform: boolean;
|
||||||
|
collidesWithLayers: number;
|
||||||
|
_localOffsetLength: number;
|
||||||
protected _isParentEntityAddedToScene: any;
|
protected _isParentEntityAddedToScene: any;
|
||||||
protected _isPositionDirty: boolean;
|
protected _isPositionDirty: boolean;
|
||||||
protected _isRotationDirty: boolean;
|
protected _isRotationDirty: boolean;
|
||||||
@@ -392,7 +400,10 @@ declare abstract class Collider extends Component {
|
|||||||
setLocalOffset(offset: Vector2): void;
|
setLocalOffset(offset: Vector2): void;
|
||||||
registerColliderWithPhysicsSystem(): void;
|
registerColliderWithPhysicsSystem(): void;
|
||||||
unregisterColliderWithPhysicsSystem(): void;
|
unregisterColliderWithPhysicsSystem(): void;
|
||||||
initialize(): void;
|
overlaps(other: Collider): any;
|
||||||
|
onEntityTransformChanged(comp: ComponentTransform): void;
|
||||||
|
onEnabled(): void;
|
||||||
|
onDisabled(): void;
|
||||||
}
|
}
|
||||||
declare class BoxCollider extends Collider {
|
declare class BoxCollider extends Collider {
|
||||||
width: number;
|
width: number;
|
||||||
@@ -460,6 +471,7 @@ declare class ComponentList {
|
|||||||
updateLists(): void;
|
updateLists(): void;
|
||||||
private handleRemove;
|
private handleRemove;
|
||||||
getComponent<T extends Component>(type: any, onlyReturnInitializedComponents: boolean): T;
|
getComponent<T extends Component>(type: any, onlyReturnInitializedComponents: boolean): T;
|
||||||
|
getComponents<T extends Component>(type: any): T[];
|
||||||
update(): void;
|
update(): void;
|
||||||
onEntityTransformChanged(comp: any): void;
|
onEntityTransformChanged(comp: any): void;
|
||||||
}
|
}
|
||||||
@@ -529,6 +541,9 @@ declare class Flags {
|
|||||||
static invertFlags(self: number): number;
|
static invertFlags(self: number): number;
|
||||||
}
|
}
|
||||||
declare class MathHelper {
|
declare class MathHelper {
|
||||||
|
static readonly Epsilon: number;
|
||||||
|
static readonly Rad2Deg: number;
|
||||||
|
static readonly Deg2Rad: number;
|
||||||
static toDegrees(radians: number): number;
|
static toDegrees(radians: number): number;
|
||||||
static toRadians(degrees: number): number;
|
static toRadians(degrees: number): number;
|
||||||
static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number;
|
static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number;
|
||||||
@@ -536,6 +551,7 @@ declare class MathHelper {
|
|||||||
static clamp(value: number, min: number, max: number): number;
|
static clamp(value: number, min: number, max: number): number;
|
||||||
static minOf(a: number, b: number, c: number, d: number): number;
|
static minOf(a: number, b: number, c: number, d: number): number;
|
||||||
static maxOf(a: number, b: number, c: number, d: number): number;
|
static maxOf(a: number, b: number, c: number, d: number): number;
|
||||||
|
static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number): Vector2;
|
||||||
}
|
}
|
||||||
declare class Matrix2D {
|
declare class Matrix2D {
|
||||||
m11: number;
|
m11: number;
|
||||||
@@ -586,6 +602,14 @@ declare class Rectangle {
|
|||||||
declare class Vector2 {
|
declare class Vector2 {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
|
private static readonly unitYVector;
|
||||||
|
private static readonly unitXVector;
|
||||||
|
private static readonly unitVector2;
|
||||||
|
private static readonly zeroVector2;
|
||||||
|
static readonly zero: Vector2;
|
||||||
|
static readonly one: Vector2;
|
||||||
|
static readonly unitX: Vector2;
|
||||||
|
static readonly unitY: Vector2;
|
||||||
constructor(x?: number, y?: number);
|
constructor(x?: number, y?: number);
|
||||||
static add(value1: Vector2, value2: Vector2): Vector2;
|
static add(value1: Vector2, value2: Vector2): Vector2;
|
||||||
static divide(value1: Vector2, value2: Vector2): Vector2;
|
static divide(value1: Vector2, value2: Vector2): Vector2;
|
||||||
@@ -599,6 +623,11 @@ declare class Vector2 {
|
|||||||
static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2;
|
static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2;
|
||||||
static transform(position: Vector2, matrix: Matrix2D): Vector2;
|
static transform(position: Vector2, matrix: Matrix2D): Vector2;
|
||||||
static distance(value1: Vector2, value2: Vector2): number;
|
static distance(value1: Vector2, value2: Vector2): number;
|
||||||
|
static negate(value: Vector2): Vector2;
|
||||||
|
}
|
||||||
|
declare class ColliderTriggerHelper {
|
||||||
|
private _entity;
|
||||||
|
update(): void;
|
||||||
}
|
}
|
||||||
declare enum PointSectors {
|
declare enum PointSectors {
|
||||||
center = 0,
|
center = 0,
|
||||||
@@ -638,6 +667,7 @@ declare abstract class Shape {
|
|||||||
center: Vector2;
|
center: Vector2;
|
||||||
abstract recalculateBounds(collider: Collider): any;
|
abstract recalculateBounds(collider: Collider): any;
|
||||||
abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
|
abstract overlaps(other: Shape): any;
|
||||||
}
|
}
|
||||||
declare class Polygon extends Shape {
|
declare class Polygon extends Shape {
|
||||||
points: Vector2[];
|
points: Vector2[];
|
||||||
@@ -651,8 +681,9 @@ declare class Polygon extends Shape {
|
|||||||
constructor(vertCount: number, radius: number);
|
constructor(vertCount: number, radius: number);
|
||||||
private buildEdgeNormals;
|
private buildEdgeNormals;
|
||||||
setPoints(points: Vector2[]): void;
|
setPoints(points: Vector2[]): void;
|
||||||
collidesWithShape(other: Shape): void;
|
collidesWithShape(other: Shape): CollisionResult;
|
||||||
recalculateCenterAndEdgeNormals(): void;
|
recalculateCenterAndEdgeNormals(): void;
|
||||||
|
overlaps(other: Shape): any;
|
||||||
static findPolygonCenter(points: Vector2[]): Vector2;
|
static findPolygonCenter(points: Vector2[]): Vector2;
|
||||||
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): {
|
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): {
|
||||||
closestPoint: any;
|
closestPoint: any;
|
||||||
@@ -677,20 +708,27 @@ declare class Circle extends Shape {
|
|||||||
pointCollidesWithShape(point: Vector2): CollisionResult;
|
pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
collidesWithShape(other: Shape): CollisionResult;
|
collidesWithShape(other: Shape): CollisionResult;
|
||||||
recalculateBounds(collider: Collider): void;
|
recalculateBounds(collider: Collider): void;
|
||||||
|
overlaps(other: Shape): any;
|
||||||
}
|
}
|
||||||
declare class CollisionResult {
|
declare class CollisionResult {
|
||||||
minimumTranslationVector: Vector2;
|
minimumTranslationVector: Vector2;
|
||||||
normal: Vector2;
|
normal: Vector2;
|
||||||
point: Vector2;
|
point: Vector2;
|
||||||
|
invertResult(): void;
|
||||||
}
|
}
|
||||||
declare class ShapeCollisions {
|
declare class ShapeCollisions {
|
||||||
static polygonToPolygon(first: Polygon, second: Polygon): void;
|
static polygonToPolygon(first: Polygon, second: Polygon): CollisionResult;
|
||||||
static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): void;
|
static intervalDistance(minA: number, maxA: number, minB: number, maxB: any): number;
|
||||||
|
static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): {
|
||||||
|
min: number;
|
||||||
|
max: number;
|
||||||
|
};
|
||||||
static circleToPolygon(circle: Circle, polygon: Polygon): CollisionResult;
|
static circleToPolygon(circle: Circle, polygon: Polygon): CollisionResult;
|
||||||
static circleToRect(circle: Circle, box: Box): CollisionResult;
|
static circleToBox(circle: Circle, box: Box): CollisionResult;
|
||||||
static pointToCicle(point: Vector2, circle: Circle): CollisionResult;
|
static pointToCircle(point: Vector2, circle: Circle): CollisionResult;
|
||||||
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
|
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
|
||||||
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
|
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
|
||||||
|
static circleToCircle(first: Circle, second: Circle): CollisionResult;
|
||||||
}
|
}
|
||||||
declare class SpatialHash {
|
declare class SpatialHash {
|
||||||
gridBounds: Rectangle;
|
gridBounds: Rectangle;
|
||||||
@@ -738,6 +776,7 @@ declare class Vector2Ext {
|
|||||||
static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean;
|
static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean;
|
||||||
static cross(u: Vector2, v: Vector2): number;
|
static cross(u: Vector2, v: Vector2): number;
|
||||||
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
||||||
|
static normalize(vec: Vector2): Vector2;
|
||||||
}
|
}
|
||||||
declare class WebGLUtils {
|
declare class WebGLUtils {
|
||||||
static getWebGL(): WebGLRenderingContext;
|
static getWebGL(): WebGLRenderingContext;
|
||||||
|
|||||||
@@ -771,6 +771,8 @@ var Component = (function () {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
Component.prototype.initialize = function () {
|
||||||
|
};
|
||||||
Component.prototype.onAddedToEntity = function () {
|
Component.prototype.onAddedToEntity = function () {
|
||||||
};
|
};
|
||||||
Component.prototype.onRemovedFromEntity = function () {
|
Component.prototype.onRemovedFromEntity = function () {
|
||||||
@@ -1011,6 +1013,9 @@ var Entity = (function () {
|
|||||||
Entity.prototype.getComponent = function (type) {
|
Entity.prototype.getComponent = function (type) {
|
||||||
return this.components.getComponent(type, false);
|
return this.components.getComponent(type, false);
|
||||||
};
|
};
|
||||||
|
Entity.prototype.getComponents = function (type) {
|
||||||
|
return this.components.getComponents(type);
|
||||||
|
};
|
||||||
Entity.prototype.removeComponentForType = function (type) {
|
Entity.prototype.removeComponentForType = function (type) {
|
||||||
var comp = this.getComponent(type);
|
var comp = this.getComponent(type);
|
||||||
if (comp) {
|
if (comp) {
|
||||||
@@ -1786,6 +1791,8 @@ var Collider = (function (_super) {
|
|||||||
function Collider() {
|
function Collider() {
|
||||||
var _this = _super !== null && _super.apply(this, arguments) || this;
|
var _this = _super !== null && _super.apply(this, arguments) || this;
|
||||||
_this.physicsLayer = 1 << 0;
|
_this.physicsLayer = 1 << 0;
|
||||||
|
_this.shouldColliderScaleAndRotationWithTransform = true;
|
||||||
|
_this.collidesWithLayers = Physics.allLayers;
|
||||||
_this._isPositionDirty = true;
|
_this._isPositionDirty = true;
|
||||||
_this._isRotationDirty = true;
|
_this._isRotationDirty = true;
|
||||||
return _this;
|
return _this;
|
||||||
@@ -1815,6 +1822,7 @@ var Collider = (function (_super) {
|
|||||||
if (this._localOffset != offset) {
|
if (this._localOffset != offset) {
|
||||||
this.unregisterColliderWithPhysicsSystem();
|
this.unregisterColliderWithPhysicsSystem();
|
||||||
this._localOffset = offset;
|
this._localOffset = offset;
|
||||||
|
this._localOffsetLength = this._localOffset.length();
|
||||||
this._isPositionDirty = true;
|
this._isPositionDirty = true;
|
||||||
this.registerColliderWithPhysicsSystem();
|
this.registerColliderWithPhysicsSystem();
|
||||||
}
|
}
|
||||||
@@ -1831,7 +1839,30 @@ var Collider = (function (_super) {
|
|||||||
}
|
}
|
||||||
this._isColliderRegisterd = false;
|
this._isColliderRegisterd = false;
|
||||||
};
|
};
|
||||||
Collider.prototype.initialize = function () {
|
Collider.prototype.overlaps = function (other) {
|
||||||
|
return this.shape.overlaps(other.shape);
|
||||||
|
};
|
||||||
|
Collider.prototype.onEntityTransformChanged = function (comp) {
|
||||||
|
switch (comp) {
|
||||||
|
case ComponentTransform.position:
|
||||||
|
this._isPositionDirty = true;
|
||||||
|
break;
|
||||||
|
case ComponentTransform.scale:
|
||||||
|
this._isPositionDirty = true;
|
||||||
|
break;
|
||||||
|
case ComponentTransform.rotation:
|
||||||
|
this._isRotationDirty = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (this._isColliderRegisterd)
|
||||||
|
Physics.updateCollider(this);
|
||||||
|
};
|
||||||
|
Collider.prototype.onEnabled = function () {
|
||||||
|
this.registerColliderWithPhysicsSystem();
|
||||||
|
this._isPositionDirty = this._isRotationDirty = true;
|
||||||
|
};
|
||||||
|
Collider.prototype.onDisabled = function () {
|
||||||
|
this.unregisterColliderWithPhysicsSystem();
|
||||||
};
|
};
|
||||||
return Collider;
|
return Collider;
|
||||||
}(Component));
|
}(Component));
|
||||||
@@ -2177,6 +2208,20 @@ var ComponentList = (function () {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
ComponentList.prototype.getComponents = function (type) {
|
||||||
|
var components = [];
|
||||||
|
for (var i = 0; i < this._components.length; i++) {
|
||||||
|
var component = this._components[i];
|
||||||
|
if (component instanceof type)
|
||||||
|
components.push(components);
|
||||||
|
}
|
||||||
|
for (var i = 0; i < this._componentsToAdd.length; i++) {
|
||||||
|
var component = this._componentsToAdd[i];
|
||||||
|
if (component instanceof type)
|
||||||
|
components.push(components);
|
||||||
|
}
|
||||||
|
return components;
|
||||||
|
};
|
||||||
ComponentList.prototype.update = function () {
|
ComponentList.prototype.update = function () {
|
||||||
this.updateLists();
|
this.updateLists();
|
||||||
for (var i = 0; i < this._components.length; i++) {
|
for (var i = 0; i < this._components.length; i++) {
|
||||||
@@ -2478,6 +2523,13 @@ var MathHelper = (function () {
|
|||||||
MathHelper.maxOf = function (a, b, c, d) {
|
MathHelper.maxOf = function (a, b, c, d) {
|
||||||
return Math.max(a, Math.max(b, Math.max(c, d)));
|
return Math.max(a, Math.max(b, Math.max(c, d)));
|
||||||
};
|
};
|
||||||
|
MathHelper.pointOnCirlce = function (circleCenter, radius, angleInDegrees) {
|
||||||
|
var radians = MathHelper.toRadians(angleInDegrees);
|
||||||
|
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
||||||
|
};
|
||||||
|
MathHelper.Epsilon = 0.00001;
|
||||||
|
MathHelper.Rad2Deg = 57.29578;
|
||||||
|
MathHelper.Deg2Rad = 0.0174532924;
|
||||||
return MathHelper;
|
return MathHelper;
|
||||||
}());
|
}());
|
||||||
var Matrix2D = (function () {
|
var Matrix2D = (function () {
|
||||||
@@ -2780,6 +2832,34 @@ var Vector2 = (function () {
|
|||||||
this.x = x ? x : 0;
|
this.x = x ? x : 0;
|
||||||
this.y = y ? y : this.x;
|
this.y = y ? y : this.x;
|
||||||
}
|
}
|
||||||
|
Object.defineProperty(Vector2, "zero", {
|
||||||
|
get: function () {
|
||||||
|
return Vector2.zeroVector2;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(Vector2, "one", {
|
||||||
|
get: function () {
|
||||||
|
return Vector2.unitVector2;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(Vector2, "unitX", {
|
||||||
|
get: function () {
|
||||||
|
return Vector2.unitXVector;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(Vector2, "unitY", {
|
||||||
|
get: function () {
|
||||||
|
return Vector2.unitYVector;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
Vector2.add = function (value1, value2) {
|
Vector2.add = function (value1, value2) {
|
||||||
var result = new Vector2(0, 0);
|
var result = new Vector2(0, 0);
|
||||||
result.x = value1.x + value2.x;
|
result.x = value1.x + value2.x;
|
||||||
@@ -2835,8 +2915,37 @@ var Vector2 = (function () {
|
|||||||
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||||
return Math.sqrt((v1 * v1) + (v2 * v2));
|
return Math.sqrt((v1 * v1) + (v2 * v2));
|
||||||
};
|
};
|
||||||
|
Vector2.negate = function (value) {
|
||||||
|
var result = new Vector2();
|
||||||
|
result.x = -value.x;
|
||||||
|
result.y = -value.y;
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
Vector2.unitYVector = new Vector2(0, 1);
|
||||||
|
Vector2.unitXVector = new Vector2(1, 0);
|
||||||
|
Vector2.unitVector2 = new Vector2(1, 1);
|
||||||
|
Vector2.zeroVector2 = new Vector2(0, 0);
|
||||||
return Vector2;
|
return Vector2;
|
||||||
}());
|
}());
|
||||||
|
var ColliderTriggerHelper = (function () {
|
||||||
|
function ColliderTriggerHelper() {
|
||||||
|
}
|
||||||
|
ColliderTriggerHelper.prototype.update = function () {
|
||||||
|
var colliders = this._entity.getComponents(Collider);
|
||||||
|
for (var i = 0; i < colliders.length; i++) {
|
||||||
|
var collider = colliders[i];
|
||||||
|
var neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
||||||
|
for (var i_2 = 0; i_2 < neighbors.length; i_2++) {
|
||||||
|
var neighbor = neighbors[i_2];
|
||||||
|
if (!collider.isTrigger && !neighbor.isTrigger)
|
||||||
|
continue;
|
||||||
|
if (collider.overlaps(neighbor)) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return ColliderTriggerHelper;
|
||||||
|
}());
|
||||||
var PointSectors;
|
var PointSectors;
|
||||||
(function (PointSectors) {
|
(function (PointSectors) {
|
||||||
PointSectors[PointSectors["center"] = 0] = "center";
|
PointSectors[PointSectors["center"] = 0] = "center";
|
||||||
@@ -3041,6 +3150,20 @@ var Polygon = (function (_super) {
|
|||||||
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
||||||
this._areEdgeNormalsDirty = true;
|
this._areEdgeNormalsDirty = true;
|
||||||
};
|
};
|
||||||
|
Polygon.prototype.overlaps = function (other) {
|
||||||
|
var result;
|
||||||
|
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");
|
||||||
|
};
|
||||||
Polygon.findPolygonCenter = function (points) {
|
Polygon.findPolygonCenter = function (points) {
|
||||||
var x = 0, y = 0;
|
var x = 0, y = 0;
|
||||||
for (var i = 0; i < points.length; i++) {
|
for (var i = 0; i < points.length; i++) {
|
||||||
@@ -3132,13 +3255,14 @@ var Circle = (function (_super) {
|
|||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
Circle.prototype.pointCollidesWithShape = function (point) {
|
Circle.prototype.pointCollidesWithShape = function (point) {
|
||||||
return ShapeCollisions.pointToCicle(point, this);
|
return ShapeCollisions.pointToCircle(point, this);
|
||||||
};
|
};
|
||||||
Circle.prototype.collidesWithShape = function (other) {
|
Circle.prototype.collidesWithShape = function (other) {
|
||||||
if (other instanceof Box && other.isUnrotated) {
|
if (other instanceof Box && other.isUnrotated) {
|
||||||
return ShapeCollisions.circleToRect(this, other);
|
return ShapeCollisions.circleToBox(this, other);
|
||||||
}
|
}
|
||||||
if (other instanceof Circle) {
|
if (other instanceof Circle) {
|
||||||
|
return ShapeCollisions.circleToCircle(this, other);
|
||||||
}
|
}
|
||||||
if (other instanceof Polygon) {
|
if (other instanceof Polygon) {
|
||||||
return ShapeCollisions.circleToPolygon(this, other);
|
return ShapeCollisions.circleToPolygon(this, other);
|
||||||
@@ -3147,12 +3271,38 @@ var Circle = (function (_super) {
|
|||||||
};
|
};
|
||||||
Circle.prototype.recalculateBounds = function (collider) {
|
Circle.prototype.recalculateBounds = function (collider) {
|
||||||
this.center = collider.localOffset;
|
this.center = collider.localOffset;
|
||||||
|
if (collider.shouldColliderScaleAndRotationWithTransform) {
|
||||||
|
var scale = collider.entity.transform.scale;
|
||||||
|
var hasUnitScale = scale.x == 1 && scale.y == 1;
|
||||||
|
var maxScale = Math.max(scale.x, scale.y);
|
||||||
|
this.radius = this._originalRadius * maxScale;
|
||||||
|
if (collider.entity.transform.rotation != 0) {
|
||||||
|
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
|
||||||
|
var 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.position = Vector2.add(collider.entity.transform.position, this.center);
|
||||||
|
this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
|
||||||
|
};
|
||||||
|
Circle.prototype.overlaps = function (other) {
|
||||||
|
if (other instanceof Box && other.isUnrotated)
|
||||||
|
return Collisions.isRectToCircle(other.bounds, this.position, this.radius);
|
||||||
|
if (other instanceof Circle)
|
||||||
|
return Collisions.isCircleToCircle(this.position, this.radius, other.position, other.radius);
|
||||||
|
if (other instanceof Polygon)
|
||||||
|
return ShapeCollisions.circleToPolygon(this, other);
|
||||||
|
throw new Error("overlaps of circle to " + other + " are not supported");
|
||||||
};
|
};
|
||||||
return Circle;
|
return Circle;
|
||||||
}(Shape));
|
}(Shape));
|
||||||
var CollisionResult = (function () {
|
var CollisionResult = (function () {
|
||||||
function CollisionResult() {
|
function CollisionResult() {
|
||||||
}
|
}
|
||||||
|
CollisionResult.prototype.invertResult = function () {
|
||||||
|
this.minimumTranslationVector = Vector2.negate(this.minimumTranslationVector);
|
||||||
|
this.normal = Vector2.negate(this.normal);
|
||||||
|
};
|
||||||
return CollisionResult;
|
return CollisionResult;
|
||||||
}());
|
}());
|
||||||
var ShapeCollisions = (function () {
|
var ShapeCollisions = (function () {
|
||||||
@@ -3179,9 +3329,36 @@ var ShapeCollisions = (function () {
|
|||||||
var maxA = 0;
|
var maxA = 0;
|
||||||
var maxB = 0;
|
var maxB = 0;
|
||||||
var intervalDist = 0;
|
var intervalDist = 0;
|
||||||
this.getInterval(axis, first, minA, maxA);
|
var ta = this.getInterval(axis, first, minA, maxA);
|
||||||
this.getInterval(axis, second, minB, maxB);
|
minA = ta.min;
|
||||||
|
minB = ta.max;
|
||||||
|
var tb = this.getInterval(axis, second, minB, maxB);
|
||||||
|
minB = tb.min;
|
||||||
|
maxB = tb.max;
|
||||||
|
var relativeIntervalOffset = Vector2.dot(polygonOffset, axis);
|
||||||
|
minA += relativeIntervalOffset;
|
||||||
|
maxA += relativeIntervalOffset;
|
||||||
|
intervalDist = this.intervalDistance(minA, maxA, minB, maxB);
|
||||||
|
if (intervalDist > 0)
|
||||||
|
isIntersecting = false;
|
||||||
|
if (!isIntersecting)
|
||||||
|
return null;
|
||||||
|
intervalDist = Math.abs(intervalDist);
|
||||||
|
if (intervalDist < minIntervalDistance) {
|
||||||
|
minIntervalDistance = intervalDist;
|
||||||
|
translationAxis = axis;
|
||||||
|
if (Vector2.dot(translationAxis, polygonOffset) < 0)
|
||||||
|
translationAxis = new Vector2(-translationAxis);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
result.normal = translationAxis;
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis), new Vector2(minIntervalDistance));
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
|
||||||
|
if (minA < minB)
|
||||||
|
return minB - maxA;
|
||||||
|
return minA - minB;
|
||||||
};
|
};
|
||||||
ShapeCollisions.getInterval = function (axis, polygon, min, max) {
|
ShapeCollisions.getInterval = function (axis, polygon, min, max) {
|
||||||
var dot = Vector2.dot(polygon.points[0], axis);
|
var dot = Vector2.dot(polygon.points[0], axis);
|
||||||
@@ -3195,6 +3372,7 @@ var ShapeCollisions = (function () {
|
|||||||
max = dot;
|
max = dot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return { min: min, max: max };
|
||||||
};
|
};
|
||||||
ShapeCollisions.circleToPolygon = function (circle, polygon) {
|
ShapeCollisions.circleToPolygon = function (circle, polygon) {
|
||||||
var result = new CollisionResult();
|
var result = new CollisionResult();
|
||||||
@@ -3205,30 +3383,47 @@ var ShapeCollisions = (function () {
|
|||||||
result.normal = gpp.edgeNormal;
|
result.normal = gpp.edgeNormal;
|
||||||
var circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
var circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
||||||
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
||||||
return result;
|
return null;
|
||||||
var mtv;
|
var mtv;
|
||||||
if (circleCenterInsidePoly) {
|
if (circleCenterInsidePoly) {
|
||||||
mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius, Math.sqrt(distanceSquared) - circle.radius));
|
mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (distanceSquared == 0) {
|
if (distanceSquared == 0) {
|
||||||
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius));
|
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var distance = Math.sqrt(distanceSquared);
|
var distance = Math.sqrt(distanceSquared);
|
||||||
|
mtv = Vector2.multiply(new Vector2(-Vector2.subtract(poly2Circle, closestPoint)), new Vector2((circle.radius - distanceSquared) / distance));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result.minimumTranslationVector = mtv;
|
||||||
|
result.point = Vector2.add(closestPoint, polygon.position);
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
ShapeCollisions.circleToRect = function (circle, box) {
|
ShapeCollisions.circleToBox = function (circle, box) {
|
||||||
var result = new CollisionResult();
|
var result = new CollisionResult();
|
||||||
var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
||||||
if (box.containsPoint(circle.position)) {
|
if (box.containsPoint(circle.position)) {
|
||||||
result.point = closestPointOnBounds;
|
result.point = closestPointOnBounds;
|
||||||
var safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius, circle.radius)));
|
var safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius)));
|
||||||
|
result.minimumTranslationVector = Vector2.subtract(circle.position, safePlace);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
var sqrDistance = Vector2.distanceSquared(closestPointOnBounds, circle.position);
|
||||||
|
if (sqrDistance == 0) {
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
||||||
|
}
|
||||||
|
else if (sqrDistance <= circle.radius * circle.radius) {
|
||||||
|
result.normal = Vector2.subtract(circle.position, closestPointOnBounds);
|
||||||
|
var depth = result.normal.length() - circle.radius;
|
||||||
|
result.normal = Vector2Ext.normalize(result.normal);
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(depth), result.normal);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
ShapeCollisions.pointToCicle = function (point, circle) {
|
ShapeCollisions.pointToCircle = function (point, circle) {
|
||||||
var result = new CollisionResult();
|
var result = new CollisionResult();
|
||||||
var distanceSquared = Vector2.distanceSquared(point, circle.position);
|
var distanceSquared = Vector2.distanceSquared(point, circle.position);
|
||||||
var sumOfRadii = 1 + circle.radius;
|
var sumOfRadii = 1 + circle.radius;
|
||||||
@@ -3240,7 +3435,7 @@ var ShapeCollisions = (function () {
|
|||||||
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
|
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
return null;
|
||||||
};
|
};
|
||||||
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
|
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
|
||||||
var v = Vector2.subtract(lineB, lineA);
|
var v = Vector2.subtract(lineB, lineA);
|
||||||
@@ -3261,7 +3456,21 @@ var ShapeCollisions = (function () {
|
|||||||
result.point = Vector2.add(closestPoint, poly.position);
|
result.point = Vector2.add(closestPoint, poly.position);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
return null;
|
||||||
|
};
|
||||||
|
ShapeCollisions.circleToCircle = function (first, second) {
|
||||||
|
var result = new CollisionResult();
|
||||||
|
var distanceSquared = Vector2.distanceSquared(first.position, second.position);
|
||||||
|
var sumOfRadii = first.radius + second.radius;
|
||||||
|
var collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||||
|
if (collided) {
|
||||||
|
result.normal = Vector2.normalize(Vector2.subtract(first.position, second.position));
|
||||||
|
var depth = sumOfRadii - Math.sqrt(distanceSquared);
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth), result.normal);
|
||||||
|
result.point = Vector2.add(second.position, Vector2.multiply(result.normal, new Vector2(second.radius)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
return ShapeCollisions;
|
return ShapeCollisions;
|
||||||
}());
|
}());
|
||||||
@@ -3493,6 +3702,16 @@ var Vector2Ext = (function () {
|
|||||||
Vector2Ext.perpendicular = function (first, second) {
|
Vector2Ext.perpendicular = function (first, second) {
|
||||||
return new Vector2(-1 * (second.y - first.y), second.x - first.x);
|
return new Vector2(-1 * (second.y - first.y), second.x - first.x);
|
||||||
};
|
};
|
||||||
|
Vector2Ext.normalize = function (vec) {
|
||||||
|
var magnitude = Math.sqrt((vec.x * vec.x) + (vec.y * vec.y));
|
||||||
|
if (magnitude > MathHelper.Epsilon) {
|
||||||
|
vec = Vector2.divide(vec, new Vector2(magnitude));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
vec.x = vec.y = 0;
|
||||||
|
}
|
||||||
|
return vec;
|
||||||
|
};
|
||||||
return Vector2Ext;
|
return Vector2Ext;
|
||||||
}());
|
}());
|
||||||
var WebGLUtils = (function () {
|
var WebGLUtils = (function () {
|
||||||
|
|||||||
2
source/bin/framework.min.js
vendored
2
source/bin/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -29,7 +29,9 @@ abstract class Component {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract initialize();
|
public initialize(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public onAddedToEntity(){
|
public onAddedToEntity(){
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,10 @@ abstract class Collider extends Component{
|
|||||||
public physicsLayer = 1 << 0;
|
public physicsLayer = 1 << 0;
|
||||||
public isTrigger: boolean;
|
public isTrigger: boolean;
|
||||||
public registeredPhysicsBounds: Rectangle;
|
public registeredPhysicsBounds: Rectangle;
|
||||||
|
public shouldColliderScaleAndRotationWithTransform = true;
|
||||||
|
public collidesWithLayers = Physics.allLayers;
|
||||||
|
|
||||||
|
public _localOffsetLength: number;
|
||||||
protected _isParentEntityAddedToScene;
|
protected _isParentEntityAddedToScene;
|
||||||
protected _isPositionDirty = true;
|
protected _isPositionDirty = true;
|
||||||
protected _isRotationDirty = true;
|
protected _isRotationDirty = true;
|
||||||
@@ -32,6 +35,7 @@ abstract class Collider extends Component{
|
|||||||
if (this._localOffset != offset){
|
if (this._localOffset != offset){
|
||||||
this.unregisterColliderWithPhysicsSystem();
|
this.unregisterColliderWithPhysicsSystem();
|
||||||
this._localOffset = offset;
|
this._localOffset = offset;
|
||||||
|
this._localOffsetLength = this._localOffset.length();
|
||||||
this._isPositionDirty = true;
|
this._isPositionDirty = true;
|
||||||
this.registerColliderWithPhysicsSystem();
|
this.registerColliderWithPhysicsSystem();
|
||||||
}
|
}
|
||||||
@@ -51,6 +55,33 @@ abstract class Collider extends Component{
|
|||||||
this._isColliderRegisterd = false;
|
this._isColliderRegisterd = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public initialize() {
|
public overlaps(other: Collider){
|
||||||
|
return this.shape.overlaps(other.shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
public onEntityTransformChanged(comp: ComponentTransform){
|
||||||
|
switch (comp){
|
||||||
|
case ComponentTransform.position:
|
||||||
|
this._isPositionDirty = true;
|
||||||
|
break;
|
||||||
|
case ComponentTransform.scale:
|
||||||
|
this._isPositionDirty = true;
|
||||||
|
break;
|
||||||
|
case ComponentTransform.rotation:
|
||||||
|
this._isRotationDirty = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._isColliderRegisterd)
|
||||||
|
Physics.updateCollider(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public onEnabled(){
|
||||||
|
this.registerColliderWithPhysicsSystem();
|
||||||
|
this._isPositionDirty = this._isRotationDirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public onDisabled(){
|
||||||
|
this.unregisterColliderWithPhysicsSystem();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
4
source/src/ECS/Components/Physics/ITriggerListener.ts
Normal file
4
source/src/ECS/Components/Physics/ITriggerListener.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
interface ITriggerListener {
|
||||||
|
onTriggerEnter(other: Collider, local: Collider);
|
||||||
|
onTriggerExit(other: Collider, local: Collider);
|
||||||
|
}
|
||||||
@@ -212,6 +212,10 @@ class Entity {
|
|||||||
return this.components.getComponent(type, false) as T;
|
return this.components.getComponent(type, false) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getComponents<T extends Component>(type): T[]{
|
||||||
|
return this.components.getComponents<T>(type);
|
||||||
|
}
|
||||||
|
|
||||||
public removeComponentForType<T extends Component>(type){
|
public removeComponentForType<T extends Component>(type){
|
||||||
let comp = this.getComponent<T>(type);
|
let comp = this.getComponent<T>(type);
|
||||||
if (comp){
|
if (comp){
|
||||||
|
|||||||
@@ -120,6 +120,23 @@ class ComponentList {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getComponents<T extends Component>(type): T[]{
|
||||||
|
let components = [];
|
||||||
|
for (let i = 0; i < this._components.length; i ++){
|
||||||
|
let component = this._components[i];
|
||||||
|
if (component instanceof type)
|
||||||
|
components.push(components);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < this._componentsToAdd.length; i ++){
|
||||||
|
let component = this._componentsToAdd[i];
|
||||||
|
if (component instanceof type)
|
||||||
|
components.push(components);
|
||||||
|
}
|
||||||
|
|
||||||
|
return components;
|
||||||
|
}
|
||||||
|
|
||||||
public update(){
|
public update(){
|
||||||
this.updateLists();
|
this.updateLists();
|
||||||
for (let i = 0; i < this._components.length; i ++){
|
for (let i = 0; i < this._components.length; i ++){
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
class MathHelper {
|
class MathHelper {
|
||||||
|
public static readonly Epsilon: number = 0.00001;
|
||||||
|
public static readonly Rad2Deg = 57.29578;
|
||||||
|
public static readonly Deg2Rad = 0.0174532924;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将弧度转换成角度。
|
* 将弧度转换成角度。
|
||||||
* @param radians 用弧度表示的角
|
* @param radians 用弧度表示的角
|
||||||
@@ -48,4 +52,9 @@ class MathHelper {
|
|||||||
public static maxOf(a: number, b: number, c: number, d: number){
|
public static maxOf(a: number, b: number, c: number, d: number){
|
||||||
return Math.max(a, Math.max(b, Math.max(c, d)));
|
return Math.max(a, Math.max(b, Math.max(c, d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number){
|
||||||
|
let radians = MathHelper.toRadians(angleInDegrees);
|
||||||
|
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,26 @@ class Vector2 {
|
|||||||
public x: number = 0;
|
public x: number = 0;
|
||||||
public y: number = 0;
|
public y: number = 0;
|
||||||
|
|
||||||
|
private static readonly unitYVector = new Vector2(0, 1);
|
||||||
|
private static readonly unitXVector = new Vector2(1, 0);
|
||||||
|
private static readonly unitVector2 = new Vector2(1, 1);
|
||||||
|
private static readonly zeroVector2 = new Vector2(0, 0);
|
||||||
|
public static get zero(){
|
||||||
|
return Vector2.zeroVector2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static get one(){
|
||||||
|
return Vector2.unitVector2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static get unitX(){
|
||||||
|
return Vector2.unitXVector;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static get unitY(){
|
||||||
|
return Vector2.unitYVector;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从两个值构造一个带有X和Y的二维向量。
|
* 从两个值构造一个带有X和Y的二维向量。
|
||||||
* @param x 二维空间中的x坐标
|
* @param x 二维空间中的x坐标
|
||||||
@@ -90,4 +110,12 @@ class Vector2 {
|
|||||||
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||||
return Math.sqrt((v1 * v1) + (v2 * v2));
|
return Math.sqrt((v1 * v1) + (v2 * v2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static negate(value: Vector2){
|
||||||
|
let result: Vector2 = new Vector2();
|
||||||
|
result.x = -value.x;
|
||||||
|
result.y = -value.y;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
21
source/src/Physics/ColliderTriggerHelper.ts
Normal file
21
source/src/Physics/ColliderTriggerHelper.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
class ColliderTriggerHelper {
|
||||||
|
private _entity: Entity;
|
||||||
|
|
||||||
|
public update(){
|
||||||
|
let colliders = this._entity.getComponents<Collider>(Collider);
|
||||||
|
for (let i = 0; i < colliders.length; i ++){
|
||||||
|
let collider = colliders[i];
|
||||||
|
|
||||||
|
let neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
||||||
|
for (let i = 0; i < neighbors.length; i ++){
|
||||||
|
let neighbor = neighbors[i];
|
||||||
|
if (!collider.isTrigger && !neighbor.isTrigger)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (collider.overlaps(neighbor)){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,26 +3,26 @@ class Circle extends Shape {
|
|||||||
public radius: number;
|
public radius: number;
|
||||||
private _originalRadius: number;
|
private _originalRadius: number;
|
||||||
|
|
||||||
constructor(radius: number){
|
constructor(radius: number) {
|
||||||
super();
|
super();
|
||||||
this.radius = radius;
|
this.radius = radius;
|
||||||
this._originalRadius = radius;
|
this._originalRadius = radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
||||||
return ShapeCollisions.pointToCicle(point, this);
|
return ShapeCollisions.pointToCircle(point, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public collidesWithShape(other: Shape): CollisionResult{
|
public collidesWithShape(other: Shape): CollisionResult {
|
||||||
if (other instanceof Box && (other as Box).isUnrotated){
|
if (other instanceof Box && (other as Box).isUnrotated) {
|
||||||
return ShapeCollisions.circleToRect(this, other as Box);
|
return ShapeCollisions.circleToBox(this, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (other instanceof Circle){
|
if (other instanceof Circle) {
|
||||||
// TODO CIRCLETOCIRCLE
|
return ShapeCollisions.circleToCircle(this, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (other instanceof Polygon){
|
if (other instanceof Polygon) {
|
||||||
return ShapeCollisions.circleToPolygon(this, other);
|
return ShapeCollisions.circleToPolygon(this, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,5 +31,34 @@ class Circle extends Shape {
|
|||||||
|
|
||||||
public recalculateBounds(collider: Collider) {
|
public recalculateBounds(collider: Collider) {
|
||||||
this.center = collider.localOffset;
|
this.center = collider.localOffset;
|
||||||
|
|
||||||
|
if (collider.shouldColliderScaleAndRotationWithTransform) {
|
||||||
|
let scale = collider.entity.transform.scale;
|
||||||
|
let hasUnitScale = scale.x == 1 && scale.y == 1;
|
||||||
|
let maxScale = Math.max(scale.x, scale.y);
|
||||||
|
this.radius = this._originalRadius * maxScale;
|
||||||
|
|
||||||
|
if (collider.entity.transform.rotation != 0) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.position = Vector2.add(collider.entity.transform.position, this.center);
|
||||||
|
this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public overlaps(other: Shape){
|
||||||
|
if (other instanceof Box && (other as Box).isUnrotated)
|
||||||
|
return Collisions.isRectToCircle(other.bounds, this.position, this.radius);
|
||||||
|
|
||||||
|
if (other instanceof Circle)
|
||||||
|
return Collisions.isCircleToCircle(this.position, this.radius, other.position, (other as Circle).radius);
|
||||||
|
|
||||||
|
if (other instanceof Polygon)
|
||||||
|
return ShapeCollisions.circleToPolygon(this, other);
|
||||||
|
|
||||||
|
throw new Error(`overlaps of circle to ${other} are not supported`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,4 +2,9 @@ class CollisionResult {
|
|||||||
public minimumTranslationVector: Vector2;
|
public minimumTranslationVector: Vector2;
|
||||||
public normal: Vector2;
|
public normal: Vector2;
|
||||||
public point: Vector2;
|
public point: Vector2;
|
||||||
|
|
||||||
|
public invertResult(){
|
||||||
|
this.minimumTranslationVector = Vector2.negate(this.minimumTranslationVector);
|
||||||
|
this.normal = Vector2.negate(this.normal);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -56,6 +56,24 @@ class Polygon extends Shape {
|
|||||||
this._areEdgeNormalsDirty = true;
|
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`);
|
||||||
|
}
|
||||||
|
|
||||||
public static findPolygonCenter(points: Vector2[]) {
|
public static findPolygonCenter(points: Vector2[]) {
|
||||||
let x = 0, y = 0;
|
let x = 0, y = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ abstract class Shape {
|
|||||||
|
|
||||||
public abstract recalculateBounds(collider: Collider);
|
public abstract recalculateBounds(collider: Collider);
|
||||||
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
|
public abstract overlaps(other: Shape);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
class ShapeCollisions {
|
class ShapeCollisions {
|
||||||
public static polygonToPolygon(first: Polygon, second: Polygon){
|
public static polygonToPolygon(first: Polygon, second: Polygon) {
|
||||||
let result = new CollisionResult();
|
let result = new CollisionResult();
|
||||||
let isIntersecting = true;
|
let isIntersecting = true;
|
||||||
|
|
||||||
@@ -9,9 +9,9 @@ class ShapeCollisions {
|
|||||||
let translationAxis = new Vector2();
|
let translationAxis = new Vector2();
|
||||||
let polygonOffset = Vector2.subtract(first.position, second.position);
|
let polygonOffset = Vector2.subtract(first.position, second.position);
|
||||||
let axis: Vector2;
|
let axis: Vector2;
|
||||||
|
|
||||||
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex ++){
|
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) {
|
||||||
if (edgeIndex < firstEdges.length){
|
if (edgeIndex < firstEdges.length) {
|
||||||
axis = firstEdges[edgeIndex];
|
axis = firstEdges[edgeIndex];
|
||||||
} else {
|
} else {
|
||||||
axis = secondEdges[edgeIndex - firstEdges.length];
|
axis = secondEdges[edgeIndex - firstEdges.length];
|
||||||
@@ -22,72 +22,130 @@ class ShapeCollisions {
|
|||||||
let maxA = 0;
|
let maxA = 0;
|
||||||
let maxB = 0;
|
let maxB = 0;
|
||||||
let intervalDist = 0;
|
let intervalDist = 0;
|
||||||
this.getInterval(axis, first, minA, maxA);
|
let ta = this.getInterval(axis, first, minA, maxA);
|
||||||
this.getInterval(axis, second, minB, maxB);
|
minA = ta.min;
|
||||||
|
minB = ta.max;
|
||||||
|
let tb = this.getInterval(axis, second, minB, maxB);
|
||||||
|
minB = tb.min;
|
||||||
|
maxB = tb.max;
|
||||||
|
|
||||||
|
let relativeIntervalOffset = Vector2.dot(polygonOffset, axis);
|
||||||
|
minA += relativeIntervalOffset;
|
||||||
|
maxA += relativeIntervalOffset;
|
||||||
|
|
||||||
|
intervalDist = this.intervalDistance(minA, maxA, minB, maxB);
|
||||||
|
if (intervalDist > 0)
|
||||||
|
isIntersecting = false;
|
||||||
|
|
||||||
|
if (!isIntersecting)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
intervalDist = Math.abs(intervalDist);
|
||||||
|
if (intervalDist < minIntervalDistance) {
|
||||||
|
minIntervalDistance = intervalDist;
|
||||||
|
translationAxis = axis;
|
||||||
|
|
||||||
|
if (Vector2.dot(translationAxis, polygonOffset) < 0)
|
||||||
|
translationAxis = new Vector2(-translationAxis);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.normal = translationAxis;
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis), new Vector2(minIntervalDistance));
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number){
|
public static intervalDistance(minA: number, maxA: number, minB: number, maxB) {
|
||||||
|
if (minA < minB)
|
||||||
|
return minB - maxA;
|
||||||
|
|
||||||
|
return minA - minB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number) {
|
||||||
let dot = Vector2.dot(polygon.points[0], axis);
|
let dot = Vector2.dot(polygon.points[0], axis);
|
||||||
min = max = dot;
|
min = max = dot;
|
||||||
|
|
||||||
for (let i = 1; i < polygon.points.length; i++){
|
for (let i = 1; i < polygon.points.length; i++) {
|
||||||
dot = Vector2.dot(polygon.points[i], axis);
|
dot = Vector2.dot(polygon.points[i], axis);
|
||||||
if (dot < min){
|
if (dot < min) {
|
||||||
min = dot;
|
min = dot;
|
||||||
}else if(dot > max){
|
} else if (dot > max) {
|
||||||
max = dot;
|
max = dot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { min: min, max: max };
|
||||||
}
|
}
|
||||||
|
|
||||||
public static circleToPolygon(circle: Circle, polygon: Polygon){
|
public static circleToPolygon(circle: Circle, polygon: Polygon) {
|
||||||
let result = new CollisionResult();
|
let result = new CollisionResult();
|
||||||
|
|
||||||
let poly2Circle = Vector2.subtract(circle.position, polygon.position);
|
let poly2Circle = Vector2.subtract(circle.position, polygon.position);
|
||||||
|
|
||||||
let gpp = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle);
|
let gpp = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle);
|
||||||
let closestPoint = gpp.closestPoint;
|
let closestPoint: Vector2 = gpp.closestPoint;
|
||||||
let distanceSquared: number = gpp.distanceSquared;
|
let distanceSquared: number = gpp.distanceSquared;
|
||||||
result.normal = gpp.edgeNormal;
|
result.normal = gpp.edgeNormal;
|
||||||
|
|
||||||
let circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
let circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
||||||
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
||||||
return result;
|
return null;
|
||||||
|
|
||||||
let mtv: Vector2;
|
let mtv: Vector2;
|
||||||
if (circleCenterInsidePoly){
|
if (circleCenterInsidePoly) {
|
||||||
mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius, Math.sqrt(distanceSquared) - circle.radius));
|
mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius));
|
||||||
}else{
|
} else {
|
||||||
if (distanceSquared == 0){
|
if (distanceSquared == 0) {
|
||||||
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius));
|
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
||||||
}else{
|
} else {
|
||||||
let distance = Math.sqrt(distanceSquared);
|
let distance = Math.sqrt(distanceSquared);
|
||||||
// mtv = Vector2.multiply( -Vector2.subtract(poly2Circle, closestPoint), new Vector2((circle.radius - distanceSquared) / distance))
|
mtv = Vector2.multiply(new Vector2(-Vector2.subtract(poly2Circle, closestPoint)), new Vector2((circle.radius - distanceSquared) / distance));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static circleToRect(circle: Circle, box: Box): CollisionResult{
|
result.minimumTranslationVector = mtv;
|
||||||
let result = new CollisionResult();
|
result.point = Vector2.add(closestPoint, polygon.position);
|
||||||
let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
|
||||||
|
|
||||||
if (box.containsPoint(circle.position)){
|
|
||||||
result.point = closestPointOnBounds;
|
|
||||||
|
|
||||||
let safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius, circle.radius)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static pointToCicle(point: Vector2, circle: Circle){
|
public static circleToBox(circle: Circle, box: Box): CollisionResult {
|
||||||
|
let result = new CollisionResult();
|
||||||
|
let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
||||||
|
|
||||||
|
if (box.containsPoint(circle.position)) {
|
||||||
|
result.point = closestPointOnBounds;
|
||||||
|
|
||||||
|
let safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius)));
|
||||||
|
result.minimumTranslationVector = Vector2.subtract(circle.position, safePlace);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
let sqrDistance = Vector2.distanceSquared(closestPointOnBounds, circle.position);
|
||||||
|
if (sqrDistance == 0) {
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
||||||
|
} else if (sqrDistance <= circle.radius * circle.radius) {
|
||||||
|
result.normal = Vector2.subtract(circle.position, closestPointOnBounds);
|
||||||
|
let depth = result.normal.length() - circle.radius;
|
||||||
|
result.normal = Vector2Ext.normalize(result.normal);
|
||||||
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(depth), result.normal);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static pointToCircle(point: Vector2, circle: Circle) {
|
||||||
let result = new CollisionResult();
|
let result = new CollisionResult();
|
||||||
|
|
||||||
let distanceSquared = Vector2.distanceSquared(point, circle.position);
|
let distanceSquared = Vector2.distanceSquared(point, circle.position);
|
||||||
let sumOfRadii = 1 + circle.radius;
|
let sumOfRadii = 1 + circle.radius;
|
||||||
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||||
if (collided){
|
if (collided) {
|
||||||
result.normal = Vector2.normalize(Vector2.subtract(point, circle.position));
|
result.normal = Vector2.normalize(Vector2.subtract(point, circle.position));
|
||||||
let depth = sumOfRadii - Math.sqrt(distanceSquared);
|
let depth = sumOfRadii - Math.sqrt(distanceSquared);
|
||||||
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth, -depth), result.normal);
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth, -depth), result.normal);
|
||||||
@@ -96,10 +154,10 @@ class ShapeCollisions {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2){
|
public static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2) {
|
||||||
let v = Vector2.subtract(lineB, lineA);
|
let v = Vector2.subtract(lineB, lineA);
|
||||||
let w = Vector2.subtract(closestTo, lineA);
|
let w = Vector2.subtract(closestTo, lineA);
|
||||||
let t = Vector2.dot(w, v) / Vector2.dot(v, v);
|
let t = Vector2.dot(w, v) / Vector2.dot(v, v);
|
||||||
@@ -108,10 +166,10 @@ class ShapeCollisions {
|
|||||||
return Vector2.add(lineA, Vector2.multiply(v, new Vector2(t, t)));
|
return Vector2.add(lineA, Vector2.multiply(v, new Vector2(t, t)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static pointToPoly(point: Vector2, poly: Polygon){
|
public static pointToPoly(point: Vector2, poly: Polygon) {
|
||||||
let result = new CollisionResult();
|
let result = new CollisionResult();
|
||||||
|
|
||||||
if (poly.containsPoint(point)){
|
if (poly.containsPoint(point)) {
|
||||||
let distanceSquared: number;
|
let distanceSquared: number;
|
||||||
let gpp = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position));
|
let gpp = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position));
|
||||||
let closestPoint = gpp.closestPoint;
|
let closestPoint = gpp.closestPoint;
|
||||||
@@ -124,6 +182,24 @@ class ShapeCollisions {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static circleToCircle(first: Circle, second: Circle){
|
||||||
|
let result = new CollisionResult();
|
||||||
|
|
||||||
|
let distanceSquared = Vector2.distanceSquared(first.position, second.position);
|
||||||
|
let sumOfRadii = first.radius + second.radius;
|
||||||
|
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||||
|
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);
|
||||||
|
result.point = Vector2.add(second.position, Vector2.multiply(result.normal, new Vector2(second.radius)));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,4 +26,20 @@ class Vector2Ext {
|
|||||||
public static perpendicular(first: Vector2, second: Vector2){
|
public static perpendicular(first: Vector2, second: Vector2){
|
||||||
return new Vector2(-1 * (second.y - first.y), second.x - first.x);
|
return new Vector2(-1 * (second.y - first.y), second.x - first.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vector2的临时解决方案
|
||||||
|
* 标准化把向量弄乱了
|
||||||
|
* @param vec
|
||||||
|
*/
|
||||||
|
public static normalize(vec: Vector2){
|
||||||
|
let magnitude = Math.sqrt((vec.x * vec.x) + (vec.y * vec.y));
|
||||||
|
if (magnitude > MathHelper.Epsilon){
|
||||||
|
vec = Vector2.divide(vec, new Vector2(magnitude));
|
||||||
|
} else {
|
||||||
|
vec.x = vec.y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user