移除所有egret 依赖。移除renderablecomponent及所有依赖,移除camera。保持ecs基础框架
This commit is contained in:
@@ -1,119 +1,168 @@
|
||||
module es {
|
||||
export var matrixPool = [];
|
||||
|
||||
/**
|
||||
* 表示右手3 * 3的浮点矩阵,可以存储平移、缩放和旋转信息。
|
||||
*/
|
||||
export class Matrix2D extends egret.Matrix {
|
||||
public get m11(): number {
|
||||
return this.a;
|
||||
}
|
||||
export class Matrix2D implements IEquatable<Matrix2D> {
|
||||
public m11: number = 0; // x 缩放
|
||||
public m12: number = 0;
|
||||
|
||||
public set m11(value: number) {
|
||||
this.a = value;
|
||||
}
|
||||
public m21: number = 0;
|
||||
public m22: number = 0;
|
||||
|
||||
public get m12(): number {
|
||||
return this.b;
|
||||
}
|
||||
public m31: number = 0;
|
||||
public m32: number = 0;
|
||||
|
||||
public set m12(value: number) {
|
||||
this.b = value;
|
||||
}
|
||||
|
||||
public get m21(): number {
|
||||
return this.c;
|
||||
}
|
||||
|
||||
public set m21(value: number) {
|
||||
this.c = value;
|
||||
}
|
||||
|
||||
public get m22(): number {
|
||||
return this.d;
|
||||
}
|
||||
|
||||
public set m22(value: number) {
|
||||
this.d = value;
|
||||
}
|
||||
|
||||
public get m31(): number {
|
||||
return this.tx;
|
||||
}
|
||||
|
||||
public set m31(value: number) {
|
||||
this.tx = value;
|
||||
}
|
||||
|
||||
public get m32(): number {
|
||||
return this.ty;
|
||||
}
|
||||
|
||||
public set m32(value: number) {
|
||||
this.ty = value;
|
||||
/**
|
||||
* 返回标识矩阵
|
||||
*/
|
||||
public static get identity(): Matrix2D {
|
||||
return this._identity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从对象池中取出或创建一个新的Matrix对象。
|
||||
* 储存在该矩阵中的位置
|
||||
*/
|
||||
public static create(): Matrix2D {
|
||||
let matrix = matrixPool.pop();
|
||||
if (!matrix)
|
||||
matrix = new Matrix2D();
|
||||
return matrix;
|
||||
public get translation(): Vector2 {
|
||||
return new Vector2(this.m31, this.m32);
|
||||
}
|
||||
|
||||
public identity(): Matrix2D {
|
||||
this.a = this.d = 1;
|
||||
this.b = this.c = this.tx = this.ty = 0;
|
||||
return this;
|
||||
public set translation(value: Vector2) {
|
||||
this.m31 = value.x;
|
||||
this.m32 = value.y;
|
||||
}
|
||||
|
||||
public translate(dx: number, dy: number): Matrix2D {
|
||||
this.tx += dx;
|
||||
this.ty += dy;
|
||||
return this;
|
||||
/**
|
||||
* 以弧度为单位的旋转,存储在这个矩阵中
|
||||
*/
|
||||
public get rotation(): number {
|
||||
return Math.atan2(this.m21, this.m11);
|
||||
}
|
||||
|
||||
public scale(sx: number, sy: number): Matrix2D {
|
||||
if (sx !== 1) {
|
||||
this.a *= sx;
|
||||
this.c *= sx;
|
||||
this.tx *= sx;
|
||||
}
|
||||
if (sy !== 1) {
|
||||
this.b *= sy;
|
||||
this.d *= sy;
|
||||
this.ty *= sy;
|
||||
}
|
||||
return this;
|
||||
public set rotation(value: number){
|
||||
let val1 = Math.cos(value);
|
||||
let val2 = Math.sin(value);
|
||||
|
||||
this.m11 = val1;
|
||||
this.m12 = val2;
|
||||
this.m21 = -val2;
|
||||
this.m22 = val1;
|
||||
}
|
||||
|
||||
public rotate(angle: number): Matrix2D {
|
||||
angle = +angle;
|
||||
if (angle !== 0) {
|
||||
angle = angle / DEG_TO_RAD;
|
||||
let u = Math.cos(angle);
|
||||
let v = Math.sin(angle);
|
||||
let ta = this.a;
|
||||
let tb = this.b;
|
||||
let tc = this.c;
|
||||
let td = this.d;
|
||||
let ttx = this.tx;
|
||||
let tty = this.ty;
|
||||
this.a = ta * u - tb * v;
|
||||
this.b = ta * v + tb * u;
|
||||
this.c = tc * u - td * v;
|
||||
this.d = tc * v + td * u;
|
||||
this.tx = ttx * u - tty * v;
|
||||
this.ty = ttx * v + tty * u;
|
||||
}
|
||||
return this;
|
||||
/**
|
||||
* 矩阵中存储的旋转度数
|
||||
*/
|
||||
public get rotationDegrees() {
|
||||
return MathHelper.toDegrees(this.rotation);
|
||||
}
|
||||
|
||||
public invert(): Matrix2D {
|
||||
this.$invertInto(this);
|
||||
return this;
|
||||
public set rotationDegrees(value: number) {
|
||||
this.rotation = MathHelper.toRadians(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 储存在这个矩阵中的缩放
|
||||
*/
|
||||
public get scale(): Vector2 {
|
||||
return new Vector2(this.m11, this.m22);
|
||||
}
|
||||
|
||||
public set scale(value: Vector2) {
|
||||
this.m11 = value.x;
|
||||
this.m22 = value.y;
|
||||
}
|
||||
|
||||
static _identity: Matrix2D = new Matrix2D(1, 0, 0, 1, 0, 0);
|
||||
|
||||
/**
|
||||
* 构建一个矩阵
|
||||
* @param m11
|
||||
* @param m12
|
||||
* @param m21
|
||||
* @param m22
|
||||
* @param m31
|
||||
* @param m32
|
||||
*/
|
||||
constructor(m11: number, m12: number, m21: number, m22: number, m31: number, m32: number){
|
||||
this.m11 = m11;
|
||||
this.m12 = m12;
|
||||
|
||||
this.m21 = m21;
|
||||
this.m22 = m22;
|
||||
|
||||
this.m31 = m31;
|
||||
this.m32 = m32;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的围绕Z轴的旋转矩阵2D
|
||||
* @param radians
|
||||
*/
|
||||
public static createRotation(radians: number){
|
||||
let result: Matrix2D = this.identity;
|
||||
|
||||
let val1 = Math.cos(radians);
|
||||
let val2 = Math.sin(radians);
|
||||
|
||||
result.m11 = val1;
|
||||
result.m12 = val2;
|
||||
result.m21 = -val2;
|
||||
result.m22 = val1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的缩放矩阵2D
|
||||
* @param xScale
|
||||
* @param yScale
|
||||
*/
|
||||
public static createScale(xScale: number, yScale: number){
|
||||
let result: Matrix2D = this.identity;
|
||||
result.m11 = xScale;
|
||||
result.m12 = 0;
|
||||
|
||||
result.m21 = 0;
|
||||
result.m22 = yScale;
|
||||
|
||||
result.m31 = 0;
|
||||
result.m32 = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的平移矩阵2D
|
||||
* @param xPosition
|
||||
* @param yPosition
|
||||
*/
|
||||
public static createTranslation(xPosition: number, yPosition: number) {
|
||||
let result: Matrix2D = this.identity;
|
||||
result.m11 = 1;
|
||||
result.m12 = 0;
|
||||
|
||||
result.m21 = 0;
|
||||
result.m22 = 1;
|
||||
|
||||
result.m31 = xPosition;
|
||||
result.m32 = yPosition;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static invert(matrix: Matrix2D) {
|
||||
let det = 1 / matrix.determinant();
|
||||
|
||||
let result = this.identity;
|
||||
result.m11 = matrix.m22 * det;
|
||||
result.m12 = -matrix.m12 * det;
|
||||
|
||||
result.m21 = -matrix.m21 * det;
|
||||
result.m22 = matrix.m11 * det;
|
||||
|
||||
result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det;
|
||||
result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,10 +234,56 @@ module es {
|
||||
return this.m11 * this.m22 - this.m12 * this.m21;
|
||||
}
|
||||
|
||||
public release(matrix: Matrix2D) {
|
||||
if (!matrix)
|
||||
return;
|
||||
matrixPool.push(matrix);
|
||||
/**
|
||||
* 创建一个新的Matrix2D,包含指定矩阵中的线性插值。
|
||||
* @param matrix1
|
||||
* @param matrix2
|
||||
* @param amount
|
||||
*/
|
||||
public static lerp(matrix1: Matrix2D, matrix2: Matrix2D, amount: number){
|
||||
matrix1.m11 = matrix1.m11 + ((matrix2.m11 - matrix1.m11) * amount);
|
||||
matrix1.m12 = matrix1.m12 + ((matrix2.m12 - matrix1.m12) * amount);
|
||||
|
||||
matrix1.m21 = matrix1.m21 + ((matrix2.m21 - matrix1.m21) * amount);
|
||||
matrix1.m22 = matrix1.m22 + ((matrix2.m22 - matrix1.m22) * amount);
|
||||
|
||||
matrix1.m31 = matrix1.m31 + ((matrix2.m31 - matrix1.m31) * amount);
|
||||
matrix1.m32 = matrix1.m32 + ((matrix2.m32 - matrix1.m32) * amount);
|
||||
return matrix1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 交换矩阵的行和列
|
||||
* @param matrix
|
||||
*/
|
||||
public static transpose(matrix: Matrix2D) {
|
||||
let ret: Matrix2D = this.identity;
|
||||
ret.m11 = matrix.m11;
|
||||
ret.m12 = matrix.m21;
|
||||
|
||||
ret.m21 = matrix.m12;
|
||||
ret.m22 = matrix.m22;
|
||||
|
||||
ret.m31 = 0;
|
||||
ret.m32 = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public mutiplyTranslation(x: number, y: number){
|
||||
let trans = Matrix2D.createTranslation(x, y);
|
||||
return MatrixHelper.mutiply(this, trans);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较当前实例是否等于指定的Matrix2D
|
||||
* @param other
|
||||
*/
|
||||
public equals(other: Matrix2D){
|
||||
return this == other;
|
||||
}
|
||||
|
||||
public toString() {
|
||||
return `{m11:${this.m11} m12:${this.m12} m21:${this.m21} m22:${this.m22} m31:${this.m31} m32:${this.m32}}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
99
source/src/Math/MatrixHelper.ts
Normal file
99
source/src/Math/MatrixHelper.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
module es {
|
||||
export class MatrixHelper {
|
||||
/**
|
||||
* 创建一个新的Matrix2D,其中包含两个矩阵的和
|
||||
* @param matrix1
|
||||
* @param matrix2
|
||||
*/
|
||||
public static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
|
||||
let result = Matrix2D.identity;
|
||||
result.m11 = matrix1.m11 + matrix2.m11;
|
||||
result.m12 = matrix1.m12 + matrix2.m12;
|
||||
|
||||
result.m21 = matrix1.m21 + matrix2.m21;
|
||||
result.m22 = matrix1.m22 + matrix2.m22;
|
||||
|
||||
result.m31 = matrix1.m31 + matrix2.m31;
|
||||
result.m32 = matrix1.m32 + matrix2.m32;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一个Matrix2D的元素除以另一个矩阵的元素
|
||||
* @param matrix1
|
||||
* @param matrix2
|
||||
*/
|
||||
public static divide(matrix1: Matrix2D, matrix2: Matrix2D) {
|
||||
let result = Matrix2D.identity;
|
||||
|
||||
result.m11 = matrix1.m11 / matrix2.m11;
|
||||
result.m12 = matrix1.m12 / matrix2.m12;
|
||||
result.m21 = matrix1.m21 / matrix2.m21;
|
||||
result.m22 = matrix1.m22 / matrix2.m22;
|
||||
result.m31 = matrix1.m31 / matrix2.m31;
|
||||
result.m32 = matrix1.m32 / matrix2.m32;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的Matrix2D,包含两个矩阵的乘法
|
||||
* @param matrix1
|
||||
* @param matrix2
|
||||
*/
|
||||
public static mutiply(matrix1: Matrix2D, matrix2: Matrix2D | number) {
|
||||
let result = Matrix2D.identity;
|
||||
if (matrix2 instanceof Matrix2D) {
|
||||
let m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21);
|
||||
let m12 = (matrix2.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
|
||||
|
||||
let m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
|
||||
let m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
|
||||
|
||||
let m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
|
||||
let m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
|
||||
|
||||
result.m11 = m11;
|
||||
result.m12 = m12;
|
||||
|
||||
result.m21 = m21;
|
||||
result.m22 = m22;
|
||||
|
||||
result.m31 = m31;
|
||||
result.m32 = m32;
|
||||
} else if (typeof matrix2 == "number"){
|
||||
result.m11 = matrix1.m11 * matrix2;
|
||||
result.m12 = matrix1.m12 * matrix2;
|
||||
|
||||
result.m21 = matrix1.m21 * matrix2;
|
||||
result.m22 = matrix1.m22 * matrix2;
|
||||
|
||||
result.m31 = matrix1.m31 * matrix2;
|
||||
result.m32 = matrix1.m32 * matrix2;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的Matrix2D,包含一个矩阵与另一个矩阵的减法。
|
||||
* @param matrix1
|
||||
* @param matrix2
|
||||
*/
|
||||
public static subtract(matrix1: Matrix2D, matrix2: Matrix2D) {
|
||||
let result = Matrix2D.identity;
|
||||
|
||||
result.m11 = matrix1.m11 - matrix2.m11;
|
||||
result.m12 = matrix1.m12 - matrix2.m12;
|
||||
|
||||
result.m21 = matrix1.m21 - matrix2.m21;
|
||||
result.m22 = matrix1.m22 - matrix2.m22;
|
||||
|
||||
result.m31 = matrix1.m31 - matrix2.m31;
|
||||
result.m32 = matrix1.m32 - matrix2.m32;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,68 @@
|
||||
module es {
|
||||
export class Rectangle extends egret.Rectangle {
|
||||
public _tempMat: Matrix2D;
|
||||
public _transformMat: Matrix2D;
|
||||
export class Rectangle implements IEquatable<Rectangle> {
|
||||
static emptyRectangle: Rectangle = new Rectangle();
|
||||
|
||||
/**
|
||||
* 该矩形的左上角的x坐标
|
||||
*/
|
||||
public x: number = 0;
|
||||
|
||||
/**
|
||||
* 该矩形的左上角的y坐标
|
||||
*/
|
||||
public y: number = 0;
|
||||
|
||||
/**
|
||||
* 该矩形的宽度
|
||||
*/
|
||||
public width: number = 0;
|
||||
|
||||
/**
|
||||
* 该矩形的高度
|
||||
*/
|
||||
public height: number = 0;
|
||||
|
||||
/**
|
||||
* 返回X=0, Y=0, Width=0, Height=0的矩形
|
||||
*/
|
||||
public static get empty(): Rectangle {
|
||||
return this.emptyRectangle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个Number.Min/Max值的矩形
|
||||
*/
|
||||
public static get maxRect(): Rectangle {
|
||||
return new Rectangle(Number.MIN_VALUE / 2, Number.MIN_VALUE / 2, Number.MAX_VALUE, Number.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回此矩形左边缘的X坐标
|
||||
*/
|
||||
public get left(): number {
|
||||
return this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回此矩形右边缘的X坐标
|
||||
*/
|
||||
public get right(): number {
|
||||
return this.x + this.width;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回此矩形顶边的y坐标
|
||||
*/
|
||||
public get top(): number {
|
||||
return this.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回此矩形底边的y坐标
|
||||
*/
|
||||
public get bottom(): number {
|
||||
return this.y + this.height;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取矩形的最大点,即右下角
|
||||
@@ -10,22 +71,26 @@ module es {
|
||||
return new Vector2(this.right, this.bottom);
|
||||
}
|
||||
|
||||
/** 中心点坐标 */
|
||||
public get center() {
|
||||
return new Vector2(this.x + (this.width / 2), this.y + (this.height / 2));
|
||||
/**
|
||||
* 这个矩形的宽和高是否为0,位置是否为(0,0)
|
||||
*/
|
||||
public isEmpty(): boolean {
|
||||
return ((((this.width == 0) && (this.height == 0)) && (this.x == 0)) && (this.y == 0));
|
||||
}
|
||||
|
||||
/** 左上角的坐标 */
|
||||
/** 这个矩形的左上角坐标 */
|
||||
public get location() {
|
||||
return new Vector2(this.x, this.y);
|
||||
}
|
||||
|
||||
/** 左上角的坐标 */
|
||||
public set location(value: Vector2) {
|
||||
this.x = value.x;
|
||||
this.y = value.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 这个矩形的宽-高坐标
|
||||
*/
|
||||
public get size() {
|
||||
return new Vector2(this.width, this.height);
|
||||
}
|
||||
@@ -35,8 +100,35 @@ module es {
|
||||
this.height = value.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 位于这个矩形中心的一个点
|
||||
* 如果 "宽度 "或 "高度 "是奇数,则中心点将向下舍入
|
||||
*/
|
||||
public get center() {
|
||||
return new Vector2(this.x + (this.width / 2), this.y + (this.height / 2));
|
||||
}
|
||||
|
||||
// temp 用于计算边界的矩阵
|
||||
public _tempMat: Matrix2D;
|
||||
public _transformMat: Matrix2D;
|
||||
|
||||
/**
|
||||
* 创建一个矩形的最小/最大点(左上角,右下角的点)
|
||||
* 创建一个新的Rectanglestruct实例,指定位置、宽度和高度。
|
||||
* @param x 创建的矩形的左上角的X坐标
|
||||
* @param y 创建的矩形的左上角的y坐标
|
||||
* @param width 创建的矩形的宽度
|
||||
* @param height 创建的矩形的高度
|
||||
*/
|
||||
constructor(x: number = 0, y: number = 0, width: number = 0, height: number = 0) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建一个给定最小/最大点(左上角,右下角)的矩形
|
||||
* @param minX
|
||||
* @param minY
|
||||
* @param maxX
|
||||
@@ -49,6 +141,7 @@ module es {
|
||||
/**
|
||||
* 给定多边形的点,计算边界
|
||||
* @param points
|
||||
* @returns 来自多边形的点
|
||||
*/
|
||||
public static rectEncompassingPoints(points: Vector2[]) {
|
||||
// 我们需要求出x/y的最小值/最大值
|
||||
@@ -71,28 +164,69 @@ module es {
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果其他相交矩形返回true
|
||||
* @param value
|
||||
* 获取指定边缘的位置
|
||||
* @param edge
|
||||
*/
|
||||
public intersects(value: egret.Rectangle) {
|
||||
public getSide(edge: Edge) {
|
||||
switch (edge) {
|
||||
case Edge.top:
|
||||
return this.top;
|
||||
case Edge.bottom:
|
||||
return this.bottom;
|
||||
case Edge.left:
|
||||
return this.left;
|
||||
case Edge.right:
|
||||
return this.right;
|
||||
default:
|
||||
throw new Error("Argument Out Of Range");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所提供的坐标是否在这个矩形的范围内
|
||||
* @param x 检查封堵点的X坐标
|
||||
* @param y 检查封堵点的Y坐标
|
||||
*/
|
||||
public contains(x: number, y: number): boolean {
|
||||
return ((((this.x <= x) && (x < (this.x + this.width))) &&
|
||||
(this.y <= y)) && (y < (this.y + this.height)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定的水平和垂直方向调整此矩形的边缘
|
||||
* @param horizontalAmount 调整左、右边缘的值
|
||||
* @param verticalAmount 调整上、下边缘的值
|
||||
*/
|
||||
public inflate(horizontalAmount: number, verticalAmount: number) {
|
||||
this.x -= horizontalAmount;
|
||||
this.y -= verticalAmount;
|
||||
this.width += horizontalAmount * 2;
|
||||
this.height += verticalAmount * 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取其他矩形是否与这个矩形相交
|
||||
* @param value 另一个用于测试的矩形
|
||||
*/
|
||||
public intersects(value: Rectangle) {
|
||||
return value.left < this.right &&
|
||||
this.left < value.right &&
|
||||
value.top < this.bottom &&
|
||||
this.top < value.bottom;
|
||||
}
|
||||
|
||||
public rayIntersects(ray: Ray2D, distance: Ref<number>): boolean{
|
||||
public rayIntersects(ray: Ray2D, distance: Ref<number>): boolean {
|
||||
distance.value = 0;
|
||||
let maxValue = Number.MAX_VALUE;
|
||||
|
||||
if (Math.abs(ray.direction.x) < 1E-06){
|
||||
if (Math.abs(ray.direction.x) < 1E-06) {
|
||||
if ((ray.start.x < this.x) || (ray.start.x > this.x + this.width))
|
||||
return false;
|
||||
}else{
|
||||
} else {
|
||||
let num11 = 1 / ray.direction.x;
|
||||
let num8 = (this.x - ray.start.x) * num11;
|
||||
let num7 = (this.x + this.width - ray.start.x) * num11;
|
||||
if (num8 > num7){
|
||||
if (num8 > num7) {
|
||||
let num14 = num8;
|
||||
num8 = num7;
|
||||
num7 = num14;
|
||||
@@ -104,14 +238,14 @@ module es {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Math.abs(ray.direction.y) < 1E-06){
|
||||
if (Math.abs(ray.direction.y) < 1E-06) {
|
||||
if ((ray.start.y < this.y) || (ray.start.y > this.y + this.height))
|
||||
return false;
|
||||
}else{
|
||||
} else {
|
||||
let num10 = 1 / ray.direction.y;
|
||||
let num6 = (this.y - ray.start.y) * num10;
|
||||
let num5 = (this.y + this.height - ray.start.y) * num10;
|
||||
if (num6 > num5){
|
||||
if (num6 > num5) {
|
||||
let num13 = num6;
|
||||
num6 = num5;
|
||||
num5 = num13;
|
||||
@@ -136,28 +270,64 @@ module es {
|
||||
(value.y < (this.y + this.height)));
|
||||
}
|
||||
|
||||
public contains(x: number, y: number): boolean{
|
||||
return ((((this.x <= x) && (x < (this.x + this.width))) && (this.y <= y)) && (y < (this.y + this.height)));
|
||||
}
|
||||
|
||||
public getHalfSize() {
|
||||
return new Vector2(this.width * 0.5, this.height * 0.5);
|
||||
}
|
||||
|
||||
public getClosestPointOnBoundsToOrigin() {
|
||||
let max = this.max;
|
||||
let minDist = Math.abs(this.location.x);
|
||||
let boundsPoint = new Vector2(this.location.x, 0);
|
||||
|
||||
if (Math.abs(max.x) < minDist) {
|
||||
minDist = Math.abs(max.x);
|
||||
boundsPoint.x = max.x;
|
||||
boundsPoint.y = 0;
|
||||
}
|
||||
|
||||
if (Math.abs(max.y) < minDist) {
|
||||
minDist = Math.abs(max.y);
|
||||
boundsPoint.x = 0;
|
||||
boundsPoint.y = max.y;
|
||||
}
|
||||
|
||||
if (Math.abs(this.location.y) < minDist) {
|
||||
minDist = Math.abs(this.location.y);
|
||||
boundsPoint.x = 0;
|
||||
boundsPoint.y = this.location.y;
|
||||
}
|
||||
|
||||
return boundsPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回离给定点最近的点
|
||||
* @param point 矩形上离点最近的点
|
||||
*/
|
||||
public getClosestPointOnRectangleToPoint(point: Vector2) {
|
||||
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
|
||||
let res = new Vector2();
|
||||
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
||||
res.y = MathHelper.clamp(point.y, this.top, this.bottom);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取矩形边界上与给定点最近的点
|
||||
* @param point
|
||||
* @param edgeNormal
|
||||
* @returns 矩形边框上离点最近的点
|
||||
*/
|
||||
public getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2 {
|
||||
edgeNormal = Vector2.zero;
|
||||
|
||||
// 对于每个轴,如果点在盒子外面
|
||||
// 对于每条轴,如果点在框外,就把它限制在框内,否则就不要管它
|
||||
let res = new Vector2();
|
||||
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
||||
res.y = MathHelper.clamp(point.y, this.top, this.bottom);
|
||||
|
||||
// 如果点在矩形内,我们需要推res到边界,因为它将在矩形内
|
||||
// 如果点在矩形内,我们需要将res推到边界上,因为它将在矩形内
|
||||
if (this.contains(res.x, res.y)) {
|
||||
let dl = res.x - this.left;
|
||||
let dr = this.right - res.x;
|
||||
@@ -189,55 +359,81 @@ module es {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 创建一个新的RectangleF,该RectangleF包含两个其他矩形的重叠区域
|
||||
* @param value1
|
||||
* @param value2
|
||||
* @returns 将两个矩形的重叠区域作为输出参数
|
||||
*/
|
||||
public getClosestPointOnBoundsToOrigin() {
|
||||
let max = this.max;
|
||||
let minDist = Math.abs(this.location.x);
|
||||
let boundsPoint = new Vector2(this.location.x, 0);
|
||||
|
||||
if (Math.abs(max.x) < minDist) {
|
||||
minDist = Math.abs(max.x);
|
||||
boundsPoint.x = max.x;
|
||||
boundsPoint.y = 0;
|
||||
public static intersect(value1: Rectangle, value2: Rectangle) {
|
||||
if (value1.intersects(value2)) {
|
||||
let right_side = Math.min(value1.x + value1.width, value2.x + value2.width);
|
||||
let left_side = Math.max(value1.x, value2.x);
|
||||
let top_side = Math.max(value1.y, value2.y);
|
||||
let bottom_side = Math.min(value1.y + value1.height, value2.y + value2.height);
|
||||
return new Rectangle(left_side, top_side, right_side - left_side, bottom_side - top_side);
|
||||
} else {
|
||||
return new Rectangle(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
if (Math.abs(max.y) < minDist) {
|
||||
minDist = Math.abs(max.y);
|
||||
boundsPoint.x = 0;
|
||||
boundsPoint.y = max.y;
|
||||
}
|
||||
|
||||
if (Math.abs(this.location.y) < minDist) {
|
||||
minDist = Math.abs(this.location.y);
|
||||
boundsPoint.x = 0;
|
||||
boundsPoint.y = this.location.y;
|
||||
}
|
||||
|
||||
return boundsPoint;
|
||||
}
|
||||
|
||||
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number) {
|
||||
/**
|
||||
* 改变这个矩形的位置
|
||||
* @param offsetX 要添加到这个矩形的X坐标
|
||||
* @param offsetY 要添加到这个矩形的y坐标
|
||||
*/
|
||||
public offset(offsetX: number, offsetY: number) {
|
||||
this.x += offsetX;
|
||||
this.y += offsetY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个完全包含两个其他矩形的新矩形
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static union(value1: Rectangle, value2: Rectangle) {
|
||||
let x = Math.min(value1.x, value2.x);
|
||||
let y = Math.min(value1.y, value2.y);
|
||||
return new Rectangle(x, y,
|
||||
Math.max(value1.right, value2.right) - x,
|
||||
Math.max(value1.bottom, value2.bottom) - y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在矩形重叠的地方创建一个新的矩形
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static overlap(value1: Rectangle, value2: Rectangle): Rectangle {
|
||||
let x = Math.max(Math.max(value1.x, value2.x), 0);
|
||||
let y = Math.max(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));
|
||||
}
|
||||
|
||||
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2,
|
||||
rotation: number, width: number, height: number) {
|
||||
if (rotation == 0) {
|
||||
this.x = parentPosition.x + position.x - origin.x * scale.x;
|
||||
this.y = parentPosition.y + position.y - origin.y * scale.y;
|
||||
this.width = width * scale.x;
|
||||
this.height = height * scale.y;
|
||||
} else {
|
||||
// 特别注意旋转的边界。我们需要找到绝对的最小/最大值并从中创建边界
|
||||
// 我们需要找到我们的绝对最小/最大值,并据此创建边界
|
||||
let worldPosX = parentPosition.x + position.x;
|
||||
let worldPosY = parentPosition.y + position.y;
|
||||
|
||||
// 将参考点设置为世界参考
|
||||
this._transformMat = Matrix2D.create().translate(-worldPosX - origin.x, -worldPosY - origin.y);
|
||||
this._tempMat = Matrix2D.create().scale(scale.x, scale.y);
|
||||
// 考虑到原点,将参考点设置为世界参考
|
||||
this._transformMat = Matrix2D.createTranslation(-worldPosX - origin.x, -worldPosY - origin.y);
|
||||
this._tempMat = Matrix2D.createScale(scale.x, scale.y);
|
||||
this._transformMat = this._transformMat.multiply(this._tempMat);
|
||||
this._tempMat = Matrix2D.create().rotate(rotation);
|
||||
this._tempMat = Matrix2D.createRotation(rotation);
|
||||
this._transformMat = this._transformMat.multiply(this._tempMat);
|
||||
this._tempMat = Matrix2D.create().translate(worldPosX, worldPosY);
|
||||
this._tempMat = Matrix2D.createTranslation(worldPosX, worldPosY);
|
||||
this._transformMat = this._transformMat.multiply(this._tempMat);
|
||||
|
||||
// TODO: 这有点傻。我们可以把世界变换留在矩阵中,避免在世界空间中得到所有的四个角
|
||||
// TODO: 我们可以把世界变换留在矩阵中,避免在世界空间中得到所有的四个角
|
||||
let topLeft = new Vector2(worldPosX, worldPosY);
|
||||
let topRight = new Vector2(worldPosX + width, worldPosY);
|
||||
let bottomLeft = new Vector2(worldPosX, worldPosY + height);
|
||||
@@ -248,6 +444,7 @@ module es {
|
||||
Vector2Ext.transformR(bottomLeft, this._transformMat, bottomLeft);
|
||||
Vector2Ext.transformR(bottomRight, this._transformMat, bottomRight);
|
||||
|
||||
// 找出最小值和最大值,这样我们就可以计算出我们的边界框。
|
||||
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
@@ -258,5 +455,105 @@ module es {
|
||||
this.height = maxY - minY;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个横跨当前矩形和提供的三角形位置的矩形
|
||||
* @param deltaX
|
||||
* @param deltaY
|
||||
*/
|
||||
public getSweptBroadphaseBounds(deltaX: number, deltaY: number){
|
||||
let broadphasebox = Rectangle.empty;
|
||||
|
||||
broadphasebox.x = deltaX > 0 ? this.x : this.x + deltaX;
|
||||
broadphasebox.y = deltaY > 0 ? this.y : this.y + deltaY;
|
||||
broadphasebox.width = deltaX > 0 ? deltaX + this.width : this.width - deltaX;
|
||||
broadphasebox.height = deltaY > 0 ? deltaY + this.height : this.height - deltaY;
|
||||
|
||||
return broadphasebox;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果发生碰撞,返回true
|
||||
* moveX和moveY将返回b1为避免碰撞而必须移动的移动量
|
||||
* @param other
|
||||
* @param moveX
|
||||
* @param moveY
|
||||
*/
|
||||
public collisionCheck(other: Rectangle, moveX: Ref<number>, moveY: Ref<number>){
|
||||
moveX.value = moveY.value = 0;
|
||||
|
||||
let l = other.x - (this.x + this.width);
|
||||
let r = (other.x + other.width) - this.x;
|
||||
let t = (other.y - (this.y + this.height));
|
||||
let b = (other.y + other.height) - this.y;
|
||||
|
||||
// 检验是否有碰撞
|
||||
if (l > 0 || r < 0 || t > 0 || b < 0)
|
||||
return false;
|
||||
|
||||
// 求两边的偏移量
|
||||
moveX.value = Math.abs(l) < r ? l : r;
|
||||
moveY.value = Math.abs(t) < b ? t : b;
|
||||
|
||||
// 只使用最小的偏移量
|
||||
if (Math.abs(moveX.value) < Math.abs(moveY.value))
|
||||
moveY.value = 0;
|
||||
else
|
||||
moveX.value = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个矩形之间有符号的交点深度
|
||||
* @param rectA
|
||||
* @param rectB
|
||||
* @returns 两个相交的矩形之间的重叠量。
|
||||
* 这些深度值可以是负值,取决于矩形/相交的哪些边。
|
||||
* 这允许调用者确定正确的推送对象的方向,以解决碰撞问题。
|
||||
* 如果矩形不相交,则返回Vector2.Zero
|
||||
*/
|
||||
public static getIntersectionDepth(rectA: Rectangle, rectB: Rectangle): Vector2 {
|
||||
// 计算半尺寸
|
||||
let halfWidthA = rectA.width / 2;
|
||||
let halfHeightA = rectA.height / 2;
|
||||
let halfWidthB = rectB.width / 2;
|
||||
let halfHeightB = rectB.height / 2;
|
||||
|
||||
// 计算中心
|
||||
let centerA = new Vector2(rectA.left + halfWidthA, rectA.top + halfHeightA);
|
||||
let centerB = new Vector2(rectB.left + halfWidthB, rectB.top + halfHeightB);
|
||||
|
||||
// 计算当前中心间的距离和最小非相交距离
|
||||
let distanceX = centerA.x - centerB.x;
|
||||
let distanceY = centerA.y - centerB.y;
|
||||
let minDistanceX = halfWidthA + halfWidthB;
|
||||
let minDistanceY = halfHeightA + halfHeightB;
|
||||
|
||||
// 如果我们根本不相交,则返回(0,0)
|
||||
if (Math.abs(distanceX) >= minDistanceX || Math.abs(distanceY) >= minDistanceY)
|
||||
return Vector2.zero;
|
||||
|
||||
// 计算并返回交叉点深度
|
||||
let depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
|
||||
let depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
|
||||
|
||||
return new Vector2(depthX, depthY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较当前实例是否等于指定的矩形
|
||||
* @param other
|
||||
*/
|
||||
public equals(other: Rectangle) {
|
||||
return this === other;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取这个矩形的哈希码
|
||||
*/
|
||||
public getHashCode(): number{
|
||||
return (this.x ^ this.y ^ this.width ^ this.height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user