新增Physics.linecast/linecastAll方法

This commit is contained in:
YHH
2020-08-22 12:21:40 +08:00
parent 73f0f54ba7
commit 6c1cfec928
16 changed files with 537 additions and 145 deletions
+20 -15
View File
@@ -1285,6 +1285,7 @@ declare module es {
static clamp01(value: number): number;
static angleBetweenVectors(from: Vector2, to: Vector2): number;
static incrementWithWrap(t: number, length: number): number;
static approach(start: number, end: number, shift: number): number;
}
}
declare module es {
@@ -1375,6 +1376,21 @@ declare module es {
static getSector(rX: number, rY: number, rW: number, rH: number, point: Vector2): PointSectors;
}
}
declare module es {
class RaycastHit {
collider: Collider;
fraction: number;
distance: number;
point: Vector2;
normal: Vector2;
centroid: Vector2;
constructor(collider?: Collider, fraction?: number, distance?: number, point?: Vector2, normal?: Vector2);
setValues(collider: Collider, fraction: number, distance: number, point: Vector2): void;
setValuesNonCollider(fraction: number, distance: number, point: Vector2, normal: Vector2): void;
reset(): void;
toString(): string;
}
}
declare module es {
class Physics {
static spatialHashCellSize: number;
@@ -1382,6 +1398,7 @@ declare module es {
private static _spatialHash;
static raycastsHitTriggers: boolean;
static raycastsStartInColliders: boolean;
static _hitArray: RaycastHit[];
static reset(): void;
static clear(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
@@ -1390,6 +1407,8 @@ declare module es {
static addCollider(collider: Collider): void;
static removeCollider(collider: Collider): void;
static updateCollider(collider: Collider): void;
static linecast(start: Vector2, end: Vector2, layerMask?: number): RaycastHit;
static linecastAll(start: Vector2, end: Vector2, hits: RaycastHit[], layerMask?: number): number;
static debugDraw(secondsToDisplay: any): void;
}
}
@@ -1401,21 +1420,6 @@ declare module es {
constructor(position: Vector2, end: Vector2);
}
}
declare module es {
class RaycastHit {
collider: Collider;
fraction: number;
distance: number;
point: Vector2;
normal: Vector2;
centroid: Vector2;
constructor(collider: Collider, fraction: number, distance: number, point: Vector2, normal: Vector2);
setValues(collider: Collider, fraction: number, distance: number, point: Vector2): void;
setValuesNonCollider(fraction: number, distance: number, point: Vector2, normal: Vector2): void;
reset(): void;
toString(): string;
}
}
declare module es {
abstract class Shape {
position: Vector2;
@@ -1539,6 +1543,7 @@ declare module es {
clear(): void;
debugDraw(secondsToDisplay: number, textScale?: number): void;
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[];
linecast(start: Vector2, end: Vector2, hits: RaycastHit[], layerMask: number): number;
overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number;
private cellCoords;
private cellAtPosition;
+115 -49
View File
@@ -1279,6 +1279,7 @@ var es;
this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this);
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
es.Input.initialize();
KeyboardUtils.init();
this.initialize();
};
Core.debugRenderEndabled = false;
@@ -5972,6 +5973,11 @@ var es;
return 0;
return t;
};
MathHelper.approach = function (start, end, shift) {
if (start < end)
return Math.min(start + shift, end);
return Math.max(start - shift, end);
};
MathHelper.Epsilon = 0.00001;
MathHelper.Rad2Deg = 57.29578;
MathHelper.Deg2Rad = 0.0174532924;
@@ -6614,6 +6620,43 @@ var es;
es.Collisions = Collisions;
})(es || (es = {}));
var es;
(function (es) {
var RaycastHit = (function () {
function RaycastHit(collider, fraction, distance, point, normal) {
this.fraction = 0;
this.distance = 0;
this.point = es.Vector2.zero;
this.normal = es.Vector2.zero;
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
this.point = point;
this.centroid = es.Vector2.zero;
}
RaycastHit.prototype.setValues = function (collider, fraction, distance, point) {
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
this.point = point;
};
RaycastHit.prototype.setValuesNonCollider = function (fraction, distance, point, normal) {
this.fraction = fraction;
this.distance = distance;
this.point = point;
this.normal = normal;
};
RaycastHit.prototype.reset = function () {
this.collider = null;
this.fraction = this.distance = 0;
};
RaycastHit.prototype.toString = function () {
return "[RaycastHit] fraction: " + this.fraction + ", distance: " + this.distance + ", normal: " + this.normal + ", centroid: " + this.centroid + ", point: " + this.point;
};
return RaycastHit;
}());
es.RaycastHit = RaycastHit;
})(es || (es = {}));
var es;
(function (es) {
var Physics = (function () {
function Physics() {
@@ -6650,6 +6693,20 @@ var es;
this._spatialHash.remove(collider);
this._spatialHash.register(collider);
};
Physics.linecast = function (start, end, layerMask) {
if (layerMask === void 0) { layerMask = Physics.allLayers; }
this._hitArray[0].reset();
this.linecastAll(start, end, this._hitArray, layerMask);
return this._hitArray[0];
};
Physics.linecastAll = function (start, end, hits, layerMask) {
if (layerMask === void 0) { layerMask = Physics.allLayers; }
if (hits.length == 0) {
console.warn("传入了一个空的hits数组。没有点击会被返回");
return 0;
}
return this._spatialHash.linecast(start, end, hits, layerMask);
};
Physics.debugDraw = function (secondsToDisplay) {
this._spatialHash.debugDraw(secondsToDisplay, 2);
};
@@ -6657,6 +6714,9 @@ var es;
Physics.allLayers = -1;
Physics.raycastsHitTriggers = false;
Physics.raycastsStartInColliders = false;
Physics._hitArray = [
new es.RaycastHit()
];
return Physics;
}());
es.Physics = Physics;
@@ -6674,43 +6734,6 @@ var es;
es.Ray2D = Ray2D;
})(es || (es = {}));
var es;
(function (es) {
var RaycastHit = (function () {
function RaycastHit(collider, fraction, distance, point, normal) {
this.fraction = 0;
this.distance = 0;
this.point = es.Vector2.zero;
this.normal = es.Vector2.zero;
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
this.point = point;
this.centroid = es.Vector2.zero;
}
RaycastHit.prototype.setValues = function (collider, fraction, distance, point) {
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
this.point = point;
};
RaycastHit.prototype.setValuesNonCollider = function (fraction, distance, point, normal) {
this.fraction = fraction;
this.distance = distance;
this.point = point;
this.normal = normal;
};
RaycastHit.prototype.reset = function () {
this.collider = null;
this.fraction = this.distance = 0;
};
RaycastHit.prototype.toString = function () {
return "[RaycastHit] fraction: " + this.fraction + ", distance: " + this.distance + ", normal: " + this.normal + ", centroid: " + this.centroid + ", point: " + this.point;
};
return RaycastHit;
}());
es.RaycastHit = RaycastHit;
})(es || (es = {}));
var es;
(function (es) {
var Shape = (function () {
function Shape() {
@@ -7467,6 +7490,48 @@ var es;
}
return this._tempHashSet;
};
SpatialHash.prototype.linecast = function (start, end, hits, layerMask) {
var ray = new es.Ray2D(start, end);
this._raycastParser.start(ray, hits, layerMask);
var currentCell = this.cellCoords(start.x, start.y);
var lastCell = this.cellCoords(end.x, end.y);
var stepX = Math.sign(ray.direction.x);
var stepY = Math.sign(ray.direction.y);
if (currentCell.x == lastCell.x)
stepX = 0;
if (currentCell.y == lastCell.y)
stepY = 0;
var xStep = stepX < 0 ? 0 : stepX;
var yStep = stepY < 0 ? 0 : stepY;
var nextBoundaryX = (currentCell.x + xStep) * this._cellSize;
var nextBoundaryY = (currentCell.y + yStep) * this._cellSize;
var tMaxX = ray.direction.x != 0 ? (nextBoundaryX - ray.start.x) / ray.direction.x : Number.MAX_VALUE;
var tMaxY = ray.direction.y != 0 ? (nextBoundaryY - ray.start.y) / ray.direction.y : Number.MAX_VALUE;
var tDeltaX = ray.direction.x != 0 ? this._cellSize / (ray.direction.x * stepX) : Number.MAX_VALUE;
var tDeltaY = ray.direction.y != 0 ? this._cellSize / (ray.direction.y * stepY) : Number.MAX_VALUE;
var cell = this.cellAtPosition(currentCell.x, currentCell.y);
if (cell && this._raycastParser.checkRayIntersection(currentCell.x, currentCell.y, cell)) {
this._raycastParser.reset();
return this._raycastParser.hitCounter;
}
while (currentCell.x != lastCell.x || currentCell.y != lastCell.y) {
if (tMaxX < tMaxY) {
currentCell.x = es.MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX));
tMaxX += tDeltaX;
}
else {
currentCell.y = es.MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY));
tMaxY += tDeltaY;
}
cell = this.cellAtPosition(currentCell.x, currentCell.y);
if (cell && this._raycastParser.checkRayIntersection(currentCell.x, currentCell.y, cell)) {
this._raycastParser.reset();
return this._raycastParser.hitCounter;
}
}
this._raycastParser.reset();
return this._raycastParser.hitCounter;
};
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius;
@@ -7547,6 +7612,7 @@ var es;
es.NumberDictionary = NumberDictionary;
var RaycastResultParser = (function () {
function RaycastResultParser() {
this._tempHit = new es.RaycastHit();
this._checkedColliders = [];
this._cellHits = [];
}
@@ -9696,10 +9762,10 @@ var KeyboardUtils = (function () {
function KeyboardUtils() {
}
KeyboardUtils.init = function () {
this.keyDownDict = {};
this.keyUpDict = {};
document.addEventListener("keydown", this.onKeyDonwHander);
document.addEventListener("keyup", this.onKeyUpHander);
KeyboardUtils.keyDownDict = {};
KeyboardUtils.keyUpDict = {};
document.addEventListener("keydown", KeyboardUtils.onKeyDonwHander);
document.addEventListener("keyup", KeyboardUtils.onKeyUpHander);
};
KeyboardUtils.registerKey = function (key, fun, thisObj, type) {
if (type === void 0) { type = 0; }
@@ -9716,16 +9782,16 @@ var KeyboardUtils = (function () {
delete keyDict[key];
};
KeyboardUtils.destroy = function () {
this.keyDownDict = null;
this.keyUpDict = null;
KeyboardUtils.keyDownDict = null;
KeyboardUtils.keyUpDict = null;
document.removeEventListener("keydown", this.onKeyDonwHander);
document.removeEventListener("keyup", this.onKeyUpHander);
};
KeyboardUtils.onKeyDonwHander = function (event) {
if (!this.keyDownDict)
if (!KeyboardUtils.keyDownDict)
return;
var key = this.keyCodeToString(event.keyCode);
var o = this.keyDownDict[key];
var key = KeyboardUtils.keyCodeToString(event.keyCode);
var o = KeyboardUtils.keyDownDict[key];
if (o) {
var fun = o["fun"];
var thisObj = o["thisObj"];
@@ -9734,10 +9800,10 @@ var KeyboardUtils = (function () {
}
};
KeyboardUtils.onKeyUpHander = function (event) {
if (!this.keyUpDict)
if (!KeyboardUtils.keyUpDict)
return;
var key = this.keyCodeToString(event.keyCode);
var o = this.keyUpDict[key];
var key = KeyboardUtils.keyCodeToString(event.keyCode);
var o = KeyboardUtils.keyUpDict[key];
if (o) {
var fun = o["fun"];
var thisObj = o["thisObj"];
File diff suppressed because one or more lines are too long
+3 -1
View File
@@ -17,7 +17,7 @@
"bin-debug/UI/mvc/BaseView.js",
"bin-debug/SampleHelpers/SampleScene.js",
"bin-debug/UI/loading/LoadingView.js",
"bin-debug/Scenes/Empty Scene/BasicScene.js",
"bin-debug/Scenes/LineCasting/LineCaster.js",
"bin-debug/Fgui/common/UI_com_tips.js",
"bin-debug/Fgui/loading/loadingBinder.js",
"bin-debug/Fgui/loading/UI_View_loading.js",
@@ -26,7 +26,9 @@
"bin-debug/Fgui/sc/UI_View_sc.js",
"bin-debug/Platform.js",
"bin-debug/Scenes/Animated Tiles/AnimatedTilesScene.js",
"bin-debug/Scenes/Empty Scene/BasicScene.js",
"bin-debug/Main.js",
"bin-debug/Scenes/LineCasting/LineCastingScene.js",
"bin-debug/UI/PopManager.js",
"bin-debug/UI/loading/LoadingControl.js",
"bin-debug/UI/loading/LoadingEvents.js",
+105
View File
@@ -0,0 +1,105 @@
module samples {
import RenderableComponent = es.RenderableComponent;
import Vector2 = es.Vector2;
import Physics = es.Physics;
export class LineCaster extends RenderableComponent {
private _lastPosition = new Vector2(101, 101);
private _collisionPosition = new Vector2(-1, -1);
private _pixelShape1: egret.Shape;
private _pixelShape2: egret.Shape;
private _lineShape: egret.Shape;
private _pixelShape3: egret.Shape;
private _delayTime = 1;
private _pressTime = 0;
private _canTouch = true;
public get width(){
return 1000;
}
public get height(){
return 1000;
}
constructor() {
super();
this._pixelShape1 = new egret.Shape();
this._pixelShape2 = new egret.Shape();
this._lineShape = new egret.Shape();
this._pixelShape3 = new egret.Shape();
this.displayObject = new egret.DisplayObjectContainer();
let displayContainer = this.displayObject as egret.DisplayObjectContainer;
displayContainer.addChild(this._pixelShape1);
displayContainer.addChild(this._pixelShape2);
displayContainer.addChild(this._pixelShape3);
displayContainer.addChild(this._lineShape);
}
public onAddedToEntity(): void {
KeyboardUtils.registerKey(KeyboardUtils.SPACE, this.spaceDown, this, KeyboardUtils.TYPE_KEY_DOWN);
}
public onRemovedFromEntity(): void {
KeyboardUtils.unregisterKey(KeyboardUtils.SPACE, KeyboardUtils.TYPE_KEY_DOWN);
}
private spaceDown(){
let hit = Physics.linecast(this._lastPosition, this.transform.position);
if (hit.collider){
this._collisionPosition = hit.point;
}
}
public render(camera: es.Camera): any {
this._pixelShape1.graphics.clear();
this._pixelShape1.graphics.beginFill(0xffff00);
this._pixelShape1.graphics.lineStyle(4, 0xffff00);
this._pixelShape1.graphics.lineTo(this._lastPosition.x, this._lastPosition.y);
this._pixelShape1.graphics.endFill();
this._pixelShape2.graphics.clear();
this._pixelShape2.graphics.beginFill(0xffffff);
this._pixelShape2.graphics.lineStyle(4, 0xffffff);
this._pixelShape2.graphics.lineTo(this.transform.position.x, this.transform.position.y);
this._pixelShape2.graphics.endFill();
this._lineShape.graphics.clear();
this._lineShape.graphics.beginFill(0xffffff);
this._lineShape.graphics.lineStyle(1, 0xffffff);
this._lineShape.graphics.lineTo(this._lastPosition.x, this._lastPosition.y);
this._lineShape.graphics.lineTo(this.transform.position.x, this.transform.position.y);
this._lineShape.graphics.endFill();
this._pixelShape3.graphics.clear();
if (this._collisionPosition.x > 0 && this._collisionPosition.y > 0){
this._pixelShape3.graphics.beginFill(0xff0000);
this._pixelShape3.graphics.lineStyle(10, 0xff0000);
this._pixelShape3.graphics.lineTo(this._collisionPosition.x, this._collisionPosition.y);
this._pixelShape3.graphics.endFill();
}
}
public update(): void {
if (!this._canTouch){
if (this._pressTime > this._delayTime){
this._canTouch = true;
this._pressTime = 0;
}else{
this._pressTime += es.Time.deltaTime;
}
return;
}
if (!es.Input.touchPosition.equals(Vector2.zero)){
this._lastPosition = this.transform.position;
this.transform.position = es.Input.touchPosition;
this._collisionPosition = new Vector2(-1, -1);
this._canTouch = false;
}
}
}
}
@@ -0,0 +1,25 @@
module samples {
import Vector2 = es.Vector2;
import SpriteRenderer = es.SpriteRenderer;
import BoxCollider = es.BoxCollider;
export class LineCastingScene extends SampleScene {
public initialize(): void {
super.initialize();
this.content.loadRes("moon_png").then((moonTex: egret.Texture) => {
let playerEntity = this.createEntity("player");
playerEntity.position = new Vector2(es.Core.scene.width / 2, es.Core.scene.height / 2);
playerEntity.addComponent(new SpriteRenderer(moonTex));
let coll = new BoxCollider().setSize(moonTex.textureWidth, moonTex.textureHeight);
playerEntity.addComponent(coll);
playerEntity.position = new Vector2(200, 100);
let lineCaster = this.createEntity("linecaster").addComponent(new LineCaster());
lineCaster.transform.position = new Vector2(300, 100);
manager.AlterManager.alter_tips("lineCasting加载成功");
});
}
}
}
+2
View File
@@ -4,6 +4,7 @@ module sc {
private _sceneList: SceneData[] = [
new SceneData("空白场景", samples.BasicScene),
new SceneData("Tiled Tiles", samples.AnimatedTilesScene),
new SceneData("Linecasting", samples.LineCastingScene),
];
constructor() {
@@ -32,6 +33,7 @@ module sc {
private scItemOnClick(evt: egret.Event){
let data = evt.currentTarget.data;
es.Core.scene = new data();
es.Core.scene.camera.position = es.Vector2.zero;
}
public destroy() {
+20 -15
View File
@@ -1285,6 +1285,7 @@ declare module es {
static clamp01(value: number): number;
static angleBetweenVectors(from: Vector2, to: Vector2): number;
static incrementWithWrap(t: number, length: number): number;
static approach(start: number, end: number, shift: number): number;
}
}
declare module es {
@@ -1375,6 +1376,21 @@ declare module es {
static getSector(rX: number, rY: number, rW: number, rH: number, point: Vector2): PointSectors;
}
}
declare module es {
class RaycastHit {
collider: Collider;
fraction: number;
distance: number;
point: Vector2;
normal: Vector2;
centroid: Vector2;
constructor(collider?: Collider, fraction?: number, distance?: number, point?: Vector2, normal?: Vector2);
setValues(collider: Collider, fraction: number, distance: number, point: Vector2): void;
setValuesNonCollider(fraction: number, distance: number, point: Vector2, normal: Vector2): void;
reset(): void;
toString(): string;
}
}
declare module es {
class Physics {
static spatialHashCellSize: number;
@@ -1382,6 +1398,7 @@ declare module es {
private static _spatialHash;
static raycastsHitTriggers: boolean;
static raycastsStartInColliders: boolean;
static _hitArray: RaycastHit[];
static reset(): void;
static clear(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
@@ -1390,6 +1407,8 @@ declare module es {
static addCollider(collider: Collider): void;
static removeCollider(collider: Collider): void;
static updateCollider(collider: Collider): void;
static linecast(start: Vector2, end: Vector2, layerMask?: number): RaycastHit;
static linecastAll(start: Vector2, end: Vector2, hits: RaycastHit[], layerMask?: number): number;
static debugDraw(secondsToDisplay: any): void;
}
}
@@ -1401,21 +1420,6 @@ declare module es {
constructor(position: Vector2, end: Vector2);
}
}
declare module es {
class RaycastHit {
collider: Collider;
fraction: number;
distance: number;
point: Vector2;
normal: Vector2;
centroid: Vector2;
constructor(collider: Collider, fraction: number, distance: number, point: Vector2, normal: Vector2);
setValues(collider: Collider, fraction: number, distance: number, point: Vector2): void;
setValuesNonCollider(fraction: number, distance: number, point: Vector2, normal: Vector2): void;
reset(): void;
toString(): string;
}
}
declare module es {
abstract class Shape {
position: Vector2;
@@ -1539,6 +1543,7 @@ declare module es {
clear(): void;
debugDraw(secondsToDisplay: number, textScale?: number): void;
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[];
linecast(start: Vector2, end: Vector2, hits: RaycastHit[], layerMask: number): number;
overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number;
private cellCoords;
private cellAtPosition;
+115 -49
View File
@@ -1279,6 +1279,7 @@ var es;
this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this);
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
es.Input.initialize();
KeyboardUtils.init();
this.initialize();
};
Core.debugRenderEndabled = false;
@@ -5972,6 +5973,11 @@ var es;
return 0;
return t;
};
MathHelper.approach = function (start, end, shift) {
if (start < end)
return Math.min(start + shift, end);
return Math.max(start - shift, end);
};
MathHelper.Epsilon = 0.00001;
MathHelper.Rad2Deg = 57.29578;
MathHelper.Deg2Rad = 0.0174532924;
@@ -6614,6 +6620,43 @@ var es;
es.Collisions = Collisions;
})(es || (es = {}));
var es;
(function (es) {
var RaycastHit = (function () {
function RaycastHit(collider, fraction, distance, point, normal) {
this.fraction = 0;
this.distance = 0;
this.point = es.Vector2.zero;
this.normal = es.Vector2.zero;
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
this.point = point;
this.centroid = es.Vector2.zero;
}
RaycastHit.prototype.setValues = function (collider, fraction, distance, point) {
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
this.point = point;
};
RaycastHit.prototype.setValuesNonCollider = function (fraction, distance, point, normal) {
this.fraction = fraction;
this.distance = distance;
this.point = point;
this.normal = normal;
};
RaycastHit.prototype.reset = function () {
this.collider = null;
this.fraction = this.distance = 0;
};
RaycastHit.prototype.toString = function () {
return "[RaycastHit] fraction: " + this.fraction + ", distance: " + this.distance + ", normal: " + this.normal + ", centroid: " + this.centroid + ", point: " + this.point;
};
return RaycastHit;
}());
es.RaycastHit = RaycastHit;
})(es || (es = {}));
var es;
(function (es) {
var Physics = (function () {
function Physics() {
@@ -6650,6 +6693,20 @@ var es;
this._spatialHash.remove(collider);
this._spatialHash.register(collider);
};
Physics.linecast = function (start, end, layerMask) {
if (layerMask === void 0) { layerMask = Physics.allLayers; }
this._hitArray[0].reset();
this.linecastAll(start, end, this._hitArray, layerMask);
return this._hitArray[0];
};
Physics.linecastAll = function (start, end, hits, layerMask) {
if (layerMask === void 0) { layerMask = Physics.allLayers; }
if (hits.length == 0) {
console.warn("传入了一个空的hits数组。没有点击会被返回");
return 0;
}
return this._spatialHash.linecast(start, end, hits, layerMask);
};
Physics.debugDraw = function (secondsToDisplay) {
this._spatialHash.debugDraw(secondsToDisplay, 2);
};
@@ -6657,6 +6714,9 @@ var es;
Physics.allLayers = -1;
Physics.raycastsHitTriggers = false;
Physics.raycastsStartInColliders = false;
Physics._hitArray = [
new es.RaycastHit()
];
return Physics;
}());
es.Physics = Physics;
@@ -6674,43 +6734,6 @@ var es;
es.Ray2D = Ray2D;
})(es || (es = {}));
var es;
(function (es) {
var RaycastHit = (function () {
function RaycastHit(collider, fraction, distance, point, normal) {
this.fraction = 0;
this.distance = 0;
this.point = es.Vector2.zero;
this.normal = es.Vector2.zero;
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
this.point = point;
this.centroid = es.Vector2.zero;
}
RaycastHit.prototype.setValues = function (collider, fraction, distance, point) {
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
this.point = point;
};
RaycastHit.prototype.setValuesNonCollider = function (fraction, distance, point, normal) {
this.fraction = fraction;
this.distance = distance;
this.point = point;
this.normal = normal;
};
RaycastHit.prototype.reset = function () {
this.collider = null;
this.fraction = this.distance = 0;
};
RaycastHit.prototype.toString = function () {
return "[RaycastHit] fraction: " + this.fraction + ", distance: " + this.distance + ", normal: " + this.normal + ", centroid: " + this.centroid + ", point: " + this.point;
};
return RaycastHit;
}());
es.RaycastHit = RaycastHit;
})(es || (es = {}));
var es;
(function (es) {
var Shape = (function () {
function Shape() {
@@ -7467,6 +7490,48 @@ var es;
}
return this._tempHashSet;
};
SpatialHash.prototype.linecast = function (start, end, hits, layerMask) {
var ray = new es.Ray2D(start, end);
this._raycastParser.start(ray, hits, layerMask);
var currentCell = this.cellCoords(start.x, start.y);
var lastCell = this.cellCoords(end.x, end.y);
var stepX = Math.sign(ray.direction.x);
var stepY = Math.sign(ray.direction.y);
if (currentCell.x == lastCell.x)
stepX = 0;
if (currentCell.y == lastCell.y)
stepY = 0;
var xStep = stepX < 0 ? 0 : stepX;
var yStep = stepY < 0 ? 0 : stepY;
var nextBoundaryX = (currentCell.x + xStep) * this._cellSize;
var nextBoundaryY = (currentCell.y + yStep) * this._cellSize;
var tMaxX = ray.direction.x != 0 ? (nextBoundaryX - ray.start.x) / ray.direction.x : Number.MAX_VALUE;
var tMaxY = ray.direction.y != 0 ? (nextBoundaryY - ray.start.y) / ray.direction.y : Number.MAX_VALUE;
var tDeltaX = ray.direction.x != 0 ? this._cellSize / (ray.direction.x * stepX) : Number.MAX_VALUE;
var tDeltaY = ray.direction.y != 0 ? this._cellSize / (ray.direction.y * stepY) : Number.MAX_VALUE;
var cell = this.cellAtPosition(currentCell.x, currentCell.y);
if (cell && this._raycastParser.checkRayIntersection(currentCell.x, currentCell.y, cell)) {
this._raycastParser.reset();
return this._raycastParser.hitCounter;
}
while (currentCell.x != lastCell.x || currentCell.y != lastCell.y) {
if (tMaxX < tMaxY) {
currentCell.x = es.MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX));
tMaxX += tDeltaX;
}
else {
currentCell.y = es.MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY));
tMaxY += tDeltaY;
}
cell = this.cellAtPosition(currentCell.x, currentCell.y);
if (cell && this._raycastParser.checkRayIntersection(currentCell.x, currentCell.y, cell)) {
this._raycastParser.reset();
return this._raycastParser.hitCounter;
}
}
this._raycastParser.reset();
return this._raycastParser.hitCounter;
};
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius;
@@ -7547,6 +7612,7 @@ var es;
es.NumberDictionary = NumberDictionary;
var RaycastResultParser = (function () {
function RaycastResultParser() {
this._tempHit = new es.RaycastHit();
this._checkedColliders = [];
this._cellHits = [];
}
@@ -9696,10 +9762,10 @@ var KeyboardUtils = (function () {
function KeyboardUtils() {
}
KeyboardUtils.init = function () {
this.keyDownDict = {};
this.keyUpDict = {};
document.addEventListener("keydown", this.onKeyDonwHander);
document.addEventListener("keyup", this.onKeyUpHander);
KeyboardUtils.keyDownDict = {};
KeyboardUtils.keyUpDict = {};
document.addEventListener("keydown", KeyboardUtils.onKeyDonwHander);
document.addEventListener("keyup", KeyboardUtils.onKeyUpHander);
};
KeyboardUtils.registerKey = function (key, fun, thisObj, type) {
if (type === void 0) { type = 0; }
@@ -9716,16 +9782,16 @@ var KeyboardUtils = (function () {
delete keyDict[key];
};
KeyboardUtils.destroy = function () {
this.keyDownDict = null;
this.keyUpDict = null;
KeyboardUtils.keyDownDict = null;
KeyboardUtils.keyUpDict = null;
document.removeEventListener("keydown", this.onKeyDonwHander);
document.removeEventListener("keyup", this.onKeyUpHander);
};
KeyboardUtils.onKeyDonwHander = function (event) {
if (!this.keyDownDict)
if (!KeyboardUtils.keyDownDict)
return;
var key = this.keyCodeToString(event.keyCode);
var o = this.keyDownDict[key];
var key = KeyboardUtils.keyCodeToString(event.keyCode);
var o = KeyboardUtils.keyDownDict[key];
if (o) {
var fun = o["fun"];
var thisObj = o["thisObj"];
@@ -9734,10 +9800,10 @@ var KeyboardUtils = (function () {
}
};
KeyboardUtils.onKeyUpHander = function (event) {
if (!this.keyUpDict)
if (!KeyboardUtils.keyUpDict)
return;
var key = this.keyCodeToString(event.keyCode);
var o = this.keyUpDict[key];
var key = KeyboardUtils.keyCodeToString(event.keyCode);
var o = KeyboardUtils.keyUpDict[key];
if (o) {
var fun = o["fun"];
var thisObj = o["thisObj"];
+1 -1
View File
File diff suppressed because one or more lines are too long
+1
View File
@@ -228,6 +228,7 @@ module es {
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
Input.initialize();
KeyboardUtils.init();
this.initialize();
}
}
+13
View File
@@ -93,5 +93,18 @@ module es {
return t;
}
/**
* start可以小于或大于end例如:开始是21046
* @param start
* @param end
* @param shift
*/
public static approach(start: number, end: number, shift: number): number {
if (start < end)
return Math.min(start + shift, end);
return Math.max(start - shift, end);
}
}
}
+35
View File
@@ -1,3 +1,4 @@
///<reference path="./RaycastHit.ts" />
module es {
export class Physics {
/** 调用reset并创建一个新的SpatialHash时使用的单元格大小 */
@@ -13,6 +14,12 @@ module es {
* 线/线
*/
public static raycastsStartInColliders = false;
/**
* raycast发生时分配它
*/
public static _hitArray: RaycastHit[] = [
new RaycastHit()
];
public static reset() {
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
@@ -85,6 +92,34 @@ module es {
this._spatialHash.register(collider);
}
/**
* layerMask匹配的碰撞器的第一次命中
* @param start
* @param end
* @param layerMask
*/
public static linecast(start: Vector2, end: Vector2, layerMask: number = Physics.allLayers): RaycastHit{
this._hitArray[0].reset();
this.linecastAll(start, end, this._hitArray, layerMask);
return this._hitArray[0];
}
/**
* hits数组
* @param start
* @param end
* @param hits
* @param layerMask
*/
public static linecastAll(start: Vector2, end: Vector2, hits: RaycastHit[], layerMask: number = Physics.allLayers){
if (hits.length == 0){
console.warn("传入了一个空的hits数组。没有点击会被返回");
return 0;
}
return this._spatialHash.linecast(start, end, hits, layerMask);
}
/**
* debug绘制空间散列的内容
* @param secondsToDisplay
+1 -1
View File
@@ -30,7 +30,7 @@ module es {
*/
public centroid: Vector2;
constructor(collider: Collider, fraction: number, distance: number, point: Vector2, normal: Vector2){
constructor(collider?: Collider, fraction?: number, distance?: number, point?: Vector2, normal?: Vector2){
this.collider = collider;
this.fraction = fraction;
this.distance = distance;
+68 -1
View File
@@ -144,6 +144,73 @@ module es {
return this._tempHashSet;
}
/**
* hits数组
* @param start
* @param end
* @param hits
* @param layerMask
*/
public linecast(start: Vector2, end: Vector2, hits: RaycastHit[], layerMask: number){
let ray = new Ray2D(start, end);
this._raycastParser.start(ray, hits, layerMask);
// 在与网格相同的空间中获取起始/结束位置
let currentCell = this.cellCoords(start.x, start.y);
let lastCell = this.cellCoords(end.x, end.y);
let stepX = Math.sign(ray.direction.x);
let stepY = Math.sign(ray.direction.y);
// 我们要确保,如果我们在同一行或同一行,我们不会步进不必要的方向
if (currentCell.x == lastCell.x) stepX = 0;
if (currentCell.y == lastCell.y) stepY = 0;
// 计算单元边界。当这一步是正的,下一个单元格在这一步之后意味着我们加1。
// 如果为负,则单元格在此之前,这种情况下不添加边界
let xStep = stepX < 0 ? 0 : stepX;
let yStep = stepY < 0 ? 0 : stepY;
let nextBoundaryX = (currentCell.x + xStep) * this._cellSize;
let nextBoundaryY = (currentCell.y + yStep) * this._cellSize;
// 确定射线穿过第一个垂直体素边界时的t值。y/horizontal。
// 这两个值的最小值将表明我们可以沿着射线走多少,而仍然保持在当前体素中,对于接近vertical/horizontal的射线来说可能是无限的
let tMaxX = ray.direction.x != 0 ? (nextBoundaryX - ray.start.x) / ray.direction.x : Number.MAX_VALUE;
let tMaxY = ray.direction.y != 0 ? (nextBoundaryY - ray.start.y) / ray.direction.y : Number.MAX_VALUE;
let tDeltaX = ray.direction.x != 0 ? this._cellSize / (ray.direction.x * stepX) : Number.MAX_VALUE;
let tDeltaY = ray.direction.y != 0 ? this._cellSize / (ray.direction.y * stepY) : Number.MAX_VALUE;
// 开始遍历并返回交叉单元格。
let cell = this.cellAtPosition(currentCell.x, currentCell.y);
if (cell && this._raycastParser.checkRayIntersection(currentCell.x, currentCell.y, cell)){
this._raycastParser.reset();
return this._raycastParser.hitCounter;
}
while (currentCell.x != lastCell.x || currentCell.y != lastCell.y){
if (tMaxX < tMaxY){
currentCell.x = MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX));
tMaxX += tDeltaX;
}else{
currentCell.y = MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY));
tMaxY += tDeltaY;
}
cell = this.cellAtPosition(currentCell.x, currentCell.y);
if (cell && this._raycastParser.checkRayIntersection(currentCell.x, currentCell.y, cell)){
this._raycastParser.reset();
return this._raycastParser.hitCounter;
}
}
this._raycastParser.reset();
return this._raycastParser.hitCounter;
}
/**
*
* @param circleCenter
@@ -268,7 +335,7 @@ module es {
};
public _hits: RaycastHit[];
public _tempHit: RaycastHit;
public _tempHit: RaycastHit = new RaycastHit();
public _checkedColliders: Collider[] = [];
public _cellHits: RaycastHit[] = [];
public _ray: Ray2D;
+12 -12
View File
@@ -84,10 +84,10 @@ class KeyboardUtils {
private static keyUpDict: Object;
public static init(): void {
this.keyDownDict = {};
this.keyUpDict = {};
document.addEventListener("keydown", this.onKeyDonwHander);
document.addEventListener("keyup", this.onKeyUpHander);
KeyboardUtils.keyDownDict = {};
KeyboardUtils.keyUpDict = {};
document.addEventListener("keydown", KeyboardUtils.onKeyDonwHander);
document.addEventListener("keyup", KeyboardUtils.onKeyUpHander);
}
/**
@@ -115,16 +115,16 @@ class KeyboardUtils {
*
*/
public static destroy(): void {
this.keyDownDict = null;
this.keyUpDict = null;
KeyboardUtils.keyDownDict = null;
KeyboardUtils.keyUpDict = null;
document.removeEventListener("keydown", this.onKeyDonwHander);
document.removeEventListener("keyup", this.onKeyUpHander);
}
private static onKeyDonwHander(event: KeyboardEvent): void {
if (!this.keyDownDict) return;
var key: string = this.keyCodeToString(event.keyCode);
var o: Object = this.keyDownDict[key];
if (!KeyboardUtils.keyDownDict) return;
var key: string = KeyboardUtils.keyCodeToString(event.keyCode);
var o: Object = KeyboardUtils.keyDownDict[key];
if (o) {
var fun: Function = o["fun"];
var thisObj: any = o["thisObj"];
@@ -134,9 +134,9 @@ class KeyboardUtils {
}
private static onKeyUpHander(event: KeyboardEvent): void {
if (!this.keyUpDict) return;
var key: string = this.keyCodeToString(event.keyCode);
var o: Object = this.keyUpDict[key];
if (!KeyboardUtils.keyUpDict) return;
var key: string = KeyboardUtils.keyCodeToString(event.keyCode);
var o: Object = KeyboardUtils.keyUpDict[key];
if (o) {
var fun: Function = o["fun"];
var thisObj: any = o["thisObj"];