修复polygon数组错误 修复emit空注册报错

This commit is contained in:
yhh
2020-06-15 20:08:21 +08:00
parent c3120d791f
commit 5186bc0187
16 changed files with 398 additions and 32 deletions

View File

@@ -1,6 +1,5 @@
///<reference path="./Collider.ts" />
class BoxCollider extends Collider {
public get width(){
return (this.shape as Box).width;
}
@@ -21,4 +20,30 @@ class BoxCollider extends Collider {
return this;
}
public get height(){
return (this.shape as Box).height;
}
public set height(value: number){
this.setHeight(value);
}
public setHeight(height: number){
this._colliderRequiresAutoSizing = false;
let box = this.shape as Box;
if (height != box.height){
box.updateBox(box.width, height);
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
}
constructor(){
super();
this.shape = new Box(1, 1);
this._colliderRequiresAutoSizing = true;
}
}

View File

@@ -6,12 +6,51 @@ abstract class Collider extends Component{
protected _isParentEntityAddedToScene;
protected _isPositionDirty = true;
protected _isRotationDirty = true;
protected _colliderRequiresAutoSizing;
protected _localOffset: Vector2;
protected _isColliderRegisterd;
public get bounds(): Rectangle {
if (this._isPositionDirty || this._isRotationDirty){
this.shape.recalculateBounds(this);
this._isPositionDirty = this._isRotationDirty = false;
}
return this.shape.bounds;
}
public get localOffset(){
return this._localOffset;
}
public set localOffset(value: Vector2){
this.setLocalOffset(value);
}
public setLocalOffset(offset: Vector2){
if (this._localOffset != offset){
this.unregisterColliderWithPhysicsSystem();
this._localOffset = offset;
this._isPositionDirty = true;
this.registerColliderWithPhysicsSystem();
}
}
public registerColliderWithPhysicsSystem(){
if (this._isParentEntityAddedToScene && !this._isColliderRegisterd){
Physics.addCollider(this);
this._isColliderRegisterd = true;
}
}
public unregisterColliderWithPhysicsSystem(){
if (this._isParentEntityAddedToScene && this._isColliderRegisterd){
Physics.removeCollider(this);
}
this._isColliderRegisterd = false;
}
public initialize() {
}
}

View File

@@ -11,6 +11,14 @@ class Physics {
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
}
public static addCollider(collider: Collider){
Physics._spatialHash.register(collider);
}
public static removeCollider(collider: Collider){
Physics._spatialHash.remove(collider);
}
public static updateCollider(collider: Collider){
this._spatialHash.remove(collider);
this._spatialHash.register(collider);

View File

@@ -18,6 +18,18 @@ class Circle extends Shape {
return ShapeCollisions.circleToRect(this, other as Box);
}
if (other instanceof Circle){
// TODO CIRCLETOCIRCLE
}
if (other instanceof Polygon){
return ShapeCollisions.circleToPolygon(this, other);
}
throw new Error(`Collisions of Circle to ${other} are not supported`);
}
public recalculateBounds(collider: Collider) {
this.center = collider.localOffset;
}
}

View File

@@ -22,7 +22,7 @@ class Polygon extends Shape {
private buildEdgeNormals(){
let totalEdges = this.isBox ? 2 : this.points.length;
if (this._edgeNormals == null || this._edgeNormals.length != totalEdges)
this._edgeNormals = new Vector2[totalEdges];
this._edgeNormals = new Array(totalEdges);
let p2: Vector2;
for (let i = 0; i < totalEdges; i ++){
@@ -42,7 +42,7 @@ class Polygon extends Shape {
this.points = points;
this.recalculateCenterAndEdgeNormals();
this._originalPoints = new Vector2[points.length];
this._originalPoints = new Array(points.length);
this._originalPoints = points;
}
@@ -116,7 +116,7 @@ class Polygon extends Shape {
}
public static buildSymmertricalPolygon(vertCount: number, radius: number) {
let verts = new Vector2[vertCount];
let verts = new Array(vertCount);
for (let i = 0; i < vertCount; i++) {
let a = 2 * Math.PI * (i / vertCount);
@@ -125,4 +125,8 @@ class Polygon extends Shape {
return verts;
}
public recalculateBounds(collider: Collider) {
this.center = collider.localOffset;
}
}

View File

@@ -1,6 +1,8 @@
abstract class Shape {
public bounds: Rectangle;
public position: Vector2;
public center: Vector2;
public abstract recalculateBounds(collider: Collider);
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
}

View File

@@ -10,6 +10,35 @@ class ShapeCollisions {
let polygonOffset = Vector2.subtract(first.position, second.position);
let axis: Vector2;
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex ++){
if (edgeIndex < firstEdges.length){
axis = firstEdges[edgeIndex];
} else {
axis = secondEdges[edgeIndex - firstEdges.length];
}
let minA = 0;
let minB = 0;
let maxA = 0;
let maxB = 0;
let intervalDist = 0;
this.getInterval(axis, first, minA, maxA);
this.getInterval(axis, second, minB, maxB);
}
}
public static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number){
let dot = Vector2.dot(polygon.points[0], axis);
min = max = dot;
for (let i = 1; i < polygon.points.length; i++){
dot = Vector2.dot(polygon.points[i], axis);
if (dot < min){
min = dot;
}else if(dot > max){
max = dot;
}
}
}
public static circleToPolygon(circle: Circle, polygon: Polygon){

View File

@@ -23,7 +23,9 @@ class Emitter<T> {
public emit(eventType: T, data: any){
let list: Function[] = this._messageTable.get(eventType);
for (let i = list.length - 1; i >= 0; i --)
list[i](data);
if (list){
for (let i = list.length - 1; i >= 0; i --)
list[i](data);
}
}
}

View File

@@ -1,12 +1,13 @@
class WebGLUtils {
public static getWebGL(): WebGLRenderingContext{
public static getWebGL(): WebGLRenderingContext {
if (egret.WebGLUtils.checkCanUseWebGL())
return document.querySelector("canvas").getContext("webgl");
throw new Error("cannot get webgl");
}
public static drawUserIndexPrimitives<T>(primitiveType: number, vertexData: T[], vertexOffset: number, numVertices: number, indexData: number[], indexOffset: number, primitiveCount: number){
public static drawUserIndexPrimitives<T>(primitiveType: number, vertexData: T[], vertexOffset: number,
numVertices: number, indexData: number[], indexOffset: number, primitiveCount: number) {
let GL = this.getWebGL();
GL.bindBuffer(GL.ARRAY_BUFFER, 0);
@@ -14,16 +15,16 @@ class WebGLUtils {
GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, 0);
this.checkGLError();
GL.drawElements(primitiveType,
this.getElementCountArray(primitiveType, primitiveCount),
GL.UNSIGNED_SHORT,
GL.drawElements(primitiveType,
this.getElementCountArray(primitiveType, primitiveCount),
GL.UNSIGNED_SHORT,
indexOffset * 2);
this.checkGLError();
}
private static getElementCountArray(primitiveType: number, primitiveCount: number){
private static getElementCountArray(primitiveType: number, primitiveCount: number) {
let GL = this.getWebGL();
switch (primitiveType){
switch (primitiveType) {
case GL.LINES:
return primitiveCount * 2;
case GL.LINE_STRIP:
@@ -37,10 +38,10 @@ class WebGLUtils {
throw new Error("not support");
}
public static checkGLError(){
public static checkGLError() {
let GL = this.getWebGL();
let error = GL.getError();
if (error != GL.NO_ERROR){
if (error != GL.NO_ERROR) {
throw new Error("GL.GetError() returned" + error);
}
}