边框支持旋转

This commit is contained in:
yhh
2020-07-28 16:11:58 +08:00
parent d730851d97
commit b3e1f2c446
9 changed files with 115 additions and 46 deletions
+2 -1
View File
@@ -1196,6 +1196,8 @@ declare module es {
} }
declare module es { declare module es {
class Rectangle extends egret.Rectangle { class Rectangle extends egret.Rectangle {
_tempMat: Matrix2D;
_transformMat: Matrix2D;
readonly max: Vector2; readonly max: Vector2;
readonly center: Vector2; readonly center: Vector2;
location: Vector2; location: Vector2;
@@ -1207,7 +1209,6 @@ declare module es {
getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2; getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2;
getClosestPointOnBoundsToOrigin(): Vector2; getClosestPointOnBoundsToOrigin(): Vector2;
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void; calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
setEgretRect(rect: egret.Rectangle): Rectangle;
static rectEncompassingPoints(points: Vector2[]): Rectangle; static rectEncompassingPoints(points: Vector2[]): Rectangle;
} }
} }
+33 -12
View File
@@ -5675,17 +5675,38 @@ var es;
return boundsPoint; return boundsPoint;
}; };
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) { Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
this.x = parentPosition.x + position.x - origin.x * scale.x; if (rotation == 0) {
this.y = parentPosition.y + position.y - origin.y * scale.y; this.x = parentPosition.x + position.x - origin.x * scale.x;
this.width = width * scale.x; this.y = parentPosition.y + position.y - origin.y * scale.y;
this.height = height * scale.y; this.width = width * scale.x;
}; this.height = height * scale.y;
Rectangle.prototype.setEgretRect = function (rect) { }
this.x = rect.x; else {
this.y = rect.y; var worldPosX = parentPosition.x + position.x;
this.width = rect.width; var worldPosY = parentPosition.y + position.y;
this.height = rect.height; this._transformMat = es.Matrix2D.create().translate(-worldPosX - origin.x, -worldPosY - origin.y);
return this; this._tempMat = es.Matrix2D.create().scale(scale.x, scale.y);
this._transformMat = this._transformMat.multiply(this._tempMat);
this._tempMat = es.Matrix2D.create().rotate(rotation);
this._transformMat = this._transformMat.multiply(this._tempMat);
this._tempMat = es.Matrix2D.create().translate(worldPosX, worldPosY);
this._transformMat = this._transformMat.multiply(this._tempMat);
var topLeft = new es.Vector2(worldPosX, worldPosY);
var topRight = new es.Vector2(worldPosX + width, worldPosY);
var bottomLeft = new es.Vector2(worldPosX, worldPosY + height);
var bottomRight = new es.Vector2(worldPosX + width, worldPosY + height);
topLeft = es.Vector2Ext.transformR(topLeft, this._transformMat);
topRight = es.Vector2Ext.transformR(topRight, this._transformMat);
bottomLeft = es.Vector2Ext.transformR(bottomLeft, this._transformMat);
bottomRight = es.Vector2Ext.transformR(bottomRight, this._transformMat);
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
this.location = new es.Vector2(minX, minY);
this.width = maxX - minX;
this.height = maxY - minY;
}
}; };
Rectangle.rectEncompassingPoints = function (points) { Rectangle.rectEncompassingPoints = function (points) {
var minX = Number.POSITIVE_INFINITY; var minX = Number.POSITIVE_INFINITY;
@@ -6620,7 +6641,7 @@ var es;
this._store = new Map(); this._store = new Map();
} }
NumberDictionary.prototype.getKey = function (x, y) { NumberDictionary.prototype.getKey = function (x, y) {
return Long.fromNumber(x).shiftLeft(32).or(Long.fromNumber(y, false)).toString(); return Long.fromNumber(x).shiftLeft(32).or(Long.fromNumber(y, true)).toString();
}; };
NumberDictionary.prototype.add = function (x, y, list) { NumberDictionary.prototype.add = function (x, y, list) {
this._store.set(this.getKey(x, y), list); this._store.set(this.getKey(x, y), list);
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -19,11 +19,11 @@ module scene {
bg.addComponent(new es.BoxCollider()); bg.addComponent(new es.BoxCollider());
bg.position = new es.Vector2(Math.random() * 200, Math.random() * 200); bg.position = new es.Vector2(Math.random() * 200, Math.random() * 200);
for (let i = 0; i < 1; i++) { for (let i = 0; i < 20; i++) {
let sprite = new es.Sprite(RES.getRes("checkbox_select_disabled_png")); let sprite = new es.Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = this.createEntity("player2"); let player2 = this.createEntity("player2");
player2.addComponent(new es.SpriteRenderer()).setSprite(sprite); player2.addComponent(new es.SpriteRenderer()).setSprite(sprite);
player2.position = new es.Vector2(Math.random() * 100, Math.random() * 100); player2.position = new es.Vector2(Math.random() * 1000, Math.random() * 1000);
player2.addComponent(new es.BoxCollider()); player2.addComponent(new es.BoxCollider());
} }
+2 -1
View File
@@ -1196,6 +1196,8 @@ declare module es {
} }
declare module es { declare module es {
class Rectangle extends egret.Rectangle { class Rectangle extends egret.Rectangle {
_tempMat: Matrix2D;
_transformMat: Matrix2D;
readonly max: Vector2; readonly max: Vector2;
readonly center: Vector2; readonly center: Vector2;
location: Vector2; location: Vector2;
@@ -1207,7 +1209,6 @@ declare module es {
getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2; getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2;
getClosestPointOnBoundsToOrigin(): Vector2; getClosestPointOnBoundsToOrigin(): Vector2;
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void; calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
setEgretRect(rect: egret.Rectangle): Rectangle;
static rectEncompassingPoints(points: Vector2[]): Rectangle; static rectEncompassingPoints(points: Vector2[]): Rectangle;
} }
} }
+33 -12
View File
@@ -5675,17 +5675,38 @@ var es;
return boundsPoint; return boundsPoint;
}; };
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) { Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
this.x = parentPosition.x + position.x - origin.x * scale.x; if (rotation == 0) {
this.y = parentPosition.y + position.y - origin.y * scale.y; this.x = parentPosition.x + position.x - origin.x * scale.x;
this.width = width * scale.x; this.y = parentPosition.y + position.y - origin.y * scale.y;
this.height = height * scale.y; this.width = width * scale.x;
}; this.height = height * scale.y;
Rectangle.prototype.setEgretRect = function (rect) { }
this.x = rect.x; else {
this.y = rect.y; var worldPosX = parentPosition.x + position.x;
this.width = rect.width; var worldPosY = parentPosition.y + position.y;
this.height = rect.height; this._transformMat = es.Matrix2D.create().translate(-worldPosX - origin.x, -worldPosY - origin.y);
return this; this._tempMat = es.Matrix2D.create().scale(scale.x, scale.y);
this._transformMat = this._transformMat.multiply(this._tempMat);
this._tempMat = es.Matrix2D.create().rotate(rotation);
this._transformMat = this._transformMat.multiply(this._tempMat);
this._tempMat = es.Matrix2D.create().translate(worldPosX, worldPosY);
this._transformMat = this._transformMat.multiply(this._tempMat);
var topLeft = new es.Vector2(worldPosX, worldPosY);
var topRight = new es.Vector2(worldPosX + width, worldPosY);
var bottomLeft = new es.Vector2(worldPosX, worldPosY + height);
var bottomRight = new es.Vector2(worldPosX + width, worldPosY + height);
topLeft = es.Vector2Ext.transformR(topLeft, this._transformMat);
topRight = es.Vector2Ext.transformR(topRight, this._transformMat);
bottomLeft = es.Vector2Ext.transformR(bottomLeft, this._transformMat);
bottomRight = es.Vector2Ext.transformR(bottomRight, this._transformMat);
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
this.location = new es.Vector2(minX, minY);
this.width = maxX - minX;
this.height = maxY - minY;
}
}; };
Rectangle.rectEncompassingPoints = function (points) { Rectangle.rectEncompassingPoints = function (points) {
var minX = Number.POSITIVE_INFINITY; var minX = Number.POSITIVE_INFINITY;
@@ -6620,7 +6641,7 @@ var es;
this._store = new Map(); this._store = new Map();
} }
NumberDictionary.prototype.getKey = function (x, y) { NumberDictionary.prototype.getKey = function (x, y) {
return Long.fromNumber(x).shiftLeft(32).or(Long.fromNumber(y, false)).toString(); return Long.fromNumber(x).shiftLeft(32).or(Long.fromNumber(y, true)).toString();
}; };
NumberDictionary.prototype.add = function (x, y, list) { NumberDictionary.prototype.add = function (x, y, list) {
this._store.set(this.getKey(x, y), list); this._store.set(this.getKey(x, y), list);
+1 -1
View File
File diff suppressed because one or more lines are too long
+40 -15
View File
@@ -1,5 +1,7 @@
module es { module es {
export class Rectangle extends egret.Rectangle { export class Rectangle extends egret.Rectangle {
public _tempMat: Matrix2D;
public _transformMat: Matrix2D;
/** /**
* *
*/ */
@@ -141,22 +143,45 @@ module es {
} }
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number){ public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number){
this.x = parentPosition.x + position.x - origin.x * scale.x; if (rotation == 0){
this.y = parentPosition.y + position.y - origin.y * scale.y; this.x = parentPosition.x + position.x - origin.x * scale.x;
this.width = width * scale.x; this.y = parentPosition.y + position.y - origin.y * scale.y;
this.height = height * 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;
/** // 将参考点设置为世界参考
* egret矩形转化为Rectangle this._transformMat = Matrix2D.create().translate(-worldPosX - origin.x, -worldPosY - origin.y);
* @param rect this._tempMat = Matrix2D.create().scale(scale.x, scale.y);
*/ this._transformMat = this._transformMat.multiply(this._tempMat);
public setEgretRect(rect: egret.Rectangle): Rectangle{ this._tempMat = Matrix2D.create().rotate(rotation);
this.x = rect.x; this._transformMat = this._transformMat.multiply(this._tempMat);
this.y = rect.y; this._tempMat = Matrix2D.create().translate(worldPosX, worldPosY);
this.width = rect.width; this._transformMat = this._transformMat.multiply(this._tempMat);
this.height = rect.height;
return this; // TODO: 这有点傻。我们可以把世界变换留在矩阵中,避免在世界空间中得到所有的四个角
let topLeft = new Vector2(worldPosX, worldPosY);
let topRight = new Vector2(worldPosX + width, worldPosY);
let bottomLeft = new Vector2(worldPosX, worldPosY + height);
let bottomRight = new Vector2(worldPosX + width, worldPosY + height);
topLeft = Vector2Ext.transformR(topLeft, this._transformMat);
topRight = Vector2Ext.transformR(topRight, this._transformMat);
bottomLeft = Vector2Ext.transformR(bottomLeft, this._transformMat);
bottomRight = Vector2Ext.transformR(bottomRight, this._transformMat);
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);
let maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
this.location = new Vector2(minX, minY);
this.width = maxX - minX;
this.height = maxY - minY;
}
} }
/** /**
+1 -1
View File
@@ -231,7 +231,7 @@ module es {
* @param y * @param y
*/ */
private getKey(x: number, y: number): string { private getKey(x: number, y: number): string {
return Long.fromNumber(x).shiftLeft(32).or(Long.fromNumber(y, false)).toString(); return Long.fromNumber(x).shiftLeft(32).or(Long.fromNumber(y, true)).toString();
} }
public add(x: number, y: number, list: Collider[]) { public add(x: number, y: number, list: Collider[]) {