修复normalized
This commit is contained in:
36
source/bin/framework.d.ts
vendored
36
source/bin/framework.d.ts
vendored
@@ -567,11 +567,13 @@ declare module es {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
static smoothStep(value1: Vector2, value2: Vector2, amount: number): Vector2;
|
static smoothStep(value1: Vector2, value2: Vector2, amount: number): Vector2;
|
||||||
|
setTo(x: number, y: number): void;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
*/
|
*/
|
||||||
add(value: Vector2): Vector2;
|
add(v: Vector2): Vector2;
|
||||||
|
addEqual(v: Vector2): Vector2;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
@@ -594,13 +596,22 @@ declare module es {
|
|||||||
* @param value 要减去的Vector2
|
* @param value 要减去的Vector2
|
||||||
* @returns 当前Vector2
|
* @returns 当前Vector2
|
||||||
*/
|
*/
|
||||||
subtract(value: Vector2): this;
|
sub(value: Vector2): Vector2;
|
||||||
|
subEqual(v: Vector2): Vector2;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param size
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
scale(size: number): Vector2;
|
||||||
/**
|
/**
|
||||||
* 将这个Vector2变成一个方向相同的单位向量
|
* 将这个Vector2变成一个方向相同的单位向量
|
||||||
*/
|
*/
|
||||||
normalize(): void;
|
normalize(): this;
|
||||||
/** 返回它的长度 */
|
/** 返回它的长度 */
|
||||||
length(): number;
|
length(): number;
|
||||||
|
magnitude(): number;
|
||||||
|
distance(v?: Vector2): number;
|
||||||
/**
|
/**
|
||||||
* 返回该Vector2的平方长度
|
* 返回该Vector2的平方长度
|
||||||
* @returns 这个Vector2的平方长度
|
* @returns 这个Vector2的平方长度
|
||||||
@@ -1116,7 +1127,7 @@ declare module es {
|
|||||||
* @param minimumTranslationVector
|
* @param minimumTranslationVector
|
||||||
* @param responseVelocity
|
* @param responseVelocity
|
||||||
*/
|
*/
|
||||||
calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2, responseVelocity?: Vector2): void;
|
calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2): Vector2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declare module es {
|
declare module es {
|
||||||
@@ -3607,7 +3618,7 @@ declare module es {
|
|||||||
* @param rect
|
* @param rect
|
||||||
* @param layerMask
|
* @param layerMask
|
||||||
*/
|
*/
|
||||||
static boxcastBroadphase(rect: Rectangle, layerMask?: number): Set<Collider>;
|
static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
|
||||||
/**
|
/**
|
||||||
* 返回所有被边界交错的碰撞器,但不包括传入的碰撞器(self)。
|
* 返回所有被边界交错的碰撞器,但不包括传入的碰撞器(self)。
|
||||||
* 如果你想为其他查询自己创建扫描边界,这个方法很有用
|
* 如果你想为其他查询自己创建扫描边界,这个方法很有用
|
||||||
@@ -3615,13 +3626,13 @@ declare module es {
|
|||||||
* @param rect
|
* @param rect
|
||||||
* @param layerMask
|
* @param layerMask
|
||||||
*/
|
*/
|
||||||
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Set<Collider>;
|
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
|
||||||
/**
|
/**
|
||||||
* 返回所有边界与 collider.bounds 相交的碰撞器,但不包括传入的碰撞器(self)
|
* 返回所有边界与 collider.bounds 相交的碰撞器,但不包括传入的碰撞器(self)
|
||||||
* @param collider
|
* @param collider
|
||||||
* @param layerMask
|
* @param layerMask
|
||||||
*/
|
*/
|
||||||
static boxcastBroadphaseExcludingSelfNonRect(collider: Collider, layerMask?: number): Set<Collider>;
|
static boxcastBroadphaseExcludingSelfNonRect(collider: Collider, layerMask?: number): Collider[];
|
||||||
/**
|
/**
|
||||||
* 返回所有被 collider.bounds 扩展为包含 deltaX/deltaY 的碰撞器,但不包括传入的碰撞器(self)
|
* 返回所有被 collider.bounds 扩展为包含 deltaX/deltaY 的碰撞器,但不包括传入的碰撞器(self)
|
||||||
* @param collider
|
* @param collider
|
||||||
@@ -3629,7 +3640,7 @@ declare module es {
|
|||||||
* @param deltaY
|
* @param deltaY
|
||||||
* @param layerMask
|
* @param layerMask
|
||||||
*/
|
*/
|
||||||
static boxcastBroadphaseExcludingSelfDelta(collider: Collider, deltaX: number, deltaY: number, layerMask?: number): Set<Collider>;
|
static boxcastBroadphaseExcludingSelfDelta(collider: Collider, deltaX: number, deltaY: number, layerMask?: number): Collider[];
|
||||||
/**
|
/**
|
||||||
* 将对撞机添加到物理系统中
|
* 将对撞机添加到物理系统中
|
||||||
* @param collider
|
* @param collider
|
||||||
@@ -3741,7 +3752,7 @@ declare module es {
|
|||||||
* @param excludeCollider
|
* @param excludeCollider
|
||||||
* @param layerMask
|
* @param layerMask
|
||||||
*/
|
*/
|
||||||
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Set<Collider>;
|
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[];
|
||||||
/**
|
/**
|
||||||
* 通过空间散列投掷一条线,并将该线碰到的任何碰撞器填入碰撞数组
|
* 通过空间散列投掷一条线,并将该线碰到的任何碰撞器填入碰撞数组
|
||||||
* https://github.com/francisengelmann/fast_voxel_traversal/blob/master/main.cpp
|
* https://github.com/francisengelmann/fast_voxel_traversal/blob/master/main.cpp
|
||||||
@@ -4091,7 +4102,10 @@ declare module es {
|
|||||||
* @param min
|
* @param min
|
||||||
* @param max
|
* @param max
|
||||||
*/
|
*/
|
||||||
static getInterval(axis: Vector2, polygon: Polygon, min: Ref<number>, max: Ref<number>): void;
|
static getInterval(axis: Vector2, polygon: Polygon): {
|
||||||
|
min: number;
|
||||||
|
max: number;
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* 计算[minA, maxA]和[minB, maxB]之间的距离。如果间隔重叠,距离是负的
|
* 计算[minA, maxA]和[minB, maxB]之间的距离。如果间隔重叠,距离是负的
|
||||||
* @param minA
|
* @param minA
|
||||||
@@ -4099,7 +4113,7 @@ declare module es {
|
|||||||
* @param minB
|
* @param minB
|
||||||
* @param maxB
|
* @param maxB
|
||||||
*/
|
*/
|
||||||
static intervalDistance(minA: number, maxA: number, minB: number, maxB: any): number;
|
static intervalDistance(minA: number, maxA: number, minB: number, maxB: number): number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declare module es {
|
declare module es {
|
||||||
|
|||||||
@@ -1214,11 +1214,13 @@ var es;
|
|||||||
* @param value
|
* @param value
|
||||||
*/
|
*/
|
||||||
Vector2.normalize = function (value) {
|
Vector2.normalize = function (value) {
|
||||||
var nValue = new Vector2(value.x, value.y);
|
var d = value.distance();
|
||||||
var val = 1 / Math.sqrt((nValue.x * nValue.x) + (nValue.y * nValue.y));
|
if (d > 0) {
|
||||||
nValue.x *= val;
|
return new Vector2(value.x / d, value.y / d);
|
||||||
nValue.y *= val;
|
}
|
||||||
return nValue;
|
else {
|
||||||
|
return new Vector2(0, 1);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 返回两个向量的点积
|
* 返回两个向量的点积
|
||||||
@@ -1335,13 +1337,20 @@ var es;
|
|||||||
Vector2.smoothStep = function (value1, value2, amount) {
|
Vector2.smoothStep = function (value1, value2, amount) {
|
||||||
return new Vector2(es.MathHelper.smoothStep(value1.x, value2.x, amount), es.MathHelper.smoothStep(value1.y, value2.y, amount));
|
return new Vector2(es.MathHelper.smoothStep(value1.x, value2.x, amount), es.MathHelper.smoothStep(value1.y, value2.y, amount));
|
||||||
};
|
};
|
||||||
|
Vector2.prototype.setTo = function (x, y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
*/
|
*/
|
||||||
Vector2.prototype.add = function (value) {
|
Vector2.prototype.add = function (v) {
|
||||||
this.x += value.x;
|
return new Vector2(this.x + v.x, this.y + v.y);
|
||||||
this.y += value.y;
|
};
|
||||||
|
Vector2.prototype.addEqual = function (v) {
|
||||||
|
this.x += v.x;
|
||||||
|
this.y += v.y;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -1382,23 +1391,49 @@ var es;
|
|||||||
* @param value 要减去的Vector2
|
* @param value 要减去的Vector2
|
||||||
* @returns 当前Vector2
|
* @returns 当前Vector2
|
||||||
*/
|
*/
|
||||||
Vector2.prototype.subtract = function (value) {
|
Vector2.prototype.sub = function (value) {
|
||||||
this.x -= value.x;
|
return new Vector2(this.x - value.x, this.y - value.y);
|
||||||
this.y -= value.y;
|
};
|
||||||
|
Vector2.prototype.subEqual = function (v) {
|
||||||
|
this.x -= v.x;
|
||||||
|
this.y -= v.y;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param size
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
Vector2.prototype.scale = function (size) {
|
||||||
|
return new Vector2(this.x * size, this.y * size);
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* 将这个Vector2变成一个方向相同的单位向量
|
* 将这个Vector2变成一个方向相同的单位向量
|
||||||
*/
|
*/
|
||||||
Vector2.prototype.normalize = function () {
|
Vector2.prototype.normalize = function () {
|
||||||
var val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
|
var d = this.distance();
|
||||||
this.x *= val;
|
if (d > 0) {
|
||||||
this.y *= val;
|
this.setTo(this.x / d, this.y / d);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.setTo(0, 1);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
/** 返回它的长度 */
|
/** 返回它的长度 */
|
||||||
Vector2.prototype.length = function () {
|
Vector2.prototype.length = function () {
|
||||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||||
};
|
};
|
||||||
|
Vector2.prototype.magnitude = function () {
|
||||||
|
return this.distance();
|
||||||
|
};
|
||||||
|
Vector2.prototype.distance = function (v) {
|
||||||
|
if (!v) {
|
||||||
|
v = Vector2.zero;
|
||||||
|
}
|
||||||
|
return Math.sqrt(Math.pow(this.x - v.x, 2) + Math.pow(this.y - v.y, 2));
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* 返回该Vector2的平方长度
|
* 返回该Vector2的平方长度
|
||||||
* @returns 这个Vector2的平方长度
|
* @returns 这个Vector2的平方长度
|
||||||
@@ -2451,14 +2486,16 @@ var es;
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this.shouldUseGravity)
|
if (this.shouldUseGravity)
|
||||||
this.velocity = es.Vector2.add(this.velocity, es.Vector2.multiplyScaler(es.Physics.gravity, es.Time.deltaTime));
|
this.velocity.addEqual(es.Physics.gravity.scale(es.Time.deltaTime));
|
||||||
this.entity.position = es.Vector2.add(this.entity.position, es.Vector2.multiplyScaler(this.velocity, es.Time.deltaTime));
|
this.entity.position = es.Vector2.add(this.entity.position, es.Vector2.multiplyScaler(this.velocity, es.Time.deltaTime));
|
||||||
var collisionResult = new es.CollisionResult();
|
var collisionResult = new es.CollisionResult();
|
||||||
// 捞取我们在新的位置上可能会碰撞到的任何东西
|
// 捞取我们在新的位置上可能会碰撞到的任何东西
|
||||||
var neighbors = es.Physics.boxcastBroadphaseExcludingSelfNonRect(this._collider, this._collider.collidesWithLayers.value);
|
var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(this._collider, this._collider.bounds, this._collider.collidesWithLayers.value);
|
||||||
try {
|
try {
|
||||||
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
|
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
|
||||||
var neighbor = neighbors_1_1.value;
|
var neighbor = neighbors_1_1.value;
|
||||||
|
if (!neighbor)
|
||||||
|
continue;
|
||||||
// 如果邻近的对撞机是同一个实体,则忽略它
|
// 如果邻近的对撞机是同一个实体,则忽略它
|
||||||
if (neighbor.entity.equals(this.entity)) {
|
if (neighbor.entity.equals(this.entity)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -2473,9 +2510,8 @@ var es;
|
|||||||
else {
|
else {
|
||||||
// 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的
|
// 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的
|
||||||
this.entity.position = es.Vector2.subtract(this.entity.position, collisionResult.minimumTranslationVector);
|
this.entity.position = es.Vector2.subtract(this.entity.position, collisionResult.minimumTranslationVector);
|
||||||
var relativeVelocity = this.velocity.clone();
|
var relativeVelocity = this.calculateResponseVelocity(this.velocity, collisionResult.minimumTranslationVector);
|
||||||
this.calculateResponseVelocity(relativeVelocity, collisionResult.minimumTranslationVector, relativeVelocity);
|
this.velocity.addEqual(relativeVelocity);
|
||||||
this.velocity.add(relativeVelocity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2495,14 +2531,14 @@ var es;
|
|||||||
*/
|
*/
|
||||||
ArcadeRigidbody.prototype.processOverlap = function (other, minimumTranslationVector) {
|
ArcadeRigidbody.prototype.processOverlap = function (other, minimumTranslationVector) {
|
||||||
if (this.isImmovable) {
|
if (this.isImmovable) {
|
||||||
other.entity.position = es.Vector2.add(other.entity.position, minimumTranslationVector);
|
other.entity.position = other.entity.position.add(minimumTranslationVector);
|
||||||
}
|
}
|
||||||
else if (other.isImmovable) {
|
else if (other.isImmovable) {
|
||||||
other.entity.position = es.Vector2.subtract(other.entity.position, minimumTranslationVector);
|
this.entity.position = this.entity.position.sub(minimumTranslationVector);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.entity.position = es.Vector2.subtract(this.entity.position, es.Vector2.multiplyScaler(minimumTranslationVector, 0.5));
|
this.entity.position = this.entity.position.sub(minimumTranslationVector.scale(0.5));
|
||||||
other.entity.position = es.Vector2.add(other.entity.position, es.Vector2.multiplyScaler(minimumTranslationVector, 0.5));
|
other.entity.position = other.entity.position.add(minimumTranslationVector.scale(0.5));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -2515,13 +2551,13 @@ var es;
|
|||||||
// 计算的基础是沿碰撞表面法线反射的物体的相对速度。
|
// 计算的基础是沿碰撞表面法线反射的物体的相对速度。
|
||||||
// 然后,响应的一部分会根据质量加到每个物体上
|
// 然后,响应的一部分会根据质量加到每个物体上
|
||||||
var relativeVelocity = es.Vector2.subtract(this.velocity, other.velocity);
|
var relativeVelocity = es.Vector2.subtract(this.velocity, other.velocity);
|
||||||
this.calculateResponseVelocity(relativeVelocity, minimumTranslationVector, relativeVelocity);
|
relativeVelocity = this.calculateResponseVelocity(relativeVelocity, minimumTranslationVector);
|
||||||
// 现在,我们使用质量来线性缩放两个刚体上的响应
|
// 现在,我们使用质量来线性缩放两个刚体上的响应
|
||||||
var totalinverseMass = this._inverseMass + other._inverseMass;
|
var totalinverseMass = this._inverseMass + other._inverseMass;
|
||||||
var ourResponseFraction = this._inverseMass / totalinverseMass;
|
var ourResponseFraction = this._inverseMass / totalinverseMass;
|
||||||
var otherResponseFraction = other._inverseMass / totalinverseMass;
|
var otherResponseFraction = other._inverseMass / totalinverseMass;
|
||||||
this.velocity = es.Vector2.add(this.velocity, es.Vector2.multiplyScaler(relativeVelocity, ourResponseFraction));
|
this.velocity = es.Vector2.add(this.velocity, relativeVelocity.scale(ourResponseFraction));
|
||||||
other.velocity = es.Vector2.subtract(other.velocity, es.Vector2.multiplyScaler(relativeVelocity, otherResponseFraction));
|
other.velocity = es.Vector2.subtract(other.velocity, relativeVelocity.scale(otherResponseFraction));
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 给定两个物体和MTV之间的相对速度,本方法修改相对速度,使其成为碰撞响应
|
* 给定两个物体和MTV之间的相对速度,本方法修改相对速度,使其成为碰撞响应
|
||||||
@@ -2529,8 +2565,7 @@ var es;
|
|||||||
* @param minimumTranslationVector
|
* @param minimumTranslationVector
|
||||||
* @param responseVelocity
|
* @param responseVelocity
|
||||||
*/
|
*/
|
||||||
ArcadeRigidbody.prototype.calculateResponseVelocity = function (relativeVelocity, minimumTranslationVector, responseVelocity) {
|
ArcadeRigidbody.prototype.calculateResponseVelocity = function (relativeVelocity, minimumTranslationVector) {
|
||||||
if (responseVelocity === void 0) { responseVelocity = es.Vector2.zero; }
|
|
||||||
// 首先,我们得到反方向的归一化MTV:表面法线
|
// 首先,我们得到反方向的归一化MTV:表面法线
|
||||||
var inverseMTV = es.Vector2.multiplyScaler(minimumTranslationVector, -1);
|
var inverseMTV = es.Vector2.multiplyScaler(minimumTranslationVector, -1);
|
||||||
var normal = es.Vector2.normalize(inverseMTV);
|
var normal = es.Vector2.normalize(inverseMTV);
|
||||||
@@ -2546,10 +2581,10 @@ var es;
|
|||||||
if (tangentialVelocityComponent.lengthSquared() < this._glue)
|
if (tangentialVelocityComponent.lengthSquared() < this._glue)
|
||||||
coefficientOfFriction = 1.01;
|
coefficientOfFriction = 1.01;
|
||||||
// 弹性影响速度的法向分量,摩擦力影响速度的切向分量
|
// 弹性影响速度的法向分量,摩擦力影响速度的切向分量
|
||||||
var r = es.Vector2.multiplyScaler(normalVelocityComponent, -(1 + this._elasticity))
|
return normalVelocityComponent
|
||||||
.subtract(es.Vector2.multiplyScaler(tangentialVelocityComponent, coefficientOfFriction));
|
.scale(1 + this._elasticity)
|
||||||
responseVelocity.x = r.x;
|
.sub(tangentialVelocityComponent.scale(coefficientOfFriction))
|
||||||
responseVelocity.y = r.y;
|
.scale(-1);
|
||||||
};
|
};
|
||||||
return ArcadeRigidbody;
|
return ArcadeRigidbody;
|
||||||
}(es.Component));
|
}(es.Component));
|
||||||
@@ -2804,9 +2839,8 @@ var es;
|
|||||||
this._skinWidth * (this.rayOriginSkinMutiplier - 1);
|
this._skinWidth * (this.rayOriginSkinMutiplier - 1);
|
||||||
for (var i = 0; i < this.totalHorizontalRays; i++) {
|
for (var i = 0; i < this.totalHorizontalRays; i++) {
|
||||||
var ray = new es.Vector2(initialRayOriginX, initialRayOriginY - i * this._verticalDistanceBetweenRays);
|
var ray = new es.Vector2(initialRayOriginX, initialRayOriginY - i * this._verticalDistanceBetweenRays);
|
||||||
// if we are grounded we will include oneWayPlatforms
|
// 如果我们接地,我们将只在第一条射线(底部)上包含 oneWayPlatforms。
|
||||||
// only on the first ray (the bottom one). this will allow us to
|
// 允许我们走上倾斜的 oneWayPlatforms
|
||||||
// walk up sloped oneWayPlatforms
|
|
||||||
if (i === 0 &&
|
if (i === 0 &&
|
||||||
this.supportSlopedOneWayPlatforms &&
|
this.supportSlopedOneWayPlatforms &&
|
||||||
this.collisionState.wasGroundedLastFrame) {
|
this.collisionState.wasGroundedLastFrame) {
|
||||||
@@ -3067,7 +3101,7 @@ var es;
|
|||||||
var _internalcollisionResult = new es.CollisionResult();
|
var _internalcollisionResult = new es.CollisionResult();
|
||||||
if (collider_1.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
if (collider_1.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
||||||
// 如果碰撞 则退回之前的移动量
|
// 如果碰撞 则退回之前的移动量
|
||||||
motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
motion.sub(_internalcollisionResult.minimumTranslationVector);
|
||||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||||
if (_internalcollisionResult.collider != null) {
|
if (_internalcollisionResult.collider != null) {
|
||||||
collisionResult.collider = _internalcollisionResult.collider;
|
collisionResult.collider = _internalcollisionResult.collider;
|
||||||
@@ -3281,8 +3315,8 @@ var es;
|
|||||||
Collider.prototype.setLocalOffset = function (offset) {
|
Collider.prototype.setLocalOffset = function (offset) {
|
||||||
if (!this._localOffset.equals(offset)) {
|
if (!this._localOffset.equals(offset)) {
|
||||||
this.unregisterColliderWithPhysicsSystem();
|
this.unregisterColliderWithPhysicsSystem();
|
||||||
this._localOffset = offset;
|
this._localOffset.setTo(offset.x, offset.y);
|
||||||
this._localOffsetLength = this._localOffset.length();
|
this._localOffsetLength = this._localOffset.magnitude();
|
||||||
this._isPositionDirty = true;
|
this._isPositionDirty = true;
|
||||||
this.registerColliderWithPhysicsSystem();
|
this.registerColliderWithPhysicsSystem();
|
||||||
}
|
}
|
||||||
@@ -3386,13 +3420,13 @@ var es;
|
|||||||
Collider.prototype.collidesWith = function (collider, motion, result) {
|
Collider.prototype.collidesWith = function (collider, motion, result) {
|
||||||
if (result === void 0) { result = new es.CollisionResult(); }
|
if (result === void 0) { result = new es.CollisionResult(); }
|
||||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||||
var oldPosition = this.entity.position.clone();
|
var oldPosition = this.entity.position;
|
||||||
this.entity.position = es.Vector2.add(this.entity.position, motion);
|
this.entity.position = this.entity.position.add(motion);
|
||||||
var didCollide = this.shape.collidesWithShape(collider.shape, result);
|
var didCollide = this.shape.collidesWithShape(collider.shape, result);
|
||||||
if (didCollide)
|
if (didCollide)
|
||||||
result.collider = collider;
|
result.collider = collider;
|
||||||
// 将图形位置返回到检查前的位置
|
// 将图形位置返回到检查前的位置
|
||||||
this.entity.position = oldPosition.clone();
|
this.entity.position = oldPosition;
|
||||||
return didCollide;
|
return didCollide;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -3406,6 +3440,7 @@ var es;
|
|||||||
result.collider = collider;
|
result.collider = collider;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
result.collider = null;
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -3671,10 +3706,9 @@ var es;
|
|||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
// 第一点和最后一点决不能相同。我们想要一个开放的多边形
|
// 第一点和最后一点决不能相同。我们想要一个开放的多边形
|
||||||
var isPolygonClosed = points[0] == points[points.length - 1];
|
var isPolygonClosed = points[0] == points[points.length - 1];
|
||||||
var linqPoints = new es.List(points);
|
|
||||||
// 最后一个移除
|
// 最后一个移除
|
||||||
if (isPolygonClosed)
|
if (isPolygonClosed)
|
||||||
linqPoints.remove(linqPoints.last());
|
points = points.slice(0, points.length - 1);
|
||||||
var center = es.Polygon.findPolygonCenter(points);
|
var center = es.Polygon.findPolygonCenter(points);
|
||||||
_this.setLocalOffset(center);
|
_this.setLocalOffset(center);
|
||||||
es.Polygon.recenterPolygonVerts(points);
|
es.Polygon.recenterPolygonVerts(points);
|
||||||
@@ -6448,8 +6482,8 @@ var es;
|
|||||||
t = es.MathHelper.clamp01(t);
|
t = es.MathHelper.clamp01(t);
|
||||||
var oneMinusT = 1 - t;
|
var oneMinusT = 1 - t;
|
||||||
return new es.Vector2(oneMinusT * oneMinusT).multiply(p0)
|
return new es.Vector2(oneMinusT * oneMinusT).multiply(p0)
|
||||||
.add(new es.Vector2(2 * oneMinusT * t).multiply(p1))
|
.addEqual(new es.Vector2(2 * oneMinusT * t).multiply(p1))
|
||||||
.add(new es.Vector2(t * t).multiply(p2));
|
.addEqual(new es.Vector2(t * t).multiply(p2));
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 求解一个立方体曲率
|
* 求解一个立方体曲率
|
||||||
@@ -6463,9 +6497,9 @@ var es;
|
|||||||
t = es.MathHelper.clamp01(t);
|
t = es.MathHelper.clamp01(t);
|
||||||
var oneMinusT = 1 - t;
|
var oneMinusT = 1 - t;
|
||||||
return new es.Vector2(oneMinusT * oneMinusT * oneMinusT).multiply(start)
|
return new es.Vector2(oneMinusT * oneMinusT * oneMinusT).multiply(start)
|
||||||
.add(new es.Vector2(3 * oneMinusT * oneMinusT * t).multiply(firstControlPoint))
|
.addEqual(new es.Vector2(3 * oneMinusT * oneMinusT * t).multiply(firstControlPoint))
|
||||||
.add(new es.Vector2(3 * oneMinusT * t * t).multiply(secondControlPoint))
|
.addEqual(new es.Vector2(3 * oneMinusT * t * t).multiply(secondControlPoint))
|
||||||
.add(new es.Vector2(t * t * t).multiply(end));
|
.addEqual(new es.Vector2(t * t * t).multiply(end));
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 得到二次贝塞尔函数的一阶导数
|
* 得到二次贝塞尔函数的一阶导数
|
||||||
@@ -6476,7 +6510,7 @@ var es;
|
|||||||
*/
|
*/
|
||||||
Bezier.getFirstDerivative = function (p0, p1, p2, t) {
|
Bezier.getFirstDerivative = function (p0, p1, p2, t) {
|
||||||
return new es.Vector2(2 * (1 - t)).multiply(es.Vector2.subtract(p1, p0))
|
return new es.Vector2(2 * (1 - t)).multiply(es.Vector2.subtract(p1, p0))
|
||||||
.add(new es.Vector2(2 * t).multiply(es.Vector2.subtract(p2, p1)));
|
.addEqual(new es.Vector2(2 * t).multiply(es.Vector2.subtract(p2, p1)));
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 得到一个三次贝塞尔函数的一阶导数
|
* 得到一个三次贝塞尔函数的一阶导数
|
||||||
@@ -6490,8 +6524,8 @@ var es;
|
|||||||
t = es.MathHelper.clamp01(t);
|
t = es.MathHelper.clamp01(t);
|
||||||
var oneMunusT = 1 - t;
|
var oneMunusT = 1 - t;
|
||||||
return new es.Vector2(3 * oneMunusT * oneMunusT).multiply(es.Vector2.subtract(firstControlPoint, start))
|
return new es.Vector2(3 * oneMunusT * oneMunusT).multiply(es.Vector2.subtract(firstControlPoint, start))
|
||||||
.add(new es.Vector2(6 * oneMunusT * t).multiply(es.Vector2.subtract(secondControlPoint, firstControlPoint)))
|
.addEqual(new es.Vector2(6 * oneMunusT * t).multiply(es.Vector2.subtract(secondControlPoint, firstControlPoint)))
|
||||||
.add(new es.Vector2(3 * t * t).multiply(es.Vector2.subtract(end, secondControlPoint)));
|
.addEqual(new es.Vector2(3 * t * t).multiply(es.Vector2.subtract(end, secondControlPoint)));
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 递归地细分bezier曲线,直到满足距离校正
|
* 递归地细分bezier曲线,直到满足距离校正
|
||||||
@@ -6582,9 +6616,9 @@ var es;
|
|||||||
if (index % 3 == 0) {
|
if (index % 3 == 0) {
|
||||||
var delta = es.Vector2.subtract(point, this._points[index]);
|
var delta = es.Vector2.subtract(point, this._points[index]);
|
||||||
if (index > 0)
|
if (index > 0)
|
||||||
this._points[index - 1].add(delta);
|
this._points[index - 1].addEqual(delta);
|
||||||
if (index + 1 < this._points.length)
|
if (index + 1 < this._points.length)
|
||||||
this._points[index + 1].add(delta);
|
this._points[index + 1].addEqual(delta);
|
||||||
}
|
}
|
||||||
this._points[index] = point;
|
this._points[index] = point;
|
||||||
};
|
};
|
||||||
@@ -8456,7 +8490,7 @@ var es;
|
|||||||
for (var i = 0; i < colliders.length; i++) {
|
for (var i = 0; i < colliders.length; i++) {
|
||||||
var collider = colliders[i];
|
var collider = colliders[i];
|
||||||
var neighbors = es.Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
var neighbors = es.Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
||||||
for (var j = 0; j < neighbors.size; j++) {
|
for (var j = 0; j < neighbors.length; j++) {
|
||||||
var neighbor = neighbors[j];
|
var neighbor = neighbors[j];
|
||||||
// 我们至少需要一个碰撞器作为触发器
|
// 我们至少需要一个碰撞器作为触发器
|
||||||
if (!collider.isTrigger && !neighbor.isTrigger)
|
if (!collider.isTrigger && !neighbor.isTrigger)
|
||||||
@@ -9083,7 +9117,7 @@ var es;
|
|||||||
for (var x = p1.x; x <= p2.x; x++) {
|
for (var x = p1.x; x <= p2.x; x++) {
|
||||||
for (var y = p1.y; y <= p2.y; y++) {
|
for (var y = p1.y; y <= p2.y; y++) {
|
||||||
var cell = this.cellAtPosition(x, y);
|
var cell = this.cellAtPosition(x, y);
|
||||||
if (cell == null)
|
if (!cell)
|
||||||
continue;
|
continue;
|
||||||
// 当cell不为空。循环并取回所有碰撞器
|
// 当cell不为空。循环并取回所有碰撞器
|
||||||
for (var i = 0; i < cell.length; i++) {
|
for (var i = 0; i < cell.length; i++) {
|
||||||
@@ -9097,7 +9131,7 @@ var es;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this._tempHashSet;
|
return Array.from(this._tempHashSet);
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 通过空间散列投掷一条线,并将该线碰到的任何碰撞器填入碰撞数组
|
* 通过空间散列投掷一条线,并将该线碰到的任何碰撞器填入碰撞数组
|
||||||
@@ -9319,7 +9353,7 @@ var es;
|
|||||||
* @param y
|
* @param y
|
||||||
*/
|
*/
|
||||||
SpatialHash.prototype.cellCoords = function (x, y) {
|
SpatialHash.prototype.cellCoords = function (x, y) {
|
||||||
return new es.Vector2(es.MathHelper.floorToInt(x * this._inverseCellSize), es.MathHelper.floorToInt(y * this._inverseCellSize));
|
return new es.Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 获取世界空间x,y值的单元格。
|
* 获取世界空间x,y值的单元格。
|
||||||
@@ -9504,9 +9538,13 @@ var es;
|
|||||||
* @param points
|
* @param points
|
||||||
*/
|
*/
|
||||||
Polygon.prototype.setPoints = function (points) {
|
Polygon.prototype.setPoints = function (points) {
|
||||||
|
var _this = this;
|
||||||
this.points = points;
|
this.points = points;
|
||||||
this.recalculateCenterAndEdgeNormals();
|
this.recalculateCenterAndEdgeNormals();
|
||||||
this._originalPoints = this.points.slice();
|
this._originalPoints = [];
|
||||||
|
this.points.forEach(function (p) {
|
||||||
|
_this._originalPoints.push(p.clone());
|
||||||
|
});
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 重新计算多边形中心
|
* 重新计算多边形中心
|
||||||
@@ -9708,7 +9746,7 @@ var es;
|
|||||||
*/
|
*/
|
||||||
Polygon.prototype.containsPoint = function (point) {
|
Polygon.prototype.containsPoint = function (point) {
|
||||||
// 将点归一化到多边形坐标空间中
|
// 将点归一化到多边形坐标空间中
|
||||||
point.subtract(this.position);
|
point.sub(this.position);
|
||||||
var isInside = false;
|
var isInside = false;
|
||||||
for (var i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
|
for (var i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
|
||||||
if (((this.points[i].y > point.y) != (this.points[j].y > point.y)) &&
|
if (((this.points[i].y > point.y) != (this.points[j].y > point.y)) &&
|
||||||
@@ -10297,8 +10335,8 @@ var es;
|
|||||||
*/
|
*/
|
||||||
ShapeCollisionsPolygon.polygonToPolygon = function (first, second, result) {
|
ShapeCollisionsPolygon.polygonToPolygon = function (first, second, result) {
|
||||||
var isIntersecting = true;
|
var isIntersecting = true;
|
||||||
var firstEdges = first.edgeNormals.slice();
|
var firstEdges = first.edgeNormals;
|
||||||
var secondEdges = second.edgeNormals.slice();
|
var secondEdges = second.edgeNormals;
|
||||||
var minIntervalDistance = Number.POSITIVE_INFINITY;
|
var minIntervalDistance = Number.POSITIVE_INFINITY;
|
||||||
var translationAxis = es.Vector2.zero;
|
var translationAxis = es.Vector2.zero;
|
||||||
var polygonOffset = es.Vector2.subtract(first.position, second.position);
|
var polygonOffset = es.Vector2.subtract(first.position, second.position);
|
||||||
@@ -10307,26 +10345,17 @@ var es;
|
|||||||
for (var edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) {
|
for (var edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) {
|
||||||
// 1. 找出当前多边形是否相交
|
// 1. 找出当前多边形是否相交
|
||||||
// 多边形的归一化轴垂直于缓存给我们的当前边
|
// 多边形的归一化轴垂直于缓存给我们的当前边
|
||||||
if (edgeIndex < firstEdges.length) {
|
axis = edgeIndex < firstEdges.length ? firstEdges[edgeIndex] : secondEdges[edgeIndex - firstEdges.length];
|
||||||
axis = firstEdges[edgeIndex];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
axis = secondEdges[edgeIndex - firstEdges.length];
|
|
||||||
}
|
|
||||||
// 求多边形在当前轴上的投影
|
// 求多边形在当前轴上的投影
|
||||||
var minA = new es.Ref(0);
|
|
||||||
var minB = new es.Ref(0);
|
|
||||||
var maxA = new es.Ref(0);
|
|
||||||
var maxB = new es.Ref(0);
|
|
||||||
var intervalDist = 0;
|
var intervalDist = 0;
|
||||||
this.getInterval(axis, first, minA, maxA);
|
var _a = this.getInterval(axis, first), minA = _a.min, maxA = _a.max;
|
||||||
this.getInterval(axis, second, minB, maxB);
|
var _b = this.getInterval(axis, second), minB = _b.min, maxB = _b.max;
|
||||||
// 将区间设为第二个多边形的空间。由轴上投影的位置差偏移。
|
// 将区间设为第二个多边形的空间。由轴上投影的位置差偏移。
|
||||||
var relativeIntervalOffset = es.Vector2.dot(polygonOffset, axis);
|
var relativeIntervalOffset = es.Vector2.dot(polygonOffset, axis);
|
||||||
minA.value += relativeIntervalOffset;
|
minA += relativeIntervalOffset;
|
||||||
maxA.value += relativeIntervalOffset;
|
maxA += relativeIntervalOffset;
|
||||||
// 检查多边形投影是否正在相交
|
// 检查多边形投影是否正在相交
|
||||||
intervalDist = this.intervalDistance(minA.value, maxA.value, minB.value, maxB.value);
|
intervalDist = this.intervalDistance(minA, maxA, minB, maxB);
|
||||||
if (intervalDist > 0)
|
if (intervalDist > 0)
|
||||||
isIntersecting = false;
|
isIntersecting = false;
|
||||||
// 对于多对多数据类型转换,添加一个Vector2?参数称为deltaMovement。为了提高速度,我们这里不使用它
|
// 对于多对多数据类型转换,添加一个Vector2?参数称为deltaMovement。为了提高速度,我们这里不使用它
|
||||||
@@ -10338,14 +10367,14 @@ var es;
|
|||||||
intervalDist = Math.abs(intervalDist);
|
intervalDist = Math.abs(intervalDist);
|
||||||
if (intervalDist < minIntervalDistance) {
|
if (intervalDist < minIntervalDistance) {
|
||||||
minIntervalDistance = intervalDist;
|
minIntervalDistance = intervalDist;
|
||||||
translationAxis = axis;
|
translationAxis.setTo(axis.x, axis.y);
|
||||||
if (es.Vector2.dot(translationAxis, polygonOffset) < 0)
|
if (es.Vector2.dot(translationAxis, polygonOffset) < 0)
|
||||||
translationAxis = new es.Vector2(-translationAxis.x, -translationAxis.y);
|
translationAxis = translationAxis.scale(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 利用最小平移向量对多边形进行推入。
|
// 利用最小平移向量对多边形进行推入。
|
||||||
result.normal = translationAxis;
|
result.normal = translationAxis;
|
||||||
result.minimumTranslationVector = new es.Vector2(-translationAxis.x * minIntervalDistance, -translationAxis.y * minIntervalDistance);
|
result.minimumTranslationVector = translationAxis.scale(minIntervalDistance * -1);
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -10355,18 +10384,22 @@ var es;
|
|||||||
* @param min
|
* @param min
|
||||||
* @param max
|
* @param max
|
||||||
*/
|
*/
|
||||||
ShapeCollisionsPolygon.getInterval = function (axis, polygon, min, max) {
|
ShapeCollisionsPolygon.getInterval = function (axis, polygon) {
|
||||||
var dot = es.Vector2.dot(polygon.points[0], axis);
|
var res = { min: 0, max: 0 };
|
||||||
min.value = max.value = dot;
|
var dot;
|
||||||
|
dot = es.Vector2.dot(polygon.points[0], axis);
|
||||||
|
res.max = dot;
|
||||||
|
res.min = dot;
|
||||||
for (var i = 1; i < polygon.points.length; i++) {
|
for (var i = 1; i < polygon.points.length; i++) {
|
||||||
dot = es.Vector2.dot(polygon.points[i], axis);
|
dot = es.Vector2.dot(polygon.points[i], axis);
|
||||||
if (dot < min.value) {
|
if (dot < res.min) {
|
||||||
min.value = dot;
|
res.min = dot;
|
||||||
}
|
}
|
||||||
else if (dot > max.value) {
|
else if (dot > res.max) {
|
||||||
max.value = dot;
|
res.max = dot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 计算[minA, maxA]和[minB, maxB]之间的距离。如果间隔重叠,距离是负的
|
* 计算[minA, maxA]和[minB, maxB]之间的距离。如果间隔重叠,距离是负的
|
||||||
@@ -13611,7 +13644,7 @@ var es;
|
|||||||
rect.height = Math.trunc(rect.height * scale.y);
|
rect.height = Math.trunc(rect.height * scale.y);
|
||||||
};
|
};
|
||||||
RectangleExt.translate = function (rect, vec) {
|
RectangleExt.translate = function (rect, vec) {
|
||||||
rect.location.add(vec);
|
rect.location.addEqual(vec);
|
||||||
};
|
};
|
||||||
return RectangleExt;
|
return RectangleExt;
|
||||||
}());
|
}());
|
||||||
@@ -14453,9 +14486,9 @@ var es;
|
|||||||
var angle = Math.atan2(dirToCircle.y, dirToCircle.x);
|
var angle = Math.atan2(dirToCircle.y, dirToCircle.x);
|
||||||
var stepSize = Math.PI / this.lineCountForCircleApproximation;
|
var stepSize = Math.PI / this.lineCountForCircleApproximation;
|
||||||
var startAngle = angle + es.MathHelper.PiOver2;
|
var startAngle = angle + es.MathHelper.PiOver2;
|
||||||
var lastPt = es.MathHelper.angleToVector(startAngle, radius).add(position);
|
var lastPt = es.MathHelper.angleToVector(startAngle, radius).addEqual(position);
|
||||||
for (var i = 1; i < this.lineCountForCircleApproximation; i++) {
|
for (var i = 1; i < this.lineCountForCircleApproximation; i++) {
|
||||||
var nextPt = es.MathHelper.angleToVector(startAngle + i * stepSize, radius).add(position);
|
var nextPt = es.MathHelper.angleToVector(startAngle + i * stepSize, radius).addEqual(position);
|
||||||
this.addLineOccluder(lastPt, nextPt);
|
this.addLineOccluder(lastPt, nextPt);
|
||||||
lastPt = nextPt;
|
lastPt = nextPt;
|
||||||
}
|
}
|
||||||
|
|||||||
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
@@ -54,7 +54,7 @@ module es {
|
|||||||
/**
|
/**
|
||||||
* 该刚体的速度
|
* 该刚体的速度
|
||||||
*/
|
*/
|
||||||
public velocity: Vector2 = es.Vector2.zero;
|
public velocity: Vector2 = Vector2.zero;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 质量为0的刚体被认为是不可移动的。改变速度和碰撞对它们没有影响
|
* 质量为0的刚体被认为是不可移动的。改变速度和碰撞对它们没有影响
|
||||||
@@ -151,14 +151,17 @@ module es {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldUseGravity)
|
if (this.shouldUseGravity)
|
||||||
this.velocity = Vector2.add(this.velocity, Vector2.multiplyScaler(Physics.gravity, Time.deltaTime));
|
this.velocity.addEqual(Physics.gravity.scale(Time.deltaTime));
|
||||||
|
|
||||||
this.entity.position = Vector2.add(this.entity.position, Vector2.multiplyScaler(this.velocity, Time.deltaTime));
|
this.entity.position = Vector2.add(this.entity.position, Vector2.multiplyScaler(this.velocity, Time.deltaTime));
|
||||||
|
|
||||||
let collisionResult = new CollisionResult();
|
let collisionResult = new CollisionResult();
|
||||||
|
|
||||||
// 捞取我们在新的位置上可能会碰撞到的任何东西
|
// 捞取我们在新的位置上可能会碰撞到的任何东西
|
||||||
let neighbors = Physics.boxcastBroadphaseExcludingSelfNonRect(this._collider, this._collider.collidesWithLayers.value);
|
let neighbors = Physics.boxcastBroadphaseExcludingSelf(this._collider, this._collider.bounds, this._collider.collidesWithLayers.value);
|
||||||
for (let neighbor of neighbors) {
|
for (const neighbor of neighbors) {
|
||||||
|
if (!neighbor)
|
||||||
|
continue;
|
||||||
|
|
||||||
// 如果邻近的对撞机是同一个实体,则忽略它
|
// 如果邻近的对撞机是同一个实体,则忽略它
|
||||||
if (neighbor.entity.equals(this.entity)) {
|
if (neighbor.entity.equals(this.entity)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -173,9 +176,8 @@ module es {
|
|||||||
} else {
|
} else {
|
||||||
// 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的
|
// 没有ArcadeRigidbody,所以我们假设它是不动的,只移动我们自己的
|
||||||
this.entity.position = Vector2.subtract(this.entity.position, collisionResult.minimumTranslationVector);
|
this.entity.position = Vector2.subtract(this.entity.position, collisionResult.minimumTranslationVector);
|
||||||
let relativeVelocity = this.velocity.clone();
|
const relativeVelocity = this.calculateResponseVelocity(this.velocity, collisionResult.minimumTranslationVector);
|
||||||
this.calculateResponseVelocity(relativeVelocity, collisionResult.minimumTranslationVector, relativeVelocity);
|
this.velocity.addEqual(relativeVelocity);
|
||||||
this.velocity.add(relativeVelocity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,12 +190,12 @@ module es {
|
|||||||
*/
|
*/
|
||||||
public processOverlap(other: ArcadeRigidbody, minimumTranslationVector: Vector2) {
|
public processOverlap(other: ArcadeRigidbody, minimumTranslationVector: Vector2) {
|
||||||
if (this.isImmovable) {
|
if (this.isImmovable) {
|
||||||
other.entity.position = Vector2.add(other.entity.position, minimumTranslationVector);
|
other.entity.position = other.entity.position.add(minimumTranslationVector);
|
||||||
} else if (other.isImmovable) {
|
} else if (other.isImmovable) {
|
||||||
other.entity.position = Vector2.subtract(other.entity.position, minimumTranslationVector);
|
this.entity.position = this.entity.position.sub(minimumTranslationVector);
|
||||||
} else {
|
} else {
|
||||||
this.entity.position = Vector2.subtract(this.entity.position, Vector2.multiplyScaler(minimumTranslationVector, 0.5));
|
this.entity.position = this.entity.position.sub(minimumTranslationVector.scale(0.5));
|
||||||
other.entity.position = Vector2.add(other.entity.position, Vector2.multiplyScaler(minimumTranslationVector, 0.5));
|
other.entity.position = other.entity.position.add(minimumTranslationVector.scale(0.5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,15 +210,16 @@ module es {
|
|||||||
// 然后,响应的一部分会根据质量加到每个物体上
|
// 然后,响应的一部分会根据质量加到每个物体上
|
||||||
let relativeVelocity = Vector2.subtract(this.velocity, other.velocity);
|
let relativeVelocity = Vector2.subtract(this.velocity, other.velocity);
|
||||||
|
|
||||||
this.calculateResponseVelocity(relativeVelocity, minimumTranslationVector, relativeVelocity);
|
relativeVelocity = this.calculateResponseVelocity(relativeVelocity, minimumTranslationVector);
|
||||||
|
|
||||||
// 现在,我们使用质量来线性缩放两个刚体上的响应
|
// 现在,我们使用质量来线性缩放两个刚体上的响应
|
||||||
let totalinverseMass = this._inverseMass + other._inverseMass;
|
const totalinverseMass = this._inverseMass + other._inverseMass;
|
||||||
let ourResponseFraction = this._inverseMass / totalinverseMass;
|
const ourResponseFraction = this._inverseMass / totalinverseMass;
|
||||||
let otherResponseFraction = other._inverseMass / totalinverseMass;
|
const otherResponseFraction = other._inverseMass / totalinverseMass;
|
||||||
|
|
||||||
this.velocity = Vector2.add(this.velocity, Vector2.multiplyScaler(relativeVelocity, ourResponseFraction));
|
|
||||||
other.velocity = Vector2.subtract(other.velocity, Vector2.multiplyScaler(relativeVelocity, otherResponseFraction));
|
this.velocity = Vector2.add(this.velocity, relativeVelocity.scale(ourResponseFraction));
|
||||||
|
other.velocity = Vector2.subtract(other.velocity, relativeVelocity.scale(otherResponseFraction));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -225,11 +228,10 @@ module es {
|
|||||||
* @param minimumTranslationVector
|
* @param minimumTranslationVector
|
||||||
* @param responseVelocity
|
* @param responseVelocity
|
||||||
*/
|
*/
|
||||||
public calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2, responseVelocity: Vector2 = es.Vector2.zero) {
|
public calculateResponseVelocity(relativeVelocity: Vector2, minimumTranslationVector: Vector2) {
|
||||||
// 首先,我们得到反方向的归一化MTV:表面法线
|
// 首先,我们得到反方向的归一化MTV:表面法线
|
||||||
let inverseMTV = Vector2.multiplyScaler(minimumTranslationVector, -1);
|
let inverseMTV = Vector2.multiplyScaler(minimumTranslationVector, -1);
|
||||||
let normal = Vector2.normalize(inverseMTV);
|
let normal = Vector2.normalize(inverseMTV);
|
||||||
|
|
||||||
// 速度是沿碰撞法线和碰撞平面分解的。
|
// 速度是沿碰撞法线和碰撞平面分解的。
|
||||||
// 弹性将影响沿法线的响应(法线速度分量),摩擦力将影响速度的切向分量(切向速度分量)
|
// 弹性将影响沿法线的响应(法线速度分量),摩擦力将影响速度的切向分量(切向速度分量)
|
||||||
let n = Vector2.dot(relativeVelocity, normal);
|
let n = Vector2.dot(relativeVelocity, normal);
|
||||||
@@ -246,10 +248,10 @@ module es {
|
|||||||
coefficientOfFriction = 1.01;
|
coefficientOfFriction = 1.01;
|
||||||
|
|
||||||
// 弹性影响速度的法向分量,摩擦力影响速度的切向分量
|
// 弹性影响速度的法向分量,摩擦力影响速度的切向分量
|
||||||
let r = Vector2.multiplyScaler(normalVelocityComponent, -(1 + this._elasticity))
|
return normalVelocityComponent
|
||||||
.subtract(Vector2.multiplyScaler(tangentialVelocityComponent, coefficientOfFriction));
|
.scale(1 + this._elasticity)
|
||||||
responseVelocity.x = r.x;
|
.sub(tangentialVelocityComponent.scale(coefficientOfFriction))
|
||||||
responseVelocity.y = r.y;
|
.scale(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -294,9 +294,8 @@ module es {
|
|||||||
initialRayOriginY - i * this._verticalDistanceBetweenRays
|
initialRayOriginY - i * this._verticalDistanceBetweenRays
|
||||||
);
|
);
|
||||||
|
|
||||||
// if we are grounded we will include oneWayPlatforms
|
// 如果我们接地,我们将只在第一条射线(底部)上包含 oneWayPlatforms。
|
||||||
// only on the first ray (the bottom one). this will allow us to
|
// 允许我们走上倾斜的 oneWayPlatforms
|
||||||
// walk up sloped oneWayPlatforms
|
|
||||||
if (
|
if (
|
||||||
i === 0 &&
|
i === 0 &&
|
||||||
this.supportSlopedOneWayPlatforms &&
|
this.supportSlopedOneWayPlatforms &&
|
||||||
|
|||||||
@@ -94,8 +94,8 @@ module es {
|
|||||||
public setLocalOffset(offset: Vector2): Collider {
|
public setLocalOffset(offset: Vector2): Collider {
|
||||||
if (!this._localOffset.equals(offset)) {
|
if (!this._localOffset.equals(offset)) {
|
||||||
this.unregisterColliderWithPhysicsSystem();
|
this.unregisterColliderWithPhysicsSystem();
|
||||||
this._localOffset = offset;
|
this._localOffset.setTo(offset.x, offset.y);
|
||||||
this._localOffsetLength = this._localOffset.length();
|
this._localOffsetLength = this._localOffset.magnitude();
|
||||||
this._isPositionDirty = true;
|
this._isPositionDirty = true;
|
||||||
this.registerColliderWithPhysicsSystem();
|
this.registerColliderWithPhysicsSystem();
|
||||||
}
|
}
|
||||||
@@ -213,15 +213,15 @@ module es {
|
|||||||
*/
|
*/
|
||||||
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult = new CollisionResult()): boolean {
|
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult = new CollisionResult()): boolean {
|
||||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||||
let oldPosition = this.entity.position.clone();
|
const oldPosition = this.entity.position;
|
||||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
this.entity.position = this.entity.position.add(motion);
|
||||||
|
|
||||||
let didCollide = this.shape.collidesWithShape(collider.shape, result);
|
const didCollide = this.shape.collidesWithShape(collider.shape, result);
|
||||||
if (didCollide)
|
if (didCollide)
|
||||||
result.collider = collider;
|
result.collider = collider;
|
||||||
|
|
||||||
// 将图形位置返回到检查前的位置
|
// 将图形位置返回到检查前的位置
|
||||||
this.entity.position = oldPosition.clone();
|
this.entity.position = oldPosition;
|
||||||
|
|
||||||
return didCollide;
|
return didCollide;
|
||||||
}
|
}
|
||||||
@@ -237,6 +237,7 @@ module es {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.collider = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,10 +13,9 @@ module es {
|
|||||||
// 第一点和最后一点决不能相同。我们想要一个开放的多边形
|
// 第一点和最后一点决不能相同。我们想要一个开放的多边形
|
||||||
let isPolygonClosed = points[0] == points[points.length - 1];
|
let isPolygonClosed = points[0] == points[points.length - 1];
|
||||||
|
|
||||||
let linqPoints = new es.List(points);
|
|
||||||
// 最后一个移除
|
// 最后一个移除
|
||||||
if (isPolygonClosed)
|
if (isPolygonClosed)
|
||||||
linqPoints.remove(linqPoints.last());
|
points = points.slice(0, points.length - 1);
|
||||||
|
|
||||||
let center = Polygon.findPolygonCenter(points);
|
let center = Polygon.findPolygonCenter(points);
|
||||||
this.setLocalOffset(center);
|
this.setLocalOffset(center);
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ module es {
|
|||||||
let _internalcollisionResult: CollisionResult = new CollisionResult();
|
let _internalcollisionResult: CollisionResult = new CollisionResult();
|
||||||
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
||||||
// 如果碰撞 则退回之前的移动量
|
// 如果碰撞 则退回之前的移动量
|
||||||
motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
motion.sub(_internalcollisionResult.minimumTranslationVector);
|
||||||
|
|
||||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||||
if (_internalcollisionResult.collider != null) {
|
if (_internalcollisionResult.collider != null) {
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ module es {
|
|||||||
t = MathHelper.clamp01(t);
|
t = MathHelper.clamp01(t);
|
||||||
let oneMinusT = 1 - t;
|
let oneMinusT = 1 - t;
|
||||||
return new Vector2(oneMinusT * oneMinusT).multiply(p0)
|
return new Vector2(oneMinusT * oneMinusT).multiply(p0)
|
||||||
.add(new Vector2(2 * oneMinusT * t).multiply(p1))
|
.addEqual(new Vector2(2 * oneMinusT * t).multiply(p1))
|
||||||
.add(new Vector2(t * t).multiply(p2));
|
.addEqual(new Vector2(t * t).multiply(p2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,9 +31,9 @@ module es {
|
|||||||
t = MathHelper.clamp01(t);
|
t = MathHelper.clamp01(t);
|
||||||
let oneMinusT = 1 - t;
|
let oneMinusT = 1 - t;
|
||||||
return new Vector2(oneMinusT * oneMinusT * oneMinusT).multiply(start)
|
return new Vector2(oneMinusT * oneMinusT * oneMinusT).multiply(start)
|
||||||
.add(new Vector2(3 * oneMinusT * oneMinusT * t).multiply(firstControlPoint))
|
.addEqual(new Vector2(3 * oneMinusT * oneMinusT * t).multiply(firstControlPoint))
|
||||||
.add(new Vector2(3 * oneMinusT * t * t).multiply(secondControlPoint))
|
.addEqual(new Vector2(3 * oneMinusT * t * t).multiply(secondControlPoint))
|
||||||
.add(new Vector2(t * t * t).multiply(end));
|
.addEqual(new Vector2(t * t * t).multiply(end));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,7 +45,7 @@ module es {
|
|||||||
*/
|
*/
|
||||||
public static getFirstDerivative(p0: Vector2, p1: Vector2, p2: Vector2, t: number) {
|
public static getFirstDerivative(p0: Vector2, p1: Vector2, p2: Vector2, t: number) {
|
||||||
return new Vector2(2 * (1 - t)).multiply(Vector2.subtract(p1, p0))
|
return new Vector2(2 * (1 - t)).multiply(Vector2.subtract(p1, p0))
|
||||||
.add(new Vector2(2 * t).multiply(Vector2.subtract(p2, p1)));
|
.addEqual(new Vector2(2 * t).multiply(Vector2.subtract(p2, p1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,8 +61,8 @@ module es {
|
|||||||
t = MathHelper.clamp01(t);
|
t = MathHelper.clamp01(t);
|
||||||
let oneMunusT = 1 - t;
|
let oneMunusT = 1 - t;
|
||||||
return new Vector2(3 * oneMunusT * oneMunusT).multiply(Vector2.subtract(firstControlPoint, start))
|
return new Vector2(3 * oneMunusT * oneMunusT).multiply(Vector2.subtract(firstControlPoint, start))
|
||||||
.add(new Vector2(6 * oneMunusT * t).multiply(Vector2.subtract(secondControlPoint, firstControlPoint)))
|
.addEqual(new Vector2(6 * oneMunusT * t).multiply(Vector2.subtract(secondControlPoint, firstControlPoint)))
|
||||||
.add(new Vector2(3 * t * t).multiply(Vector2.subtract(end, secondControlPoint)));
|
.addEqual(new Vector2(3 * t * t).multiply(Vector2.subtract(end, secondControlPoint)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -34,10 +34,10 @@ module es {
|
|||||||
if (index % 3 == 0) {
|
if (index % 3 == 0) {
|
||||||
let delta = Vector2.subtract(point, this._points[index]);
|
let delta = Vector2.subtract(point, this._points[index]);
|
||||||
if (index > 0)
|
if (index > 0)
|
||||||
this._points[index - 1].add(delta);
|
this._points[index - 1].addEqual(delta);
|
||||||
|
|
||||||
if (index + 1 < this._points.length)
|
if (index + 1 < this._points.length)
|
||||||
this._points[index + 1].add(delta);
|
this._points[index + 1].addEqual(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._points[index] = point;
|
this._points[index] = point;
|
||||||
|
|||||||
@@ -120,11 +120,12 @@ module es {
|
|||||||
* @param value
|
* @param value
|
||||||
*/
|
*/
|
||||||
public static normalize(value: Vector2) {
|
public static normalize(value: Vector2) {
|
||||||
let nValue = new Vector2(value.x, value.y);
|
const d = value.distance();
|
||||||
let val = 1 / Math.sqrt((nValue.x * nValue.x) + (nValue.y * nValue.y));
|
if (d > 0) {
|
||||||
nValue.x *= val;
|
return new Vector2(value.x / d, value.y / d);
|
||||||
nValue.y *= val;
|
} else {
|
||||||
return nValue;
|
return new Vector2(0, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -260,13 +261,22 @@ module es {
|
|||||||
MathHelper.smoothStep(value1.y, value2.y, amount));
|
MathHelper.smoothStep(value1.y, value2.y, amount));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public setTo(x: number, y: number) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
*/
|
*/
|
||||||
public add(value: Vector2): Vector2 {
|
public add(v: Vector2): Vector2 {
|
||||||
this.x += value.x;
|
return new Vector2(this.x + v.x, this.y + v.y);
|
||||||
this.y += value.y;
|
}
|
||||||
|
|
||||||
|
public addEqual(v: Vector2): Vector2 {
|
||||||
|
this.x += v.x;
|
||||||
|
this.y += v.y;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,19 +322,37 @@ module es {
|
|||||||
* @param value 要减去的Vector2
|
* @param value 要减去的Vector2
|
||||||
* @returns 当前Vector2
|
* @returns 当前Vector2
|
||||||
*/
|
*/
|
||||||
public subtract(value: Vector2) {
|
public sub(value: Vector2) {
|
||||||
this.x -= value.x;
|
return new Vector2(this.x - value.x, this.y - value.y);
|
||||||
this.y -= value.y;
|
}
|
||||||
|
|
||||||
|
public subEqual(v: Vector2): Vector2 {
|
||||||
|
this.x -= v.x;
|
||||||
|
this.y -= v.y;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param size
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
public scale(size: number): Vector2 {
|
||||||
|
return new Vector2(this.x * size, this.y * size);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将这个Vector2变成一个方向相同的单位向量
|
* 将这个Vector2变成一个方向相同的单位向量
|
||||||
*/
|
*/
|
||||||
public normalize() {
|
public normalize() {
|
||||||
let val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
|
const d = this.distance();
|
||||||
this.x *= val;
|
if (d > 0) {
|
||||||
this.y *= val;
|
this.setTo(this.x / d, this.y / d);
|
||||||
|
return this;
|
||||||
|
} else {
|
||||||
|
this.setTo(0, 1);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 返回它的长度 */
|
/** 返回它的长度 */
|
||||||
@@ -332,6 +360,18 @@ module es {
|
|||||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public magnitude(): number {
|
||||||
|
return this.distance();
|
||||||
|
}
|
||||||
|
|
||||||
|
public distance(v?: Vector2): number {
|
||||||
|
if (!v) {
|
||||||
|
v = Vector2.zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.sqrt(Math.pow(this.x - v.x, 2) + Math.pow(this.y - v.y, 2));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回该Vector2的平方长度
|
* 返回该Vector2的平方长度
|
||||||
* @returns 这个Vector2的平方长度
|
* @returns 这个Vector2的平方长度
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ module es {
|
|||||||
let collider = colliders[i];
|
let collider = colliders[i];
|
||||||
|
|
||||||
let neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
let neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
||||||
for (let j = 0; j < neighbors.size; j++) {
|
for (let j = 0; j < neighbors.length; j++) {
|
||||||
let neighbor = neighbors[j];
|
let neighbor = neighbors[j];
|
||||||
// 我们至少需要一个碰撞器作为触发器
|
// 我们至少需要一个碰撞器作为触发器
|
||||||
if (!collider.isTrigger && !neighbor.isTrigger)
|
if (!collider.isTrigger && !neighbor.isTrigger)
|
||||||
|
|||||||
@@ -54,7 +54,10 @@ module es {
|
|||||||
this.points = points;
|
this.points = points;
|
||||||
this.recalculateCenterAndEdgeNormals();
|
this.recalculateCenterAndEdgeNormals();
|
||||||
|
|
||||||
this._originalPoints = this.points.slice();
|
this._originalPoints = [];
|
||||||
|
this.points.forEach(p => {
|
||||||
|
this._originalPoints.push(p.clone());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -301,7 +304,7 @@ module es {
|
|||||||
*/
|
*/
|
||||||
public containsPoint(point: Vector2) {
|
public containsPoint(point: Vector2) {
|
||||||
// 将点归一化到多边形坐标空间中
|
// 将点归一化到多边形坐标空间中
|
||||||
point.subtract(this.position);
|
point.sub(this.position);
|
||||||
|
|
||||||
let isInside = false;
|
let isInside = false;
|
||||||
for (let i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
|
for (let i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ module es {
|
|||||||
public static polygonToPolygon(first: Polygon, second: Polygon, result: CollisionResult): boolean {
|
public static polygonToPolygon(first: Polygon, second: Polygon, result: CollisionResult): boolean {
|
||||||
let isIntersecting = true;
|
let isIntersecting = true;
|
||||||
|
|
||||||
let firstEdges = first.edgeNormals.slice();
|
const firstEdges = first.edgeNormals;
|
||||||
let secondEdges = second.edgeNormals.slice();
|
const secondEdges = second.edgeNormals;
|
||||||
let minIntervalDistance = Number.POSITIVE_INFINITY;
|
let minIntervalDistance = Number.POSITIVE_INFINITY;
|
||||||
let translationAxis = es.Vector2.zero;
|
let translationAxis = es.Vector2.zero;
|
||||||
let polygonOffset = Vector2.subtract(first.position, second.position);
|
let polygonOffset = Vector2.subtract(first.position, second.position);
|
||||||
@@ -20,28 +20,20 @@ module es {
|
|||||||
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) {
|
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) {
|
||||||
// 1. 找出当前多边形是否相交
|
// 1. 找出当前多边形是否相交
|
||||||
// 多边形的归一化轴垂直于缓存给我们的当前边
|
// 多边形的归一化轴垂直于缓存给我们的当前边
|
||||||
if (edgeIndex < firstEdges.length) {
|
axis = edgeIndex < firstEdges.length ? firstEdges[edgeIndex] : secondEdges[edgeIndex - firstEdges.length];
|
||||||
axis = firstEdges[edgeIndex];
|
|
||||||
} else {
|
|
||||||
axis = secondEdges[edgeIndex - firstEdges.length];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 求多边形在当前轴上的投影
|
// 求多边形在当前轴上的投影
|
||||||
let minA = new Ref(0);
|
|
||||||
let minB = new Ref(0);
|
|
||||||
let maxA = new Ref(0);
|
|
||||||
let maxB = new Ref(0);
|
|
||||||
let intervalDist = 0;
|
let intervalDist = 0;
|
||||||
this.getInterval(axis, first, minA, maxA);
|
let {min: minA, max: maxA} = this.getInterval(axis, first);
|
||||||
this.getInterval(axis, second, minB, maxB);
|
const {min: minB, max: maxB} = this.getInterval(axis, second);
|
||||||
|
|
||||||
// 将区间设为第二个多边形的空间。由轴上投影的位置差偏移。
|
// 将区间设为第二个多边形的空间。由轴上投影的位置差偏移。
|
||||||
let relativeIntervalOffset = Vector2.dot(polygonOffset, axis);
|
let relativeIntervalOffset = Vector2.dot(polygonOffset, axis);
|
||||||
minA.value += relativeIntervalOffset;
|
minA += relativeIntervalOffset;
|
||||||
maxA.value += relativeIntervalOffset;
|
maxA += relativeIntervalOffset;
|
||||||
|
|
||||||
// 检查多边形投影是否正在相交
|
// 检查多边形投影是否正在相交
|
||||||
intervalDist = this.intervalDistance(minA.value, maxA.value, minB.value, maxB.value);
|
intervalDist = this.intervalDistance(minA, maxA, minB, maxB);
|
||||||
if (intervalDist > 0)
|
if (intervalDist > 0)
|
||||||
isIntersecting = false;
|
isIntersecting = false;
|
||||||
|
|
||||||
@@ -56,16 +48,16 @@ module es {
|
|||||||
intervalDist = Math.abs(intervalDist);
|
intervalDist = Math.abs(intervalDist);
|
||||||
if (intervalDist < minIntervalDistance) {
|
if (intervalDist < minIntervalDistance) {
|
||||||
minIntervalDistance = intervalDist;
|
minIntervalDistance = intervalDist;
|
||||||
translationAxis = axis;
|
translationAxis.setTo(axis.x, axis.y);
|
||||||
|
|
||||||
if (Vector2.dot(translationAxis, polygonOffset) < 0)
|
if (Vector2.dot(translationAxis, polygonOffset) < 0)
|
||||||
translationAxis = new Vector2(-translationAxis.x, -translationAxis.y);
|
translationAxis = translationAxis.scale(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 利用最小平移向量对多边形进行推入。
|
// 利用最小平移向量对多边形进行推入。
|
||||||
result.normal = translationAxis;
|
result.normal = translationAxis;
|
||||||
result.minimumTranslationVector = new Vector2(-translationAxis.x * minIntervalDistance, -translationAxis.y * minIntervalDistance);
|
result.minimumTranslationVector = translationAxis.scale(minIntervalDistance * -1);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -77,18 +69,21 @@ module es {
|
|||||||
* @param min
|
* @param min
|
||||||
* @param max
|
* @param max
|
||||||
*/
|
*/
|
||||||
public static getInterval(axis: Vector2, polygon: Polygon, min: Ref<number>, max: Ref<number>) {
|
public static getInterval(axis: Vector2, polygon: Polygon): {min: number, max: number} {
|
||||||
let dot = Vector2.dot(polygon.points[0], axis);
|
const res = {min: 0, max: 0};
|
||||||
min.value = max.value = dot;
|
let dot: number;
|
||||||
|
dot = Vector2.dot( polygon.points[0], axis);
|
||||||
|
res.max = dot;
|
||||||
|
res.min = 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.value) {
|
if (dot < res.min) {
|
||||||
min.value = dot;
|
res.min = dot;
|
||||||
} else if (dot > max.value) {
|
} else if (dot > res.max) {
|
||||||
max.value = dot;
|
res.max = dot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -98,7 +93,7 @@ module es {
|
|||||||
* @param minB
|
* @param minB
|
||||||
* @param maxB
|
* @param maxB
|
||||||
*/
|
*/
|
||||||
public static intervalDistance(minA: number, maxA: number, minB: number, maxB) {
|
public static intervalDistance(minA: number, maxA: number, minB: number, maxB: number) {
|
||||||
if (minA < minB)
|
if (minA < minB)
|
||||||
return minB - maxA;
|
return minB - maxA;
|
||||||
|
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ module es {
|
|||||||
* @param excludeCollider
|
* @param excludeCollider
|
||||||
* @param layerMask
|
* @param layerMask
|
||||||
*/
|
*/
|
||||||
public aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Set<Collider> {
|
public aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[] {
|
||||||
this._tempHashSet.clear();
|
this._tempHashSet.clear();
|
||||||
|
|
||||||
let p1 = this.cellCoords(bounds.x, bounds.y);
|
let p1 = this.cellCoords(bounds.x, bounds.y);
|
||||||
@@ -124,12 +124,12 @@ module es {
|
|||||||
for (let x = p1.x; x <= p2.x; x++) {
|
for (let x = p1.x; x <= p2.x; x++) {
|
||||||
for (let y = p1.y; y <= p2.y; y++) {
|
for (let y = p1.y; y <= p2.y; y++) {
|
||||||
let cell = this.cellAtPosition(x, y);
|
let cell = this.cellAtPosition(x, y);
|
||||||
if (cell == null)
|
if (!cell)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// 当cell不为空。循环并取回所有碰撞器
|
// 当cell不为空。循环并取回所有碰撞器
|
||||||
for (let i = 0; i < cell.length; i++) {
|
for (let i = 0; i < cell.length; i++) {
|
||||||
let collider = cell[i];
|
const collider = cell[i];
|
||||||
|
|
||||||
// 如果它是自身或者如果它不匹配我们的层掩码 跳过这个碰撞器
|
// 如果它是自身或者如果它不匹配我们的层掩码 跳过这个碰撞器
|
||||||
if (collider == excludeCollider || !Flags.isFlagSet(layerMask, collider.physicsLayer.value))
|
if (collider == excludeCollider || !Flags.isFlagSet(layerMask, collider.physicsLayer.value))
|
||||||
@@ -142,7 +142,7 @@ module es {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._tempHashSet;
|
return Array.from(this._tempHashSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -365,7 +365,7 @@ module es {
|
|||||||
* @param y
|
* @param y
|
||||||
*/
|
*/
|
||||||
public cellCoords(x: number, y: number): Vector2 {
|
public cellCoords(x: number, y: number): Vector2 {
|
||||||
return new Vector2(MathHelper.floorToInt(x * this._inverseCellSize), MathHelper.floorToInt(y * this._inverseCellSize));
|
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -389,7 +389,7 @@ module es {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static translate(rect: Rectangle, vec: Vector2) {
|
public static translate(rect: Rectangle, vec: Vector2) {
|
||||||
rect.location.add(vec);
|
rect.location.addEqual(vec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,9 +68,9 @@ module es {
|
|||||||
|
|
||||||
let stepSize = Math.PI / this.lineCountForCircleApproximation;
|
let stepSize = Math.PI / this.lineCountForCircleApproximation;
|
||||||
let startAngle = angle + MathHelper.PiOver2;
|
let startAngle = angle + MathHelper.PiOver2;
|
||||||
let lastPt = MathHelper.angleToVector(startAngle, radius).add(position);
|
let lastPt = MathHelper.angleToVector(startAngle, radius).addEqual(position);
|
||||||
for (let i = 1; i < this.lineCountForCircleApproximation; i ++) {
|
for (let i = 1; i < this.lineCountForCircleApproximation; i ++) {
|
||||||
let nextPt = MathHelper.angleToVector(startAngle + i * stepSize, radius).add(position);
|
let nextPt = MathHelper.angleToVector(startAngle + i * stepSize, radius).addEqual(position);
|
||||||
this.addLineOccluder(lastPt, nextPt);
|
this.addLineOccluder(lastPt, nextPt);
|
||||||
lastPt = nextPt;
|
lastPt = nextPt;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user