完善shapeCollision 支持多边形
This commit is contained in:
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;
|
||||
enabled: boolean;
|
||||
setEnabled(isEnabled: boolean): this;
|
||||
abstract initialize(): any;
|
||||
initialize(): void;
|
||||
onAddedToEntity(): void;
|
||||
onRemovedFromEntity(): void;
|
||||
onEnabled(): void;
|
||||
@@ -194,6 +194,7 @@ declare class Entity {
|
||||
hasComponent<T extends Component>(type: any): boolean;
|
||||
getOrCreateComponent<T extends Component>(type: T): T;
|
||||
getComponent<T extends Component>(type: any): T;
|
||||
getComponents<T extends Component>(type: any): T[];
|
||||
removeComponentForType<T extends Component>(type: any): boolean;
|
||||
removeComponent(component: Component): void;
|
||||
removeAllComponents(): void;
|
||||
@@ -376,11 +377,18 @@ declare class SpriteRenderer extends RenderableComponent {
|
||||
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
||||
initialize(): void;
|
||||
}
|
||||
interface ITriggerListener {
|
||||
onTriggerEnter(other: Collider, local: Collider): any;
|
||||
onTriggerExit(other: Collider, local: Collider): any;
|
||||
}
|
||||
declare abstract class Collider extends Component {
|
||||
shape: Shape;
|
||||
physicsLayer: number;
|
||||
isTrigger: boolean;
|
||||
registeredPhysicsBounds: Rectangle;
|
||||
shouldColliderScaleAndRotationWithTransform: boolean;
|
||||
collidesWithLayers: number;
|
||||
_localOffsetLength: number;
|
||||
protected _isParentEntityAddedToScene: any;
|
||||
protected _isPositionDirty: boolean;
|
||||
protected _isRotationDirty: boolean;
|
||||
@@ -392,7 +400,10 @@ declare abstract class Collider extends Component {
|
||||
setLocalOffset(offset: Vector2): void;
|
||||
registerColliderWithPhysicsSystem(): void;
|
||||
unregisterColliderWithPhysicsSystem(): void;
|
||||
initialize(): void;
|
||||
overlaps(other: Collider): any;
|
||||
onEntityTransformChanged(comp: ComponentTransform): void;
|
||||
onEnabled(): void;
|
||||
onDisabled(): void;
|
||||
}
|
||||
declare class BoxCollider extends Collider {
|
||||
width: number;
|
||||
@@ -460,6 +471,7 @@ declare class ComponentList {
|
||||
updateLists(): void;
|
||||
private handleRemove;
|
||||
getComponent<T extends Component>(type: any, onlyReturnInitializedComponents: boolean): T;
|
||||
getComponents<T extends Component>(type: any): T[];
|
||||
update(): void;
|
||||
onEntityTransformChanged(comp: any): void;
|
||||
}
|
||||
@@ -529,6 +541,9 @@ declare class Flags {
|
||||
static invertFlags(self: number): number;
|
||||
}
|
||||
declare class MathHelper {
|
||||
static readonly Epsilon: number;
|
||||
static readonly Rad2Deg: number;
|
||||
static readonly Deg2Rad: number;
|
||||
static toDegrees(radians: number): number;
|
||||
static toRadians(degrees: 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 minOf(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 {
|
||||
m11: number;
|
||||
@@ -586,6 +602,14 @@ declare class Rectangle {
|
||||
declare class Vector2 {
|
||||
x: 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);
|
||||
static add(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 transform(position: Vector2, matrix: Matrix2D): Vector2;
|
||||
static distance(value1: Vector2, value2: Vector2): number;
|
||||
static negate(value: Vector2): Vector2;
|
||||
}
|
||||
declare class ColliderTriggerHelper {
|
||||
private _entity;
|
||||
update(): void;
|
||||
}
|
||||
declare enum PointSectors {
|
||||
center = 0,
|
||||
@@ -638,6 +667,7 @@ declare abstract class Shape {
|
||||
center: Vector2;
|
||||
abstract recalculateBounds(collider: Collider): any;
|
||||
abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||
abstract overlaps(other: Shape): any;
|
||||
}
|
||||
declare class Polygon extends Shape {
|
||||
points: Vector2[];
|
||||
@@ -651,8 +681,9 @@ declare class Polygon extends Shape {
|
||||
constructor(vertCount: number, radius: number);
|
||||
private buildEdgeNormals;
|
||||
setPoints(points: Vector2[]): void;
|
||||
collidesWithShape(other: Shape): void;
|
||||
collidesWithShape(other: Shape): CollisionResult;
|
||||
recalculateCenterAndEdgeNormals(): void;
|
||||
overlaps(other: Shape): any;
|
||||
static findPolygonCenter(points: Vector2[]): Vector2;
|
||||
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): {
|
||||
closestPoint: any;
|
||||
@@ -677,20 +708,27 @@ declare class Circle extends Shape {
|
||||
pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||
collidesWithShape(other: Shape): CollisionResult;
|
||||
recalculateBounds(collider: Collider): void;
|
||||
overlaps(other: Shape): any;
|
||||
}
|
||||
declare class CollisionResult {
|
||||
minimumTranslationVector: Vector2;
|
||||
normal: Vector2;
|
||||
point: Vector2;
|
||||
invertResult(): void;
|
||||
}
|
||||
declare class ShapeCollisions {
|
||||
static polygonToPolygon(first: Polygon, second: Polygon): void;
|
||||
static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): void;
|
||||
static polygonToPolygon(first: Polygon, second: Polygon): CollisionResult;
|
||||
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 circleToRect(circle: Circle, box: Box): CollisionResult;
|
||||
static pointToCicle(point: Vector2, circle: Circle): CollisionResult;
|
||||
static circleToBox(circle: Circle, box: Box): CollisionResult;
|
||||
static pointToCircle(point: Vector2, circle: Circle): CollisionResult;
|
||||
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
|
||||
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
|
||||
static circleToCircle(first: Circle, second: Circle): CollisionResult;
|
||||
}
|
||||
declare class SpatialHash {
|
||||
gridBounds: Rectangle;
|
||||
@@ -738,6 +776,7 @@ declare class Vector2Ext {
|
||||
static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean;
|
||||
static cross(u: Vector2, v: Vector2): number;
|
||||
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
||||
static normalize(vec: Vector2): Vector2;
|
||||
}
|
||||
declare class WebGLUtils {
|
||||
static getWebGL(): WebGLRenderingContext;
|
||||
|
||||
@@ -771,6 +771,8 @@ var Component = (function () {
|
||||
}
|
||||
return this;
|
||||
};
|
||||
Component.prototype.initialize = function () {
|
||||
};
|
||||
Component.prototype.onAddedToEntity = function () {
|
||||
};
|
||||
Component.prototype.onRemovedFromEntity = function () {
|
||||
@@ -1011,6 +1013,9 @@ var Entity = (function () {
|
||||
Entity.prototype.getComponent = function (type) {
|
||||
return this.components.getComponent(type, false);
|
||||
};
|
||||
Entity.prototype.getComponents = function (type) {
|
||||
return this.components.getComponents(type);
|
||||
};
|
||||
Entity.prototype.removeComponentForType = function (type) {
|
||||
var comp = this.getComponent(type);
|
||||
if (comp) {
|
||||
@@ -1786,6 +1791,8 @@ var Collider = (function (_super) {
|
||||
function Collider() {
|
||||
var _this = _super !== null && _super.apply(this, arguments) || this;
|
||||
_this.physicsLayer = 1 << 0;
|
||||
_this.shouldColliderScaleAndRotationWithTransform = true;
|
||||
_this.collidesWithLayers = Physics.allLayers;
|
||||
_this._isPositionDirty = true;
|
||||
_this._isRotationDirty = true;
|
||||
return _this;
|
||||
@@ -1815,6 +1822,7 @@ var Collider = (function (_super) {
|
||||
if (this._localOffset != offset) {
|
||||
this.unregisterColliderWithPhysicsSystem();
|
||||
this._localOffset = offset;
|
||||
this._localOffsetLength = this._localOffset.length();
|
||||
this._isPositionDirty = true;
|
||||
this.registerColliderWithPhysicsSystem();
|
||||
}
|
||||
@@ -1831,7 +1839,30 @@ var Collider = (function (_super) {
|
||||
}
|
||||
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;
|
||||
}(Component));
|
||||
@@ -2177,6 +2208,20 @@ var ComponentList = (function () {
|
||||
}
|
||||
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 () {
|
||||
this.updateLists();
|
||||
for (var i = 0; i < this._components.length; i++) {
|
||||
@@ -2478,6 +2523,13 @@ var MathHelper = (function () {
|
||||
MathHelper.maxOf = function (a, b, 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;
|
||||
}());
|
||||
var Matrix2D = (function () {
|
||||
@@ -2780,6 +2832,34 @@ var Vector2 = (function () {
|
||||
this.x = x ? x : 0;
|
||||
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) {
|
||||
var result = new Vector2(0, 0);
|
||||
result.x = value1.x + value2.x;
|
||||
@@ -2835,8 +2915,37 @@ var Vector2 = (function () {
|
||||
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||
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;
|
||||
}());
|
||||
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;
|
||||
(function (PointSectors) {
|
||||
PointSectors[PointSectors["center"] = 0] = "center";
|
||||
@@ -3041,6 +3150,20 @@ var Polygon = (function (_super) {
|
||||
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
||||
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) {
|
||||
var x = 0, y = 0;
|
||||
for (var i = 0; i < points.length; i++) {
|
||||
@@ -3132,13 +3255,14 @@ var Circle = (function (_super) {
|
||||
return _this;
|
||||
}
|
||||
Circle.prototype.pointCollidesWithShape = function (point) {
|
||||
return ShapeCollisions.pointToCicle(point, this);
|
||||
return ShapeCollisions.pointToCircle(point, this);
|
||||
};
|
||||
Circle.prototype.collidesWithShape = function (other) {
|
||||
if (other instanceof Box && other.isUnrotated) {
|
||||
return ShapeCollisions.circleToRect(this, other);
|
||||
return ShapeCollisions.circleToBox(this, other);
|
||||
}
|
||||
if (other instanceof Circle) {
|
||||
return ShapeCollisions.circleToCircle(this, other);
|
||||
}
|
||||
if (other instanceof Polygon) {
|
||||
return ShapeCollisions.circleToPolygon(this, other);
|
||||
@@ -3147,12 +3271,38 @@ var Circle = (function (_super) {
|
||||
};
|
||||
Circle.prototype.recalculateBounds = function (collider) {
|
||||
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;
|
||||
}(Shape));
|
||||
var CollisionResult = (function () {
|
||||
function CollisionResult() {
|
||||
}
|
||||
CollisionResult.prototype.invertResult = function () {
|
||||
this.minimumTranslationVector = Vector2.negate(this.minimumTranslationVector);
|
||||
this.normal = Vector2.negate(this.normal);
|
||||
};
|
||||
return CollisionResult;
|
||||
}());
|
||||
var ShapeCollisions = (function () {
|
||||
@@ -3179,9 +3329,36 @@ var ShapeCollisions = (function () {
|
||||
var maxA = 0;
|
||||
var maxB = 0;
|
||||
var intervalDist = 0;
|
||||
this.getInterval(axis, first, minA, maxA);
|
||||
this.getInterval(axis, second, minB, maxB);
|
||||
var ta = this.getInterval(axis, first, minA, maxA);
|
||||
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) {
|
||||
var dot = Vector2.dot(polygon.points[0], axis);
|
||||
@@ -3195,6 +3372,7 @@ var ShapeCollisions = (function () {
|
||||
max = dot;
|
||||
}
|
||||
}
|
||||
return { min: min, max: max };
|
||||
};
|
||||
ShapeCollisions.circleToPolygon = function (circle, polygon) {
|
||||
var result = new CollisionResult();
|
||||
@@ -3205,30 +3383,47 @@ var ShapeCollisions = (function () {
|
||||
result.normal = gpp.edgeNormal;
|
||||
var circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
||||
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
||||
return result;
|
||||
return null;
|
||||
var mtv;
|
||||
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 {
|
||||
if (distanceSquared == 0) {
|
||||
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius));
|
||||
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
||||
}
|
||||
else {
|
||||
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 closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
||||
if (box.containsPoint(circle.position)) {
|
||||
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 distanceSquared = Vector2.distanceSquared(point, circle.position);
|
||||
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)));
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
return null;
|
||||
};
|
||||
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
|
||||
var v = Vector2.subtract(lineB, lineA);
|
||||
@@ -3261,7 +3456,21 @@ var ShapeCollisions = (function () {
|
||||
result.point = Vector2.add(closestPoint, poly.position);
|
||||
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;
|
||||
}());
|
||||
@@ -3493,6 +3702,16 @@ var Vector2Ext = (function () {
|
||||
Vector2Ext.perpendicular = function (first, second) {
|
||||
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;
|
||||
}());
|
||||
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 BoxCollider());
|
||||
|
||||
let player2 = scene.createEntity("player2");
|
||||
player2.addComponent(new BoxCollider());
|
||||
|
||||
Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
|
||||
console.log("update emitter");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user