新增renderablecomponent显示
优化返回值
This commit is contained in:
@@ -122,7 +122,7 @@ module es {
|
||||
|
||||
this._bounds.location = new Vector2(minX, minY);
|
||||
this._bounds.width = maxX - minX;
|
||||
this._bounds.height = maxX - minY;
|
||||
this._bounds.height = maxY - minY;
|
||||
} else {
|
||||
this._bounds.location = topLeft;
|
||||
this._bounds.width = bottomRight.x - topLeft.x;
|
||||
|
||||
@@ -205,7 +205,7 @@ module es {
|
||||
* 检查这个形状是否与物理系统中的其他对撞机重叠
|
||||
* @param other
|
||||
*/
|
||||
public overlaps(other: Collider) {
|
||||
public overlaps(other: Collider): boolean {
|
||||
return this.shape.overlaps(other.shape);
|
||||
}
|
||||
|
||||
@@ -213,20 +213,21 @@ module es {
|
||||
* 检查这个与运动应用的碰撞器(移动向量)是否与碰撞器碰撞。如果是这样,将返回true,并且结果将填充碰撞数据。
|
||||
* @param collider
|
||||
* @param motion
|
||||
* @param result
|
||||
*/
|
||||
public collidesWith(collider: Collider, motion: Vector2) {
|
||||
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean {
|
||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||
let oldPosition = this.entity.position;
|
||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||
this.entity.position.add(motion);
|
||||
|
||||
let result = this.shape.collidesWithShape(collider.shape);
|
||||
if (result)
|
||||
let didCollide = this.shape.collidesWithShape(collider.shape, result);
|
||||
if (didCollide)
|
||||
result.collider = collider;
|
||||
|
||||
// 将图形位置返回到检查前的位置
|
||||
this.entity.position = oldPosition;
|
||||
|
||||
return result;
|
||||
return didCollide;
|
||||
}
|
||||
|
||||
public clone(): Component{
|
||||
|
||||
@@ -16,12 +16,11 @@ module es {
|
||||
/**
|
||||
* 计算修改运动矢量的运动,以考虑移动时可能发生的碰撞
|
||||
* @param motion
|
||||
* @param collisionResult
|
||||
*/
|
||||
public calculateMovement(motion: Vector2){
|
||||
let collisionResult = new CollisionResult();
|
||||
|
||||
public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean{
|
||||
if (!this.entity.getComponent(Collider) || !this._triggerHelper){
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 移动所有的非触发碰撞器并获得最近的碰撞
|
||||
@@ -37,9 +36,7 @@ module es {
|
||||
let bounds = collider.bounds;
|
||||
bounds.x += motion.x;
|
||||
bounds.y += motion.y;
|
||||
let boxcastResult = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
|
||||
bounds = boxcastResult.bounds;
|
||||
let neighbors = boxcastResult.tempHashSet;
|
||||
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
|
||||
|
||||
for (let j = 0; j < neighbors.length; j ++){
|
||||
let neighbor = neighbors[j];
|
||||
@@ -47,13 +44,13 @@ module es {
|
||||
if (neighbor.isTrigger)
|
||||
continue;
|
||||
|
||||
let _internalcollisionResult = collider.collidesWith(neighbor, motion);
|
||||
if (_internalcollisionResult){
|
||||
let _internalcollisionResult: CollisionResult = new CollisionResult();
|
||||
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)){
|
||||
// 如果碰撞 则退回之前的移动量
|
||||
motion = Vector2.subtract(motion, _internalcollisionResult.minimumTranslationVector);
|
||||
motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
||||
|
||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||
if (_internalcollisionResult.collider){
|
||||
if (_internalcollisionResult.collider != null){
|
||||
collisionResult = _internalcollisionResult;
|
||||
}
|
||||
}
|
||||
@@ -62,7 +59,7 @@ module es {
|
||||
|
||||
ListPool.free(colliders);
|
||||
|
||||
return {collisionResult: collisionResult, motion: motion};
|
||||
return collisionResult.collider != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,15 +78,12 @@ module es {
|
||||
/**
|
||||
* 通过调用calculateMovement和applyMovement来移动考虑碰撞的实体;
|
||||
* @param motion
|
||||
* @param collisionResult
|
||||
*/
|
||||
public move(motion: Vector2){
|
||||
let movementResult = this.calculateMovement(motion);
|
||||
let collisionResult = movementResult.collisionResult;
|
||||
motion = movementResult.motion;
|
||||
|
||||
public move(motion: Vector2, collisionResult: CollisionResult){
|
||||
this.calculateMovement(motion, collisionResult);
|
||||
this.applyMovement(motion);
|
||||
|
||||
return collisionResult;
|
||||
return collisionResult.collider != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,8 +28,8 @@ module es {
|
||||
|
||||
// 获取任何可能在新位置发生碰撞的东西
|
||||
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers);
|
||||
for (let i = 0; i < neighbors.colliders.length; i ++){
|
||||
let neighbor = neighbors.colliders[i];
|
||||
for (let i = 0; i < neighbors.length; i ++){
|
||||
let neighbor = neighbors[i];
|
||||
if (this._collider.overlaps(neighbor) && neighbor.enabled){
|
||||
didCollide = true;
|
||||
this.notifyTriggerListeners(this._collider, neighbor);
|
||||
|
||||
@@ -4,6 +4,10 @@ module es {
|
||||
* 所有可渲染组件的基类
|
||||
*/
|
||||
export abstract class RenderableComponent extends Component implements IRenderable {
|
||||
/**
|
||||
* 用于装载egret显示对象
|
||||
*/
|
||||
public displayObject: egret.DisplayObject = new egret.DisplayObject();
|
||||
/**
|
||||
* renderableComponent的宽度
|
||||
* 如果你不重写bounds属性则需要实现这个
|
||||
@@ -107,6 +111,7 @@ module es {
|
||||
* 如果渲染器不适用isVisibleFromCamera进行剔除检查 这些方法不会被调用
|
||||
*/
|
||||
protected onBecameVisible() {
|
||||
this.displayObject.visible = this.isVisible;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,6 +119,7 @@ module es {
|
||||
* 如果渲染器不适用isVisibleFromCamera进行剔除检查 这些方法不会被调用
|
||||
*/
|
||||
protected onBecameInvisible() {
|
||||
this.displayObject.visible = this.isVisible;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,6 +171,17 @@ module es {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 进行状态同步
|
||||
*/
|
||||
public sync(camera: Camera){
|
||||
this.displayObject.x = this.entity.position.x + this.localOffset.x - camera.position.x + camera.origin.x;
|
||||
this.displayObject.y = this.entity.position.y + this.localOffset.y - camera.position.y + camera.origin.y;
|
||||
this.displayObject.scaleX = this.entity.scale.x;
|
||||
this.displayObject.scaleY = this.entity.scale.y;
|
||||
this.displayObject.rotation = this.entity.rotation;
|
||||
}
|
||||
|
||||
public toString(){
|
||||
return `[RenderableComponent] renderLayer: ${this.renderLayer}`;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
module es {
|
||||
import Bitmap = egret.Bitmap;
|
||||
|
||||
export class SpriteRenderer extends RenderableComponent {
|
||||
public get bounds() {
|
||||
if (this._areBoundsDirty) {
|
||||
@@ -8,9 +10,9 @@ module es {
|
||||
this._sprite.sourceRect.height);
|
||||
this._areBoundsDirty = false;
|
||||
}
|
||||
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +85,10 @@ module es {
|
||||
this._sprite = sprite;
|
||||
if (this._sprite) {
|
||||
this._origin = this._sprite.origin;
|
||||
this.displayObject.anchorOffsetX = this._origin.x;
|
||||
this.displayObject.anchorOffsetY = this._origin.y;
|
||||
}
|
||||
this.displayObject = new Bitmap(sprite.texture2D);
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -95,6 +100,8 @@ module es {
|
||||
public setOrigin(origin: Vector2): SpriteRenderer {
|
||||
if (this._origin != origin) {
|
||||
this._origin = origin;
|
||||
this.displayObject.anchorOffsetX = this._origin.x;
|
||||
this.displayObject.anchorOffsetY = this._origin.y;
|
||||
this._areBoundsDirty = true;
|
||||
}
|
||||
|
||||
@@ -113,7 +120,7 @@ module es {
|
||||
}
|
||||
|
||||
public render(camera: Camera) {
|
||||
// TODO: render
|
||||
this.sync(camera);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,8 +81,8 @@ module es {
|
||||
this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this);
|
||||
this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this);
|
||||
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
|
||||
this.addEventListener(egret.Event.RENDER, this.draw, this);
|
||||
|
||||
Input.initialize();
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
@@ -135,6 +135,8 @@ module es {
|
||||
}
|
||||
|
||||
// this.endDebugUpdate();
|
||||
|
||||
await this.draw();
|
||||
}
|
||||
|
||||
public async draw() {
|
||||
|
||||
@@ -7,6 +7,8 @@ module transform {
|
||||
}
|
||||
|
||||
module es {
|
||||
import HashObject = egret.HashObject;
|
||||
|
||||
export enum DirtyType {
|
||||
clean,
|
||||
positionDirty,
|
||||
@@ -14,7 +16,7 @@ module es {
|
||||
rotationDirty,
|
||||
}
|
||||
|
||||
export class Transform {
|
||||
export class Transform extends HashObject {
|
||||
/** 与此转换关联的实体 */
|
||||
public readonly entity: Entity;
|
||||
/**
|
||||
@@ -243,6 +245,7 @@ module es {
|
||||
public _children: Transform[];
|
||||
|
||||
constructor(entity: Entity) {
|
||||
super();
|
||||
this.entity = entity;
|
||||
this.scale = Vector2.one;
|
||||
this._children = [];
|
||||
@@ -261,7 +264,7 @@ module es {
|
||||
* @param parent
|
||||
*/
|
||||
public setParent(parent: Transform): Transform {
|
||||
if (this._parent == parent)
|
||||
if (this._parent.equals(parent))
|
||||
return this;
|
||||
|
||||
if (!this._parent) {
|
||||
@@ -282,7 +285,7 @@ module es {
|
||||
*/
|
||||
public setPosition(x: number, y: number): Transform {
|
||||
let position = new Vector2(x, y);
|
||||
if (position == this._position)
|
||||
if (position.equals(this._position))
|
||||
return this;
|
||||
|
||||
this._position = position;
|
||||
@@ -301,7 +304,7 @@ module es {
|
||||
* @param localPosition
|
||||
*/
|
||||
public setLocalPosition(localPosition: Vector2): Transform {
|
||||
if (localPosition == this._localPosition)
|
||||
if (localPosition.equals(this._localPosition))
|
||||
return this;
|
||||
|
||||
this._localPosition = localPosition;
|
||||
@@ -493,5 +496,9 @@ module es {
|
||||
scale: ${this.scale}, localPosition: ${this._localPosition}, localRotation: ${this._localRotation},
|
||||
localScale: ${this._localScale}]`;
|
||||
}
|
||||
|
||||
public equals(other: Transform){
|
||||
return other.hashCode == this.hashCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,8 +76,11 @@ module es {
|
||||
let component = this._components[i];
|
||||
|
||||
// 处理渲染层列表
|
||||
if (component instanceof RenderableComponent)
|
||||
if (component instanceof RenderableComponent){
|
||||
this._entity.scene.removeChild(component.displayObject);
|
||||
this._entity.scene.renderableComponents.remove(component);
|
||||
}
|
||||
|
||||
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
|
||||
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
|
||||
@@ -88,8 +91,10 @@ module es {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components[i];
|
||||
|
||||
if (component instanceof RenderableComponent)
|
||||
if (component instanceof RenderableComponent){
|
||||
this._entity.scene.addChild(component.displayObject);
|
||||
this._entity.scene.renderableComponents.add(component);
|
||||
}
|
||||
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
|
||||
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
||||
@@ -112,8 +117,11 @@ module es {
|
||||
if (this._componentsToAdd.length > 0) {
|
||||
for (let i = 0, count = this._componentsToAdd.length; i < count; i++) {
|
||||
let component = this._componentsToAdd[i];
|
||||
if (component instanceof RenderableComponent)
|
||||
if (component instanceof RenderableComponent){
|
||||
this._entity.scene.addChild(component.displayObject);
|
||||
this._entity.scene.renderableComponents.add(component);
|
||||
}
|
||||
|
||||
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
|
||||
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
||||
@@ -148,8 +156,11 @@ module es {
|
||||
|
||||
public handleRemove(component: Component) {
|
||||
// 处理渲染层列表
|
||||
if (component instanceof RenderableComponent)
|
||||
if (component instanceof RenderableComponent){
|
||||
this._entity.scene.removeChild(component.displayObject);
|
||||
this._entity.scene.renderableComponents.remove(component);
|
||||
}
|
||||
|
||||
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
|
||||
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
///<reference path="./Renderer.ts" />
|
||||
module es {
|
||||
export class DefaultRenderer extends Renderer {
|
||||
constructor(){
|
||||
super(0, null);
|
||||
}
|
||||
|
||||
public render(scene: Scene) {
|
||||
let cam = this.camera ? this.camera : scene.camera;
|
||||
this.beginRender(cam);
|
||||
|
||||
@@ -9,6 +9,15 @@ module es {
|
||||
* Renderer子类可以选择调用beginRender时使用的摄像头
|
||||
*/
|
||||
public camera: Camera;
|
||||
/**
|
||||
* 指定场景调用渲染器的顺序
|
||||
*/
|
||||
public readonly renderOrder: number = 0;
|
||||
|
||||
protected constructor(renderOrder: number, camera: Camera = null){
|
||||
this.camera = camera;
|
||||
this.renderOrder = renderOrder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当渲染器被添加到场景时调用
|
||||
@@ -16,17 +25,18 @@ module es {
|
||||
*/
|
||||
public onAddedToScene(scene: Scene){}
|
||||
|
||||
protected beginRender(cam: Camera){
|
||||
|
||||
}
|
||||
/**
|
||||
* 当场景结束或渲染器从场景中移除时调用。使用这个进行清理。
|
||||
*/
|
||||
public unload(){ }
|
||||
|
||||
/**
|
||||
*
|
||||
* @param scene
|
||||
* @param cam
|
||||
*/
|
||||
public abstract render(scene: Scene);
|
||||
protected beginRender(cam: Camera){ }
|
||||
|
||||
public unload(){ }
|
||||
public abstract render(scene: Scene);
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -36,5 +46,18 @@ module es {
|
||||
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera){
|
||||
renderable.render(cam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当默认场景渲染目标被调整大小和当场景已经开始添加渲染器时调用
|
||||
* @param newWidth
|
||||
* @param newHeight
|
||||
*/
|
||||
public onSceneBackBufferSizeChanged(newWidth: number, newHeight: number){
|
||||
|
||||
}
|
||||
|
||||
public compareTo(other: Renderer): number{
|
||||
return this.renderOrder - other.renderOrder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,9 +70,10 @@ module es {
|
||||
/**
|
||||
* 获取矩形边界上与给定点最近的点
|
||||
* @param point
|
||||
* @param edgeNormal
|
||||
*/
|
||||
public getClosestPointOnRectangleBorderToPoint(point: Vector2): { res: Vector2, edgeNormal: Vector2 } {
|
||||
let edgeNormal = Vector2.zero;
|
||||
public getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2 {
|
||||
edgeNormal = Vector2.zero;
|
||||
|
||||
// 对于每个轴,如果点在盒子外面
|
||||
let res = new Vector2();
|
||||
@@ -107,7 +108,7 @@ module es {
|
||||
if (res.y == this.bottom) edgeNormal.y = 1;
|
||||
}
|
||||
|
||||
return { res: res, edgeNormal: edgeNormal };
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -197,7 +197,8 @@ module es {
|
||||
* @param matrix
|
||||
*/
|
||||
public static transform(position: Vector2, matrix: Matrix2D){
|
||||
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22));
|
||||
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31,
|
||||
(position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -221,5 +222,9 @@ module es {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public equals(other: Vector2){
|
||||
return other.x == this.x && other.y == this.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,7 @@ module es {
|
||||
for (let i = 0; i < colliders.length; i++) {
|
||||
let collider = colliders[i];
|
||||
|
||||
let boxcastResult = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
||||
collider.bounds = boxcastResult.rect;
|
||||
let neighbors = boxcastResult.colliders;
|
||||
let neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
|
||||
for (let j = 0; j < neighbors.length; j++) {
|
||||
let neighbor = neighbors[j];
|
||||
if (!collider.isTrigger && !neighbor.isTrigger)
|
||||
|
||||
@@ -17,15 +17,37 @@ module es {
|
||||
this._spatialHash.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取位于指定圆内的所有碰撞器
|
||||
* @param center
|
||||
* @param randius
|
||||
* @param results
|
||||
* @param layerMask
|
||||
*/
|
||||
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
|
||||
if (results.length == 0){
|
||||
console.error("An empty results array was passed in. No results will ever be returned.");
|
||||
return;
|
||||
}
|
||||
|
||||
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回所有碰撞器与边界相交的碰撞器。bounds。请注意,这是一个broadphase检查,所以它只检查边界,不做单个碰撞到碰撞器的检查!
|
||||
* @param rect
|
||||
* @param layerMask
|
||||
*/
|
||||
public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers){
|
||||
let boxcastResult = this._spatialHash.aabbBroadphase(rect, null, layerMask);
|
||||
return {colliders: boxcastResult.tempHashSet, rect: boxcastResult.bounds};
|
||||
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回所有与边界相交的碰撞器,不包括传入的碰撞器(self)。如果您希望为其他查询自行创建扫过的边界,则此方法非常有用
|
||||
* @param collider
|
||||
* @param rect
|
||||
* @param layerMask
|
||||
*/
|
||||
public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers){
|
||||
return this._spatialHash.aabbBroadphase(rect, collider, layerMask);
|
||||
}
|
||||
|
||||
@@ -66,15 +66,15 @@ module es {
|
||||
return super.overlaps(other);
|
||||
}
|
||||
|
||||
public collidesWithShape(other: Shape){
|
||||
public collidesWithShape(other: Shape, result: CollisionResult): boolean{
|
||||
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
||||
if (other instanceof Box && (other as Box).isUnrotated){
|
||||
return ShapeCollisions.boxToBox(this, other);
|
||||
return ShapeCollisions.boxToBox(this, other, result);
|
||||
}
|
||||
|
||||
// TODO: 让 minkowski 运行于 cricleToBox
|
||||
|
||||
return super.collidesWithShape(other);
|
||||
return super.collidesWithShape(other, result);
|
||||
}
|
||||
|
||||
public containsPoint(point: Vector2){
|
||||
|
||||
@@ -34,6 +34,7 @@ module es {
|
||||
}
|
||||
|
||||
public overlaps(other: Shape) {
|
||||
let result: CollisionResult = new CollisionResult();
|
||||
if (other instanceof Box && (other as Box).isUnrotated)
|
||||
return Collisions.isRectToCircle(other.bounds, this.position, this.radius);
|
||||
|
||||
@@ -41,29 +42,29 @@ module es {
|
||||
return Collisions.isCircleToCircle(this.position, this.radius, other.position, (other as Circle).radius);
|
||||
|
||||
if (other instanceof Polygon)
|
||||
return ShapeCollisions.circleToPolygon(this, other);
|
||||
return ShapeCollisions.circleToPolygon(this, other, result);
|
||||
|
||||
throw new Error(`overlaps of circle to ${other} are not supported`);
|
||||
}
|
||||
|
||||
public collidesWithShape(other: Shape): CollisionResult {
|
||||
public collidesWithShape(other: Shape, result: CollisionResult): boolean {
|
||||
if (other instanceof Box && (other as Box).isUnrotated) {
|
||||
return ShapeCollisions.circleToBox(this, other);
|
||||
return ShapeCollisions.circleToBox(this, other, result);
|
||||
}
|
||||
|
||||
if (other instanceof Circle) {
|
||||
return ShapeCollisions.circleToCircle(this, other);
|
||||
return ShapeCollisions.circleToCircle(this, other, result);
|
||||
}
|
||||
|
||||
if (other instanceof Polygon) {
|
||||
return ShapeCollisions.circleToPolygon(this, other);
|
||||
return ShapeCollisions.circleToPolygon(this, other, result);
|
||||
}
|
||||
|
||||
throw new Error(`Collisions of Circle to ${other} are not supported`);
|
||||
}
|
||||
|
||||
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
||||
return ShapeCollisions.pointToCircle(point, this);
|
||||
public pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean {
|
||||
return ShapeCollisions.pointToCircle(point, this, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,10 +141,12 @@ module es {
|
||||
* 点应该在多边形的空间中(点-多边形.位置)
|
||||
* @param points
|
||||
* @param point
|
||||
* @param distanceSquared
|
||||
* @param edgeNormal
|
||||
*/
|
||||
public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { closestPoint, distanceSquared, edgeNormal } {
|
||||
let distanceSquared = Number.MAX_VALUE;
|
||||
let edgeNormal = new Vector2(0, 0);
|
||||
public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2, distanceSquared: number, edgeNormal: Vector2): Vector2 {
|
||||
distanceSquared = Number.MAX_VALUE;
|
||||
edgeNormal = new Vector2(0, 0);
|
||||
let closestPoint = new Vector2(0, 0);
|
||||
|
||||
let tempDistanceSquared;
|
||||
@@ -166,9 +168,9 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
edgeNormal = Vector2.normalize(edgeNormal);
|
||||
Vector2Ext.normalize(edgeNormal);
|
||||
|
||||
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal };
|
||||
return closestPoint;
|
||||
}
|
||||
|
||||
public recalculateBounds(collider: Collider){
|
||||
@@ -221,13 +223,12 @@ module es {
|
||||
}
|
||||
|
||||
public overlaps(other: Shape){
|
||||
let result: CollisionResult;
|
||||
let result: CollisionResult = new CollisionResult();
|
||||
if (other instanceof Polygon)
|
||||
return ShapeCollisions.polygonToPolygon(this, other);
|
||||
return ShapeCollisions.polygonToPolygon(this, other, result);
|
||||
|
||||
if (other instanceof Circle){
|
||||
result = ShapeCollisions.circleToPolygon(other, this);
|
||||
if (result){
|
||||
if (ShapeCollisions.circleToPolygon(other, this, result)){
|
||||
result.invertResult();
|
||||
return true;
|
||||
}
|
||||
@@ -238,20 +239,18 @@ module es {
|
||||
throw new Error(`overlaps of Pologon to ${other} are not supported`);
|
||||
}
|
||||
|
||||
public collidesWithShape(other: Shape){
|
||||
let result = new CollisionResult();
|
||||
public collidesWithShape(other: Shape, result: CollisionResult): boolean{
|
||||
if (other instanceof Polygon){
|
||||
return ShapeCollisions.polygonToPolygon(this, other);
|
||||
return ShapeCollisions.polygonToPolygon(this, other, result);
|
||||
}
|
||||
|
||||
if (other instanceof Circle){
|
||||
result = ShapeCollisions.circleToPolygon(other, this);
|
||||
if (result){
|
||||
if (ShapeCollisions.circleToPolygon(other, this, result)){
|
||||
result.invertResult();
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
throw new Error(`overlaps of Polygon to ${other} are not supported`);
|
||||
@@ -278,8 +277,8 @@ module es {
|
||||
return isInside;
|
||||
}
|
||||
|
||||
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
||||
return ShapeCollisions.pointToPoly(point, this);
|
||||
public pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean {
|
||||
return ShapeCollisions.pointToPoly(point, this, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,9 @@ module es {
|
||||
public bounds: Rectangle;
|
||||
|
||||
public abstract recalculateBounds(collider: Collider);
|
||||
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||
public abstract overlaps(other: Shape);
|
||||
public abstract collidesWithShape(other: Shape): CollisionResult;
|
||||
public abstract overlaps(other: Shape): boolean;
|
||||
public abstract collidesWithShape(other: Shape, collisionResult: CollisionResult): boolean;
|
||||
public abstract pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
|
||||
|
||||
public clone(): Shape{
|
||||
return ObjectUtils.clone<Shape>(this);
|
||||
|
||||
@@ -4,9 +4,9 @@ module es {
|
||||
* 检查两个多边形之间的碰撞
|
||||
* @param first
|
||||
* @param second
|
||||
* @param result
|
||||
*/
|
||||
public static polygonToPolygon(first: Polygon, second: Polygon) {
|
||||
let result = new CollisionResult();
|
||||
public static polygonToPolygon(first: Polygon, second: Polygon, result: CollisionResult): boolean {
|
||||
let isIntersecting = true;
|
||||
|
||||
let firstEdges = first.edgeNormals;
|
||||
@@ -54,7 +54,7 @@ module es {
|
||||
|
||||
// 如果多边形不相交,也不会相交,退出循环
|
||||
if (!isIntersecting)
|
||||
return null;
|
||||
return false;
|
||||
|
||||
// 检查当前间隔距离是否为最小值。如果是,则存储间隔距离和当前距离。这将用于计算最小平移向量
|
||||
intervalDist = Math.abs(intervalDist);
|
||||
@@ -71,7 +71,7 @@ module es {
|
||||
result.normal = translationAxis;
|
||||
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis.x, -translationAxis.y), new Vector2(minIntervalDistance));
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,20 +115,17 @@ module es {
|
||||
*
|
||||
* @param circle
|
||||
* @param polygon
|
||||
* @param result
|
||||
*/
|
||||
public static circleToPolygon(circle: Circle, polygon: Polygon) {
|
||||
let result = new CollisionResult();
|
||||
|
||||
public static circleToPolygon(circle: Circle, polygon: Polygon, result: CollisionResult): boolean {
|
||||
let poly2Circle = Vector2.subtract(circle.position, polygon.position);
|
||||
|
||||
let gpp = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle);
|
||||
let closestPoint: Vector2 = gpp.closestPoint;
|
||||
let distanceSquared: number = gpp.distanceSquared;
|
||||
result.normal = gpp.edgeNormal;
|
||||
let distanceSquared = 0;
|
||||
let closestPoint = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle, distanceSquared, result.normal);
|
||||
|
||||
let circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
||||
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
||||
return null;
|
||||
return false;
|
||||
|
||||
let mtv: Vector2;
|
||||
if (circleCenterInsidePoly) {
|
||||
@@ -145,17 +142,17 @@ module es {
|
||||
result.minimumTranslationVector = mtv;
|
||||
result.point = Vector2.add(closestPoint, polygon.position);
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 适用于圆心在方框内以及只与方框外圆心重叠的圆。
|
||||
* @param circle
|
||||
* @param box
|
||||
* @param result
|
||||
*/
|
||||
public static circleToBox(circle: Circle, box: Box): CollisionResult {
|
||||
let result = new CollisionResult();
|
||||
let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
||||
public static circleToBox(circle: Circle, box: Box, result: CollisionResult): boolean {
|
||||
let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position, result.normal);
|
||||
|
||||
if (box.containsPoint(circle.position)) {
|
||||
result.point = closestPointOnBounds;
|
||||
@@ -163,7 +160,7 @@ module es {
|
||||
let safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius)));
|
||||
result.minimumTranslationVector = Vector2.subtract(circle.position, safePlace);
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
let sqrDistance = Vector2.distanceSquared(closestPointOnBounds, circle.position);
|
||||
@@ -172,23 +169,24 @@ module es {
|
||||
} else if (sqrDistance <= circle.radius * circle.radius) {
|
||||
result.normal = Vector2.subtract(circle.position, closestPointOnBounds);
|
||||
let depth = result.normal.length() - circle.radius;
|
||||
result.normal = Vector2Ext.normalize(result.normal);
|
||||
|
||||
result.point = closestPointOnBounds;
|
||||
Vector2Ext.normalize(result.normal);
|
||||
result.minimumTranslationVector = Vector2.multiply(new Vector2(depth), result.normal);
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param point
|
||||
* @param circle
|
||||
* @param result
|
||||
*/
|
||||
public static pointToCircle(point: Vector2, circle: Circle) {
|
||||
let result = new CollisionResult();
|
||||
|
||||
public static pointToCircle(point: Vector2, circle: Circle, result: CollisionResult): boolean {
|
||||
let distanceSquared = Vector2.distanceSquared(point, circle.position);
|
||||
let sumOfRadii = 1 + circle.radius;
|
||||
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||
@@ -198,10 +196,10 @@ module es {
|
||||
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth, -depth), result.normal);
|
||||
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -210,7 +208,7 @@ module es {
|
||||
* @param lineB
|
||||
* @param closestTo
|
||||
*/
|
||||
public static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2) {
|
||||
public static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2 {
|
||||
let v = Vector2.subtract(lineB, lineA);
|
||||
let w = Vector2.subtract(closestTo, lineA);
|
||||
let t = Vector2.dot(w, v) / Vector2.dot(v, v);
|
||||
@@ -223,24 +221,20 @@ module es {
|
||||
*
|
||||
* @param point
|
||||
* @param poly
|
||||
* @param result
|
||||
*/
|
||||
public static pointToPoly(point: Vector2, poly: Polygon) {
|
||||
let result = new CollisionResult();
|
||||
|
||||
public static pointToPoly(point: Vector2, poly: Polygon, result: CollisionResult): boolean {
|
||||
if (poly.containsPoint(point)) {
|
||||
let distanceSquared: number;
|
||||
let gpp = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position));
|
||||
let closestPoint = gpp.closestPoint;
|
||||
distanceSquared = gpp.distanceSquared;
|
||||
result.normal = gpp.edgeNormal;
|
||||
let distanceSquared: number = 0;
|
||||
let closestPoint = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position), distanceSquared, result.normal);
|
||||
|
||||
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared)));
|
||||
result.point = Vector2.add(closestPoint, poly.position);
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,9 +242,7 @@ module es {
|
||||
* @param first
|
||||
* @param second
|
||||
*/
|
||||
public static circleToCircle(first: Circle, second: Circle){
|
||||
let result = new CollisionResult();
|
||||
|
||||
public static circleToCircle(first: Circle, second: Circle, result: CollisionResult): boolean{
|
||||
let distanceSquared = Vector2.distanceSquared(first.position, second.position);
|
||||
let sumOfRadii = first.radius + second.radius;
|
||||
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||
@@ -260,35 +252,34 @@ module es {
|
||||
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth), result.normal);
|
||||
result.point = Vector2.add(second.position, Vector2.multiply(result.normal, new Vector2(second.radius)));
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param first
|
||||
* @param second
|
||||
* @param result
|
||||
*/
|
||||
public static boxToBox(first: Box, second: Box){
|
||||
let result = new CollisionResult();
|
||||
|
||||
public static boxToBox(first: Box, second: Box, result: CollisionResult): boolean{
|
||||
let minkowskiDiff = this.minkowskiDifference(first, second);
|
||||
if (minkowskiDiff.contains(0, 0)){
|
||||
// 计算MTV。如果它是零,我们就可以称它为非碰撞
|
||||
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
|
||||
|
||||
if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
|
||||
return null;
|
||||
if (result.minimumTranslationVector.equals(Vector2.zero))
|
||||
return false;
|
||||
|
||||
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
|
||||
result.normal.normalize();
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static minkowskiDifference(first: Box, second: Box){
|
||||
|
||||
@@ -144,7 +144,7 @@ module es {
|
||||
* @param excludeCollider
|
||||
* @param layerMask
|
||||
*/
|
||||
public aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number) {
|
||||
public aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number) : Collider[]{
|
||||
this._tempHashSet.length = 0;
|
||||
|
||||
let p1 = this.cellCoords(bounds.x, bounds.y);
|
||||
@@ -172,7 +172,7 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
return {tempHashSet: this._tempHashSet, bounds: bounds};
|
||||
return this._tempHashSet;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,16 +182,14 @@ module es {
|
||||
* @param results
|
||||
* @param layerMask
|
||||
*/
|
||||
public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask) {
|
||||
public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask): number {
|
||||
let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
|
||||
|
||||
this._overlapTestCircle.radius = radius;
|
||||
this._overlapTestCircle.position = circleCenter;
|
||||
|
||||
let resultCounter = 0;
|
||||
let aabbBroadphaseResult = this.aabbBroadphase(bounds, null, layerMask);
|
||||
bounds = aabbBroadphaseResult.bounds;
|
||||
let potentials = aabbBroadphaseResult.tempHashSet;
|
||||
let potentials = this.aabbBroadphase(bounds, null, layerMask);
|
||||
for (let i = 0; i < potentials.length; i++) {
|
||||
let collider = potentials[i];
|
||||
if (collider instanceof BoxCollider) {
|
||||
|
||||
Reference in New Issue
Block a user