修复因Vector2.add/substract/divide/mutiply导致的计算错误
This commit is contained in:
14
source/bin/framework.d.ts
vendored
14
source/bin/framework.d.ts
vendored
@@ -895,9 +895,6 @@ declare module es {
|
|||||||
}
|
}
|
||||||
declare module es {
|
declare module es {
|
||||||
class BoxCollider extends Collider {
|
class BoxCollider extends Collider {
|
||||||
/**
|
|
||||||
* 零参数构造函数要求RenderableComponent在实体上,这样碰撞器可以在实体被添加到场景时调整自身的大小。
|
|
||||||
*/
|
|
||||||
constructor(x: number, y: number, width: number, height: number);
|
constructor(x: number, y: number, width: number, height: number);
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
@@ -1832,7 +1829,6 @@ declare module es {
|
|||||||
}
|
}
|
||||||
declare module es {
|
declare module es {
|
||||||
class Rectangle implements IEquatable<Rectangle> {
|
class Rectangle implements IEquatable<Rectangle> {
|
||||||
static emptyRectangle: Rectangle;
|
|
||||||
/**
|
/**
|
||||||
* 该矩形的左上角的x坐标
|
* 该矩形的左上角的x坐标
|
||||||
*/
|
*/
|
||||||
@@ -2017,6 +2013,7 @@ declare module es {
|
|||||||
* 获取这个矩形的哈希码
|
* 获取这个矩形的哈希码
|
||||||
*/
|
*/
|
||||||
getHashCode(): number;
|
getHashCode(): number;
|
||||||
|
clone(): Rectangle;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declare module es {
|
declare module es {
|
||||||
@@ -2195,14 +2192,7 @@ declare module es {
|
|||||||
* @returns 如果实例相同true 否则false
|
* @returns 如果实例相同true 否则false
|
||||||
*/
|
*/
|
||||||
equals(other: Vector2 | object): boolean;
|
equals(other: Vector2 | object): boolean;
|
||||||
}
|
clone(): Vector2;
|
||||||
}
|
|
||||||
declare module es {
|
|
||||||
class Vector3 {
|
|
||||||
x: number;
|
|
||||||
y: number;
|
|
||||||
z: number;
|
|
||||||
constructor(x: number, y: number, z: number);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declare module es {
|
declare module es {
|
||||||
|
|||||||
@@ -983,17 +983,13 @@ var es;
|
|||||||
})(DirtyType = es.DirtyType || (es.DirtyType = {}));
|
})(DirtyType = es.DirtyType || (es.DirtyType = {}));
|
||||||
var Transform = /** @class */ (function () {
|
var Transform = /** @class */ (function () {
|
||||||
function Transform(entity) {
|
function Transform(entity) {
|
||||||
/**
|
|
||||||
* 值会根据位置、旋转和比例自动重新计算
|
|
||||||
*/
|
|
||||||
this._localTransform = es.Matrix2D.identity;
|
|
||||||
/**
|
/**
|
||||||
* 值将自动从本地和父矩阵重新计算。
|
* 值将自动从本地和父矩阵重新计算。
|
||||||
*/
|
*/
|
||||||
this._worldTransform = es.Matrix2D.identity;
|
this._worldTransform = es.Matrix2D.identity;
|
||||||
this._rotationMatrix = es.Matrix2D.identity;
|
this._rotationMatrix = es.Matrix2D.identity;
|
||||||
this._translationMatrix = es.Matrix2D.identity;
|
this._translationMatrix = es.Matrix2D.identity;
|
||||||
this._scaleMatrix = es.Matrix2D.identity;
|
this._children = [];
|
||||||
this._worldToLocalTransform = es.Matrix2D.identity;
|
this._worldToLocalTransform = es.Matrix2D.identity;
|
||||||
this._worldInverseTransform = es.Matrix2D.identity;
|
this._worldInverseTransform = es.Matrix2D.identity;
|
||||||
this._position = es.Vector2.zero;
|
this._position = es.Vector2.zero;
|
||||||
@@ -1004,7 +1000,6 @@ var es;
|
|||||||
this._localRotation = 0;
|
this._localRotation = 0;
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
this.scale = this._localScale = es.Vector2.one;
|
this.scale = this._localScale = es.Vector2.one;
|
||||||
this._children = [];
|
|
||||||
}
|
}
|
||||||
Object.defineProperty(Transform.prototype, "childCount", {
|
Object.defineProperty(Transform.prototype, "childCount", {
|
||||||
/**
|
/**
|
||||||
@@ -1111,7 +1106,7 @@ var es;
|
|||||||
get: function () {
|
get: function () {
|
||||||
this.updateTransform();
|
this.updateTransform();
|
||||||
if (this._positionDirty) {
|
if (this._positionDirty) {
|
||||||
if (!this.parent) {
|
if (this.parent == null) {
|
||||||
this._position = this._localPosition;
|
this._position = this._localPosition;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -1255,7 +1250,7 @@ var es;
|
|||||||
if (position.equals(this._position))
|
if (position.equals(this._position))
|
||||||
return this;
|
return this;
|
||||||
this._position = position;
|
this._position = position;
|
||||||
if (this.parent) {
|
if (this.parent != null) {
|
||||||
this.localPosition = es.Vector2.transform(this._position, this._worldToLocalTransform);
|
this.localPosition = es.Vector2.transform(this._position, this._worldToLocalTransform);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -1355,7 +1350,7 @@ var es;
|
|||||||
};
|
};
|
||||||
Transform.prototype.updateTransform = function () {
|
Transform.prototype.updateTransform = function () {
|
||||||
if (this.hierarchyDirty != DirtyType.clean) {
|
if (this.hierarchyDirty != DirtyType.clean) {
|
||||||
if (this.parent)
|
if (this.parent != null)
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
if (this._localDirty) {
|
if (this._localDirty) {
|
||||||
if (this._localPositionDirty) {
|
if (this._localPositionDirty) {
|
||||||
@@ -1372,7 +1367,7 @@ var es;
|
|||||||
}
|
}
|
||||||
this._localTransform = this._scaleMatrix.multiply(this._rotationMatrix);
|
this._localTransform = this._scaleMatrix.multiply(this._rotationMatrix);
|
||||||
this._localTransform = this._localTransform.multiply(this._translationMatrix);
|
this._localTransform = this._localTransform.multiply(this._translationMatrix);
|
||||||
if (!this.parent) {
|
if (this.parent == null) {
|
||||||
this._worldTransform = this._localTransform;
|
this._worldTransform = this._localTransform;
|
||||||
this._rotation = this._localRotation;
|
this._rotation = this._localRotation;
|
||||||
this._scale = this._localScale;
|
this._scale = this._localScale;
|
||||||
@@ -1380,7 +1375,7 @@ var es;
|
|||||||
}
|
}
|
||||||
this._localDirty = false;
|
this._localDirty = false;
|
||||||
}
|
}
|
||||||
if (this.parent) {
|
if (this.parent != null) {
|
||||||
this._worldTransform = this._localTransform.multiply(this.parent._worldTransform);
|
this._worldTransform = this._localTransform.multiply(this.parent._worldTransform);
|
||||||
this._rotation = this._localRotation + this.parent._rotation;
|
this._rotation = this._localRotation + this.parent._rotation;
|
||||||
this._scale = es.Vector2.multiply(this.parent._scale, this._localScale);
|
this._scale = es.Vector2.multiply(this.parent._scale, this._localScale);
|
||||||
@@ -1405,8 +1400,6 @@ var es;
|
|||||||
this.entity.onTransformChanged(transform.Component.scale);
|
this.entity.onTransformChanged(transform.Component.scale);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!this._children)
|
|
||||||
this._children = [];
|
|
||||||
// 告诉子项发生了变换
|
// 告诉子项发生了变换
|
||||||
for (var i = 0; i < this._children.length; i++)
|
for (var i = 0; i < this._children.length; i++)
|
||||||
this._children[i].setDirty(dirtyFlagType);
|
this._children[i].setDirty(dirtyFlagType);
|
||||||
@@ -1614,7 +1607,7 @@ var es;
|
|||||||
* @param collisionResult
|
* @param collisionResult
|
||||||
*/
|
*/
|
||||||
Mover.prototype.calculateMovement = function (motion, collisionResult) {
|
Mover.prototype.calculateMovement = function (motion, collisionResult) {
|
||||||
if (!this.entity.getComponent(es.Collider) || !this._triggerHelper) {
|
if (this.entity.getComponent(es.Collider) == null || this._triggerHelper == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// 移动所有的非触发碰撞器并获得最近的碰撞
|
// 移动所有的非触发碰撞器并获得最近的碰撞
|
||||||
@@ -1625,7 +1618,7 @@ var es;
|
|||||||
if (collider.isTrigger)
|
if (collider.isTrigger)
|
||||||
return "continue";
|
return "continue";
|
||||||
// 获取我们在新位置可能发生碰撞的任何东西
|
// 获取我们在新位置可能发生碰撞的任何东西
|
||||||
var bounds = collider.bounds;
|
var bounds = collider.bounds.clone();
|
||||||
bounds.x += motion.x;
|
bounds.x += motion.x;
|
||||||
bounds.y += motion.y;
|
bounds.y += motion.y;
|
||||||
var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers.value);
|
var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers.value);
|
||||||
@@ -1637,7 +1630,7 @@ var es;
|
|||||||
var _internalcollisionResult = new es.CollisionResult();
|
var _internalcollisionResult = new es.CollisionResult();
|
||||||
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
||||||
// 如果碰撞 则退回之前的移动量
|
// 如果碰撞 则退回之前的移动量
|
||||||
motion = motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
||||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||||
if (_internalcollisionResult.collider != null) {
|
if (_internalcollisionResult.collider != null) {
|
||||||
collisionResult = _internalcollisionResult;
|
collisionResult = _internalcollisionResult;
|
||||||
@@ -1703,7 +1696,7 @@ var es;
|
|||||||
return false;
|
return false;
|
||||||
var didCollide = false;
|
var didCollide = false;
|
||||||
// 获取我们在新位置可能发生碰撞的任何东西
|
// 获取我们在新位置可能发生碰撞的任何东西
|
||||||
this.entity.position.add(motion);
|
this.entity.position = es.Vector2.add(this.entity.position, motion);
|
||||||
// 获取任何可能在新位置发生碰撞的东西
|
// 获取任何可能在新位置发生碰撞的东西
|
||||||
var neighbors = es.Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers.value);
|
var neighbors = es.Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers.value);
|
||||||
for (var i = 0; i < neighbors.size; i++) {
|
for (var i = 0; i < neighbors.size; i++) {
|
||||||
@@ -1739,6 +1732,10 @@ var es;
|
|||||||
__extends(Collider, _super);
|
__extends(Collider, _super);
|
||||||
function Collider() {
|
function Collider() {
|
||||||
var _this = _super !== null && _super.apply(this, arguments) || this;
|
var _this = _super !== null && _super.apply(this, arguments) || this;
|
||||||
|
/**
|
||||||
|
* 如果这个碰撞器是一个触发器,它将不会引起碰撞,但它仍然会触发事件
|
||||||
|
*/
|
||||||
|
_this.isTrigger = false;
|
||||||
/**
|
/**
|
||||||
* 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法
|
* 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法
|
||||||
*/
|
*/
|
||||||
@@ -1902,8 +1899,8 @@ var es;
|
|||||||
*/
|
*/
|
||||||
Collider.prototype.collidesWith = function (collider, motion, result) {
|
Collider.prototype.collidesWith = function (collider, motion, result) {
|
||||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||||
var oldPosition = this.entity.position;
|
var oldPosition = this.entity.position.clone();
|
||||||
this.entity.position.add(motion);
|
this.entity.position = es.Vector2.add(this.entity.position, 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;
|
||||||
@@ -1921,9 +1918,6 @@ var es;
|
|||||||
(function (es) {
|
(function (es) {
|
||||||
var BoxCollider = /** @class */ (function (_super) {
|
var BoxCollider = /** @class */ (function (_super) {
|
||||||
__extends(BoxCollider, _super);
|
__extends(BoxCollider, _super);
|
||||||
/**
|
|
||||||
* 零参数构造函数要求RenderableComponent在实体上,这样碰撞器可以在实体被添加到场景时调整自身的大小。
|
|
||||||
*/
|
|
||||||
function BoxCollider(x, y, width, height) {
|
function BoxCollider(x, y, width, height) {
|
||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
_this._localOffset = new es.Vector2(x + width / 2, y + height / 2);
|
_this._localOffset = new es.Vector2(x + width / 2, y + height / 2);
|
||||||
@@ -2217,7 +2211,7 @@ var es;
|
|||||||
var BitSet = /** @class */ (function () {
|
var BitSet = /** @class */ (function () {
|
||||||
function BitSet(nbits) {
|
function BitSet(nbits) {
|
||||||
if (nbits === void 0) { nbits = 64; }
|
if (nbits === void 0) { nbits = 64; }
|
||||||
var length = nbits >> 6 >>> 0;
|
var length = nbits >> 6;
|
||||||
if ((nbits & BitSet.LONG_MASK) != 0)
|
if ((nbits & BitSet.LONG_MASK) != 0)
|
||||||
length++;
|
length++;
|
||||||
this._bits = new Array(length);
|
this._bits = new Array(length);
|
||||||
@@ -2237,7 +2231,7 @@ var es;
|
|||||||
this._bits[i] &= ~bs._bits[i];
|
this._bits[i] &= ~bs._bits[i];
|
||||||
};
|
};
|
||||||
BitSet.prototype.cardinality = function () {
|
BitSet.prototype.cardinality = function () {
|
||||||
var card = 0 >>> 0;
|
var card = 0;
|
||||||
for (var i = this._bits.length - 1; i >= 0; i--) {
|
for (var i = this._bits.length - 1; i >= 0; i--) {
|
||||||
var a = this._bits[i];
|
var a = this._bits[i];
|
||||||
if (a == 0)
|
if (a == 0)
|
||||||
@@ -4548,7 +4542,7 @@ var es;
|
|||||||
* 返回X=0, Y=0, Width=0, Height=0的矩形
|
* 返回X=0, Y=0, Width=0, Height=0的矩形
|
||||||
*/
|
*/
|
||||||
get: function () {
|
get: function () {
|
||||||
return this.emptyRectangle;
|
return new Rectangle();
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
@@ -4830,7 +4824,6 @@ var es;
|
|||||||
* @returns 矩形边框上离点最近的点
|
* @returns 矩形边框上离点最近的点
|
||||||
*/
|
*/
|
||||||
Rectangle.prototype.getClosestPointOnRectangleBorderToPoint = function (point, edgeNormal) {
|
Rectangle.prototype.getClosestPointOnRectangleBorderToPoint = function (point, edgeNormal) {
|
||||||
edgeNormal = es.Vector2.zero;
|
|
||||||
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
|
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
|
||||||
var res = new es.Vector2();
|
var res = new es.Vector2();
|
||||||
res.x = es.MathHelper.clamp(point.x, this.left, this.right);
|
res.x = es.MathHelper.clamp(point.x, this.left, this.right);
|
||||||
@@ -4914,8 +4907,8 @@ var es;
|
|||||||
* @param value2
|
* @param value2
|
||||||
*/
|
*/
|
||||||
Rectangle.overlap = function (value1, value2) {
|
Rectangle.overlap = function (value1, value2) {
|
||||||
var x = Math.max(Math.max(value1.x, value2.x), 0);
|
var x = Math.max(value1.x, value2.x, 0);
|
||||||
var y = Math.max(Math.max(value1.y, value2.y), 0);
|
var y = Math.max(value1.y, value2.y, 0);
|
||||||
return new Rectangle(x, y, Math.max(Math.min(value1.right, value2.right) - x, 0), Math.max(Math.min(value1.bottom, value2.bottom) - y, 0));
|
return new Rectangle(x, y, Math.max(Math.min(value1.right, value2.right) - x, 0), Math.max(Math.min(value1.bottom, value2.bottom) - y, 0));
|
||||||
};
|
};
|
||||||
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
|
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
|
||||||
@@ -5039,7 +5032,9 @@ var es;
|
|||||||
Rectangle.prototype.getHashCode = function () {
|
Rectangle.prototype.getHashCode = function () {
|
||||||
return (this.x ^ this.y ^ this.width ^ this.height);
|
return (this.x ^ this.y ^ this.width ^ this.height);
|
||||||
};
|
};
|
||||||
Rectangle.emptyRectangle = new Rectangle();
|
Rectangle.prototype.clone = function () {
|
||||||
|
return new Rectangle(this.x, this.y, this.width, this.height);
|
||||||
|
};
|
||||||
return Rectangle;
|
return Rectangle;
|
||||||
}());
|
}());
|
||||||
es.Rectangle = Rectangle;
|
es.Rectangle = Rectangle;
|
||||||
@@ -5154,7 +5149,7 @@ var es;
|
|||||||
* @param value2
|
* @param value2
|
||||||
*/
|
*/
|
||||||
Vector2.add = function (value1, value2) {
|
Vector2.add = function (value1, value2) {
|
||||||
var result = new Vector2(0, 0);
|
var result = Vector2.zero;
|
||||||
result.x = value1.x + value2.x;
|
result.x = value1.x + value2.x;
|
||||||
result.y = value1.y + value2.y;
|
result.y = value1.y + value2.y;
|
||||||
return result;
|
return result;
|
||||||
@@ -5165,7 +5160,7 @@ var es;
|
|||||||
* @param value2
|
* @param value2
|
||||||
*/
|
*/
|
||||||
Vector2.divide = function (value1, value2) {
|
Vector2.divide = function (value1, value2) {
|
||||||
var result = new Vector2(0, 0);
|
var result = Vector2.zero;
|
||||||
result.x = value1.x / value2.x;
|
result.x = value1.x / value2.x;
|
||||||
result.y = value1.y / value2.y;
|
result.y = value1.y / value2.y;
|
||||||
return result;
|
return result;
|
||||||
@@ -5351,23 +5346,14 @@ var es;
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
Vector2.prototype.clone = function () {
|
||||||
|
return new Vector2(this.x, this.y);
|
||||||
|
};
|
||||||
return Vector2;
|
return Vector2;
|
||||||
}());
|
}());
|
||||||
es.Vector2 = Vector2;
|
es.Vector2 = Vector2;
|
||||||
})(es || (es = {}));
|
})(es || (es = {}));
|
||||||
var es;
|
var es;
|
||||||
(function (es) {
|
|
||||||
var Vector3 = /** @class */ (function () {
|
|
||||||
function Vector3(x, y, z) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
this.z = z;
|
|
||||||
}
|
|
||||||
return Vector3;
|
|
||||||
}());
|
|
||||||
es.Vector3 = Vector3;
|
|
||||||
})(es || (es = {}));
|
|
||||||
var es;
|
|
||||||
(function (es) {
|
(function (es) {
|
||||||
/**
|
/**
|
||||||
* 移动器使用的帮助器类,用于管理触发器碰撞器交互并调用itriggerlistener
|
* 移动器使用的帮助器类,用于管理触发器碰撞器交互并调用itriggerlistener
|
||||||
@@ -5830,7 +5816,7 @@ var es;
|
|||||||
* @param collider
|
* @param collider
|
||||||
*/
|
*/
|
||||||
SpatialHash.prototype.register = function (collider) {
|
SpatialHash.prototype.register = function (collider) {
|
||||||
var bounds = collider.bounds;
|
var bounds = collider.bounds.clone();
|
||||||
collider.registeredPhysicsBounds = bounds;
|
collider.registeredPhysicsBounds = bounds;
|
||||||
var p1 = this.cellCoords(bounds.x, bounds.y);
|
var p1 = this.cellCoords(bounds.x, bounds.y);
|
||||||
var p2 = this.cellCoords(bounds.right, bounds.bottom);
|
var p2 = this.cellCoords(bounds.right, bounds.bottom);
|
||||||
@@ -5854,7 +5840,7 @@ var es;
|
|||||||
* @param collider
|
* @param collider
|
||||||
*/
|
*/
|
||||||
SpatialHash.prototype.remove = function (collider) {
|
SpatialHash.prototype.remove = function (collider) {
|
||||||
var bounds = collider.registeredPhysicsBounds;
|
var bounds = collider.registeredPhysicsBounds.clone();
|
||||||
var p1 = this.cellCoords(bounds.x, bounds.y);
|
var p1 = this.cellCoords(bounds.x, bounds.y);
|
||||||
var p2 = this.cellCoords(bounds.right, bounds.bottom);
|
var p2 = this.cellCoords(bounds.right, bounds.bottom);
|
||||||
for (var x = p1.x; x <= p2.x; x++) {
|
for (var x = p1.x; x <= p2.x; x++) {
|
||||||
@@ -5891,7 +5877,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)
|
if (cell == null)
|
||||||
continue;
|
continue;
|
||||||
// 当cell不为空。循环并取回所有碰撞器
|
// 当cell不为空。循环并取回所有碰撞器
|
||||||
for (var i = 0; i < cell.length; i++) {
|
for (var i = 0; i < cell.length; i++) {
|
||||||
@@ -6104,7 +6090,7 @@ var es;
|
|||||||
// TODO: rayIntersects的性能够吗?需要测试它。Collisions.rectToLine可能更快
|
// TODO: rayIntersects的性能够吗?需要测试它。Collisions.rectToLine可能更快
|
||||||
// TODO: 如果边界检查返回更多数据,我们就不需要为BoxCollider检查做任何事情
|
// TODO: 如果边界检查返回更多数据,我们就不需要为BoxCollider检查做任何事情
|
||||||
// 在做形状测试之前先做一个边界检查
|
// 在做形状测试之前先做一个边界检查
|
||||||
var colliderBounds = potential.bounds;
|
var colliderBounds = potential.bounds.clone();
|
||||||
if (colliderBounds.rayIntersects(this._ray, fraction) && fraction.value <= 1) {
|
if (colliderBounds.rayIntersects(this._ray, fraction) && fraction.value <= 1) {
|
||||||
if (potential.shape.collidesWithLine(this._ray.start, this._ray.end, this._tempHit)) {
|
if (potential.shape.collidesWithLine(this._ray.start, this._ray.end, this._tempHit)) {
|
||||||
// 检查一下,我们应该排除这些射线,射线cast是否在碰撞器中开始
|
// 检查一下,我们应该排除这些射线,射线cast是否在碰撞器中开始
|
||||||
@@ -6247,7 +6233,7 @@ var es;
|
|||||||
Polygon.recenterPolygonVerts = function (points) {
|
Polygon.recenterPolygonVerts = function (points) {
|
||||||
var center = this.findPolygonCenter(points);
|
var center = this.findPolygonCenter(points);
|
||||||
for (var i = 0; i < points.length; i++)
|
for (var i = 0; i < points.length; i++)
|
||||||
points[i].subtract(center);
|
points[i] = es.Vector2.subtract(points[i], center);
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
|
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
|
||||||
@@ -6327,7 +6313,7 @@ var es;
|
|||||||
};
|
};
|
||||||
Polygon.prototype.recalculateBounds = function (collider) {
|
Polygon.prototype.recalculateBounds = function (collider) {
|
||||||
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
||||||
this.center = collider.localOffset;
|
this.center = collider.localOffset.clone();
|
||||||
if (collider.shouldColliderScaleAndRotateWithTransform) {
|
if (collider.shouldColliderScaleAndRotateWithTransform) {
|
||||||
var hasUnitScale = true;
|
var hasUnitScale = true;
|
||||||
var tempMat = void 0;
|
var tempMat = void 0;
|
||||||
@@ -6344,7 +6330,7 @@ var es;
|
|||||||
combinedMatrix = combinedMatrix.multiply(tempMat);
|
combinedMatrix = combinedMatrix.multiply(tempMat);
|
||||||
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动
|
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动
|
||||||
// 我们的偏移使角度为0我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
|
// 我们的偏移使角度为0我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
|
||||||
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * es.MathHelper.Rad2Deg;
|
var offsetAngle = Math.atan2(collider.localOffset.y * collider.entity.transform.scale.y, collider.localOffset.x * collider.entity.transform.scale.x) * es.MathHelper.Rad2Deg;
|
||||||
var offsetLength = hasUnitScale ? collider._localOffsetLength :
|
var offsetLength = hasUnitScale ? collider._localOffsetLength :
|
||||||
es.Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
|
es.Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
|
||||||
this.center = es.MathHelper.pointOnCirlce(es.Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle);
|
this.center = es.MathHelper.pointOnCirlce(es.Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle);
|
||||||
@@ -6360,7 +6346,7 @@ var es;
|
|||||||
}
|
}
|
||||||
this.position = es.Vector2.add(collider.entity.transform.position, this.center);
|
this.position = es.Vector2.add(collider.entity.transform.position, this.center);
|
||||||
this.bounds = es.Rectangle.rectEncompassingPoints(this.points);
|
this.bounds = es.Rectangle.rectEncompassingPoints(this.points);
|
||||||
this.bounds.location.add(this.position);
|
this.bounds.location = es.Vector2.add(this.bounds.location, this.position);
|
||||||
};
|
};
|
||||||
Polygon.prototype.overlaps = function (other) {
|
Polygon.prototype.overlaps = function (other) {
|
||||||
var result = new es.CollisionResult();
|
var result = new es.CollisionResult();
|
||||||
@@ -6972,7 +6958,7 @@ var es;
|
|||||||
var u = (c.x * b.y - c.y * b.x) / bDotDPerp;
|
var u = (c.x * b.y - c.y * b.x) / bDotDPerp;
|
||||||
if (u < 0 || u > 1)
|
if (u < 0 || u > 1)
|
||||||
return false;
|
return false;
|
||||||
intersection = intersection.add(a1).add(es.Vector2.multiply(new es.Vector2(t), b));
|
intersection = es.Vector2.add(a1, es.Vector2.multiply(new es.Vector2(t), b));
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
ShapeCollisions.lineToCircle = function (start, end, s, hit) {
|
ShapeCollisions.lineToCircle = function (start, end, s, hit) {
|
||||||
@@ -8087,7 +8073,7 @@ var es;
|
|||||||
result.x = Math.min(first.x, rect.x);
|
result.x = Math.min(first.x, rect.x);
|
||||||
result.y = Math.min(first.y, rect.y);
|
result.y = Math.min(first.y, rect.y);
|
||||||
result.width = Math.max(first.right, rect.right) - result.x;
|
result.width = Math.max(first.right, rect.right) - result.x;
|
||||||
result.height = Math.max(first.bottom, result.bottom) - result.y;
|
result.height = Math.max(first.bottom, rect.bottom) - result.y;
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
RectangleExt.getHalfRect = function (rect, edge) {
|
RectangleExt.getHalfRect = function (rect, edge) {
|
||||||
|
|||||||
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
@@ -1,9 +1,6 @@
|
|||||||
///<reference path="./Collider.ts" />
|
///<reference path="./Collider.ts" />
|
||||||
module es {
|
module es {
|
||||||
export class BoxCollider extends Collider {
|
export class BoxCollider extends Collider {
|
||||||
/**
|
|
||||||
* 零参数构造函数要求RenderableComponent在实体上,这样碰撞器可以在实体被添加到场景时调整自身的大小。
|
|
||||||
*/
|
|
||||||
constructor(x: number, y: number, width: number, height: number) {
|
constructor(x: number, y: number, width: number, height: number) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ module es {
|
|||||||
/**
|
/**
|
||||||
* 如果这个碰撞器是一个触发器,它将不会引起碰撞,但它仍然会触发事件
|
* 如果这个碰撞器是一个触发器,它将不会引起碰撞,但它仍然会触发事件
|
||||||
*/
|
*/
|
||||||
public isTrigger: boolean;
|
public isTrigger: boolean = false;
|
||||||
/**
|
/**
|
||||||
* 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法
|
* 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法
|
||||||
*/
|
*/
|
||||||
@@ -26,6 +26,7 @@ module es {
|
|||||||
* 存储这个允许我们始终能够安全地从物理系统中移除对撞机,即使它在试图移除它之前已经被移动了。
|
* 存储这个允许我们始终能够安全地从物理系统中移除对撞机,即使它在试图移除它之前已经被移动了。
|
||||||
*/
|
*/
|
||||||
public registeredPhysicsBounds: Rectangle = new Rectangle();
|
public registeredPhysicsBounds: Rectangle = new Rectangle();
|
||||||
|
|
||||||
public _localOffsetLength: number;
|
public _localOffsetLength: number;
|
||||||
public _isPositionDirty: boolean = true;
|
public _isPositionDirty: boolean = true;
|
||||||
public _isRotationDirty: boolean = true;
|
public _isRotationDirty: boolean = true;
|
||||||
@@ -183,8 +184,8 @@ module es {
|
|||||||
*/
|
*/
|
||||||
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean {
|
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean {
|
||||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||||
let oldPosition = this.entity.position;
|
let oldPosition = this.entity.position.clone();
|
||||||
this.entity.position.add(motion);
|
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||||
|
|
||||||
let didCollide = this.shape.collidesWithShape(collider.shape, result);
|
let didCollide = this.shape.collidesWithShape(collider.shape, result);
|
||||||
if (didCollide)
|
if (didCollide)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ module es {
|
|||||||
* @param collisionResult
|
* @param collisionResult
|
||||||
*/
|
*/
|
||||||
public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean {
|
public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean {
|
||||||
if (!this.entity.getComponent(Collider) || !this._triggerHelper) {
|
if (this.entity.getComponent(Collider) == null || this._triggerHelper == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ module es {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// 获取我们在新位置可能发生碰撞的任何东西
|
// 获取我们在新位置可能发生碰撞的任何东西
|
||||||
let bounds = collider.bounds;
|
let bounds = collider.bounds.clone();
|
||||||
bounds.x += motion.x;
|
bounds.x += motion.x;
|
||||||
bounds.y += motion.y;
|
bounds.y += motion.y;
|
||||||
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers.value);
|
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers.value);
|
||||||
@@ -47,7 +47,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 = motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
||||||
|
|
||||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||||
if (_internalcollisionResult.collider != null) {
|
if (_internalcollisionResult.collider != null) {
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ module es {
|
|||||||
let didCollide = false;
|
let didCollide = false;
|
||||||
|
|
||||||
// 获取我们在新位置可能发生碰撞的任何东西
|
// 获取我们在新位置可能发生碰撞的任何东西
|
||||||
this.entity.position.add(motion);
|
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||||
|
|
||||||
// 获取任何可能在新位置发生碰撞的东西
|
// 获取任何可能在新位置发生碰撞的东西
|
||||||
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers.value);
|
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers.value);
|
||||||
|
|||||||
@@ -28,20 +28,19 @@ module es {
|
|||||||
/**
|
/**
|
||||||
* 值会根据位置、旋转和比例自动重新计算
|
* 值会根据位置、旋转和比例自动重新计算
|
||||||
*/
|
*/
|
||||||
public _localTransform: Matrix2D = Matrix2D.identity;
|
public _localTransform: Matrix2D;
|
||||||
/**
|
/**
|
||||||
* 值将自动从本地和父矩阵重新计算。
|
* 值将自动从本地和父矩阵重新计算。
|
||||||
*/
|
*/
|
||||||
public _worldTransform = Matrix2D.identity;
|
public _worldTransform = Matrix2D.identity;
|
||||||
public _rotationMatrix: Matrix2D = Matrix2D.identity;
|
public _rotationMatrix: Matrix2D = Matrix2D.identity;
|
||||||
public _translationMatrix: Matrix2D = Matrix2D.identity;
|
public _translationMatrix: Matrix2D = Matrix2D.identity;
|
||||||
public _scaleMatrix: Matrix2D = Matrix2D.identity;
|
public _scaleMatrix: Matrix2D;
|
||||||
public _children: Transform[];
|
public _children: Transform[] = [];
|
||||||
|
|
||||||
constructor(entity: Entity) {
|
constructor(entity: Entity) {
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
this.scale = this._localScale = Vector2.one;
|
this.scale = this._localScale = Vector2.one;
|
||||||
this._children = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -140,7 +139,7 @@ module es {
|
|||||||
public get position(): Vector2 {
|
public get position(): Vector2 {
|
||||||
this.updateTransform();
|
this.updateTransform();
|
||||||
if (this._positionDirty) {
|
if (this._positionDirty) {
|
||||||
if (!this.parent) {
|
if (this.parent == null) {
|
||||||
this._position = this._localPosition;
|
this._position = this._localPosition;
|
||||||
} else {
|
} else {
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
@@ -290,7 +289,7 @@ module es {
|
|||||||
return this;
|
return this;
|
||||||
|
|
||||||
this._position = position;
|
this._position = position;
|
||||||
if (this.parent) {
|
if (this.parent != null) {
|
||||||
this.localPosition = Vector2.transform(this._position, this._worldToLocalTransform);
|
this.localPosition = Vector2.transform(this._position, this._worldToLocalTransform);
|
||||||
} else {
|
} else {
|
||||||
this.localPosition = position;
|
this.localPosition = position;
|
||||||
@@ -404,7 +403,7 @@ module es {
|
|||||||
|
|
||||||
public updateTransform() {
|
public updateTransform() {
|
||||||
if (this.hierarchyDirty != DirtyType.clean) {
|
if (this.hierarchyDirty != DirtyType.clean) {
|
||||||
if (this.parent)
|
if (this.parent != null)
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
|
|
||||||
if (this._localDirty) {
|
if (this._localDirty) {
|
||||||
@@ -426,7 +425,7 @@ module es {
|
|||||||
this._localTransform = this._scaleMatrix.multiply(this._rotationMatrix);
|
this._localTransform = this._scaleMatrix.multiply(this._rotationMatrix);
|
||||||
this._localTransform = this._localTransform.multiply(this._translationMatrix);
|
this._localTransform = this._localTransform.multiply(this._translationMatrix);
|
||||||
|
|
||||||
if (!this.parent) {
|
if (this.parent == null) {
|
||||||
this._worldTransform = this._localTransform;
|
this._worldTransform = this._localTransform;
|
||||||
this._rotation = this._localRotation;
|
this._rotation = this._localRotation;
|
||||||
this._scale = this._localScale;
|
this._scale = this._localScale;
|
||||||
@@ -436,7 +435,7 @@ module es {
|
|||||||
this._localDirty = false;
|
this._localDirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.parent) {
|
if (this.parent != null) {
|
||||||
this._worldTransform = this._localTransform.multiply(this.parent._worldTransform);
|
this._worldTransform = this._localTransform.multiply(this.parent._worldTransform);
|
||||||
|
|
||||||
this._rotation = this._localRotation + this.parent._rotation;
|
this._rotation = this._localRotation + this.parent._rotation;
|
||||||
@@ -466,9 +465,6 @@ module es {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this._children)
|
|
||||||
this._children = [];
|
|
||||||
|
|
||||||
// 告诉子项发生了变换
|
// 告诉子项发生了变换
|
||||||
for (let i = 0; i < this._children.length; i++)
|
for (let i = 0; i < this._children.length; i++)
|
||||||
this._children[i].setDirty(dirtyFlagType);
|
this._children[i].setDirty(dirtyFlagType);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ module es {
|
|||||||
private _bits: number[];
|
private _bits: number[];
|
||||||
|
|
||||||
constructor(nbits: number = 64) {
|
constructor(nbits: number = 64) {
|
||||||
let length = nbits >> 6 >>> 0;
|
let length = nbits >> 6;
|
||||||
if ((nbits & BitSet.LONG_MASK) != 0)
|
if ((nbits & BitSet.LONG_MASK) != 0)
|
||||||
length++;
|
length++;
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ module es {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public cardinality(): number {
|
public cardinality(): number {
|
||||||
let card = 0 >>> 0;
|
let card = 0;
|
||||||
for (let i = this._bits.length - 1; i >= 0; i--) {
|
for (let i = this._bits.length - 1; i >= 0; i--) {
|
||||||
let a = this._bits[i];
|
let a = this._bits[i];
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
module es {
|
module es {
|
||||||
export class Rectangle implements IEquatable<Rectangle> {
|
export class Rectangle implements IEquatable<Rectangle> {
|
||||||
static emptyRectangle: Rectangle = new Rectangle();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 该矩形的左上角的x坐标
|
* 该矩形的左上角的x坐标
|
||||||
*/
|
*/
|
||||||
@@ -26,7 +24,7 @@ module es {
|
|||||||
* 返回X=0, Y=0, Width=0, Height=0的矩形
|
* 返回X=0, Y=0, Width=0, Height=0的矩形
|
||||||
*/
|
*/
|
||||||
public static get empty(): Rectangle {
|
public static get empty(): Rectangle {
|
||||||
return this.emptyRectangle;
|
return new Rectangle();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -320,8 +318,6 @@ module es {
|
|||||||
* @returns 矩形边框上离点最近的点
|
* @returns 矩形边框上离点最近的点
|
||||||
*/
|
*/
|
||||||
public getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2 {
|
public getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2 {
|
||||||
edgeNormal = Vector2.zero;
|
|
||||||
|
|
||||||
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
|
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
|
||||||
let res = new Vector2();
|
let res = new Vector2();
|
||||||
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
||||||
@@ -405,8 +401,8 @@ module es {
|
|||||||
* @param value2
|
* @param value2
|
||||||
*/
|
*/
|
||||||
public static overlap(value1: Rectangle, value2: Rectangle): Rectangle {
|
public static overlap(value1: Rectangle, value2: Rectangle): Rectangle {
|
||||||
let x = Math.max(Math.max(value1.x, value2.x), 0);
|
let x = Math.max(value1.x, value2.x, 0);
|
||||||
let y = Math.max(Math.max(value1.y, value2.y), 0);
|
let y = Math.max(value1.y, value2.y, 0);
|
||||||
return new Rectangle(x, y,
|
return new Rectangle(x, y,
|
||||||
Math.max(Math.min(value1.right, value2.right) - x, 0),
|
Math.max(Math.min(value1.right, value2.right) - x, 0),
|
||||||
Math.max(Math.min(value1.bottom, value2.bottom) - y, 0));
|
Math.max(Math.min(value1.bottom, value2.bottom) - y, 0));
|
||||||
@@ -555,5 +551,9 @@ module es {
|
|||||||
public getHashCode(): number{
|
public getHashCode(): number{
|
||||||
return (this.x ^ this.y ^ this.width ^ this.height);
|
return (this.x ^ this.y ^ this.width ^ this.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public clone(): Rectangle {
|
||||||
|
return new Rectangle(this.x, this.y, this.width, this.height);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ module es {
|
|||||||
* @param value2
|
* @param value2
|
||||||
*/
|
*/
|
||||||
public static add(value1: Vector2, value2: Vector2) {
|
public static add(value1: Vector2, value2: Vector2) {
|
||||||
let result: Vector2 = new Vector2(0, 0);
|
let result: Vector2 = Vector2.zero;
|
||||||
result.x = value1.x + value2.x;
|
result.x = value1.x + value2.x;
|
||||||
result.y = value1.y + value2.y;
|
result.y = value1.y + value2.y;
|
||||||
return result;
|
return result;
|
||||||
@@ -48,7 +48,7 @@ module es {
|
|||||||
* @param value2
|
* @param value2
|
||||||
*/
|
*/
|
||||||
public static divide(value1: Vector2, value2: Vector2) {
|
public static divide(value1: Vector2, value2: Vector2) {
|
||||||
let result: Vector2 = new Vector2(0, 0);
|
let result: Vector2 = Vector2.zero;
|
||||||
result.x = value1.x / value2.x;
|
result.x = value1.x / value2.x;
|
||||||
result.y = value1.y / value2.y;
|
result.y = value1.y / value2.y;
|
||||||
return result;
|
return result;
|
||||||
@@ -258,5 +258,9 @@ module es {
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public clone(): Vector2 {
|
||||||
|
return new Vector2(this.x, this.y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
module es {
|
|
||||||
export class Vector3 {
|
|
||||||
public x: number;
|
|
||||||
public y: number;
|
|
||||||
public z: number;
|
|
||||||
|
|
||||||
constructor(x: number, y: number, z: number) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
this.z = z;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -116,7 +116,7 @@ module es {
|
|||||||
public static recenterPolygonVerts(points: Vector2[]) {
|
public static recenterPolygonVerts(points: Vector2[]) {
|
||||||
let center = this.findPolygonCenter(points);
|
let center = this.findPolygonCenter(points);
|
||||||
for (let i = 0; i < points.length; i++)
|
for (let i = 0; i < points.length; i++)
|
||||||
points[i].subtract(center);
|
points[i] = Vector2.subtract(points[i], center);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -212,7 +212,7 @@ module es {
|
|||||||
|
|
||||||
public recalculateBounds(collider: Collider) {
|
public recalculateBounds(collider: Collider) {
|
||||||
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
||||||
this.center = collider.localOffset;
|
this.center = collider.localOffset.clone();
|
||||||
|
|
||||||
if (collider.shouldColliderScaleAndRotateWithTransform) {
|
if (collider.shouldColliderScaleAndRotateWithTransform) {
|
||||||
let hasUnitScale = true;
|
let hasUnitScale = true;
|
||||||
@@ -234,7 +234,7 @@ module es {
|
|||||||
|
|
||||||
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动
|
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动
|
||||||
// 我们的偏移使角度为0我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
|
// 我们的偏移使角度为0我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
|
||||||
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
|
let offsetAngle = Math.atan2(collider.localOffset.y * collider.entity.transform.scale.y, collider.localOffset.x * collider.entity.transform.scale.x) * MathHelper.Rad2Deg;
|
||||||
let offsetLength = hasUnitScale ? collider._localOffsetLength :
|
let offsetLength = hasUnitScale ? collider._localOffsetLength :
|
||||||
Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
|
Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
|
||||||
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength,
|
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength,
|
||||||
@@ -256,7 +256,7 @@ module es {
|
|||||||
|
|
||||||
this.position = Vector2.add(collider.entity.transform.position, this.center);
|
this.position = Vector2.add(collider.entity.transform.position, this.center);
|
||||||
this.bounds = Rectangle.rectEncompassingPoints(this.points);
|
this.bounds = Rectangle.rectEncompassingPoints(this.points);
|
||||||
this.bounds.location.add(this.position);
|
this.bounds.location = Vector2.add(this.bounds.location, this.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
public overlaps(other: Shape) {
|
public overlaps(other: Shape) {
|
||||||
|
|||||||
@@ -369,7 +369,7 @@ module es {
|
|||||||
if (u < 0 || u > 1)
|
if (u < 0 || u > 1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
intersection = intersection.add(a1).add(Vector2.multiply(new Vector2(t), b));
|
intersection = Vector2.add(a1, Vector2.multiply(new Vector2(t), b));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ module es {
|
|||||||
* @param collider
|
* @param collider
|
||||||
*/
|
*/
|
||||||
public register(collider: Collider) {
|
public register(collider: Collider) {
|
||||||
let bounds = collider.bounds;
|
let bounds = collider.bounds.clone();
|
||||||
collider.registeredPhysicsBounds = bounds;
|
collider.registeredPhysicsBounds = bounds;
|
||||||
let p1 = this.cellCoords(bounds.x, bounds.y);
|
let p1 = this.cellCoords(bounds.x, bounds.y);
|
||||||
let p2 = this.cellCoords(bounds.right, bounds.bottom);
|
let p2 = this.cellCoords(bounds.right, bounds.bottom);
|
||||||
@@ -63,7 +63,7 @@ module es {
|
|||||||
* @param collider
|
* @param collider
|
||||||
*/
|
*/
|
||||||
public remove(collider: Collider) {
|
public remove(collider: Collider) {
|
||||||
let bounds = collider.registeredPhysicsBounds;
|
let bounds = collider.registeredPhysicsBounds.clone();
|
||||||
let p1 = this.cellCoords(bounds.x, bounds.y);
|
let p1 = this.cellCoords(bounds.x, bounds.y);
|
||||||
let p2 = this.cellCoords(bounds.right, bounds.bottom);
|
let p2 = this.cellCoords(bounds.right, bounds.bottom);
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ 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)
|
if (cell == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// 当cell不为空。循环并取回所有碰撞器
|
// 当cell不为空。循环并取回所有碰撞器
|
||||||
@@ -350,7 +350,7 @@ module es {
|
|||||||
// TODO: rayIntersects的性能够吗?需要测试它。Collisions.rectToLine可能更快
|
// TODO: rayIntersects的性能够吗?需要测试它。Collisions.rectToLine可能更快
|
||||||
// TODO: 如果边界检查返回更多数据,我们就不需要为BoxCollider检查做任何事情
|
// TODO: 如果边界检查返回更多数据,我们就不需要为BoxCollider检查做任何事情
|
||||||
// 在做形状测试之前先做一个边界检查
|
// 在做形状测试之前先做一个边界检查
|
||||||
let colliderBounds = potential.bounds;
|
let colliderBounds = potential.bounds.clone();
|
||||||
if (colliderBounds.rayIntersects(this._ray, fraction) && fraction.value <= 1){
|
if (colliderBounds.rayIntersects(this._ray, fraction) && fraction.value <= 1){
|
||||||
if (potential.shape.collidesWithLine(this._ray.start, this._ray.end, this._tempHit)) {
|
if (potential.shape.collidesWithLine(this._ray.start, this._ray.end, this._tempHit)) {
|
||||||
// 检查一下,我们应该排除这些射线,射线cast是否在碰撞器中开始
|
// 检查一下,我们应该排除这些射线,射线cast是否在碰撞器中开始
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ module es {
|
|||||||
result.x = Math.min(first.x, rect.x);
|
result.x = Math.min(first.x, rect.x);
|
||||||
result.y = Math.min(first.y, rect.y);
|
result.y = Math.min(first.y, rect.y);
|
||||||
result.width = Math.max(first.right, rect.right) - result.x;
|
result.width = Math.max(first.right, rect.right) - result.x;
|
||||||
result.height = Math.max(first.bottom, result.bottom) - result.y;
|
result.height = Math.max(first.bottom, rect.bottom) - result.y;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user