新增扇形collider
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
module es {
|
||||
/**
|
||||
* 扇形碰撞器
|
||||
*/
|
||||
export class SectorCollider extends Collider {
|
||||
constructor(
|
||||
center: Vector2,
|
||||
radius: number,
|
||||
startAngle: number,
|
||||
endAngle: number
|
||||
) {
|
||||
super();
|
||||
this.shape = new Sector(center, radius, startAngle, endAngle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -337,6 +337,14 @@ module es {
|
||||
return (this.x * this.x) + (this.y * this.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从原点到向量末端的距离
|
||||
* @returns
|
||||
*/
|
||||
public getLength(): number {
|
||||
return Math.sqrt(this.x * this.x + this.y * this.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 四舍五入X和Y值
|
||||
*/
|
||||
@@ -355,6 +363,37 @@ module es {
|
||||
return Vector2Ext.angle(one, two);
|
||||
}
|
||||
|
||||
public getDistance(other: Vector2): number {
|
||||
return Math.sqrt(this.getDistanceSquared(other));
|
||||
}
|
||||
|
||||
public getDistanceSquared(other: Vector2): number {
|
||||
const dx = other.x - this.x;
|
||||
const dy = other.y - this.y;
|
||||
return dx * dx + dy * dy;
|
||||
}
|
||||
|
||||
public isBetween(v1: Vector2, v2: Vector2): boolean {
|
||||
const cross = v2.sub(v1).cross(this.sub(v1));
|
||||
return Math.abs(cross) < Number.EPSILON && this.dot(v2.sub(v1)) >= 0 && this.dot(v1.sub(v2)) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 两个向量的叉积
|
||||
* @param other
|
||||
* @returns
|
||||
*/
|
||||
public cross(other: Vector2): number {
|
||||
return this.x * other.y - this.y * other.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算向量与x轴之间的夹角
|
||||
*/
|
||||
public getAngle(): number {
|
||||
return Math.atan2(this.y, this.x);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较当前实例是否等于指定的对象
|
||||
* @param other 要比较的对象
|
||||
@@ -412,9 +451,19 @@ module es {
|
||||
return round ? Math.round(angle) : angle;
|
||||
}
|
||||
|
||||
public static fromAngle(angle: number, magnitude: number = 1): Vector2 {
|
||||
return new Vector2(magnitude * Math.cos(angle), magnitude * Math.sin(angle));
|
||||
}
|
||||
|
||||
|
||||
public clone(): Vector2 {
|
||||
return new Vector2(this.x, this.y);
|
||||
}
|
||||
|
||||
public copyFrom(source: Vector2): Vector2 {
|
||||
this.x = source.x;
|
||||
this.y = source.y;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,17 @@ module es {
|
||||
this._originalPoints[i] = this.points[i];
|
||||
}
|
||||
|
||||
public getEdges(): Array<Line> {
|
||||
const edges = [];
|
||||
|
||||
for (let i = 0; i < this.points.length; i++) {
|
||||
const j = (i + 1) % this.points.length;
|
||||
edges.push(new Line(this.points[i], this.points[j]));
|
||||
}
|
||||
|
||||
return edges;
|
||||
}
|
||||
|
||||
public overlaps(other: Shape) {
|
||||
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
||||
if (this.isUnrotated) {
|
||||
@@ -90,5 +101,33 @@ module es {
|
||||
|
||||
return super.pointCollidesWithShape(point, result);
|
||||
}
|
||||
|
||||
public getFurthestPoint(normal: Vector2): Vector2 {
|
||||
let furthestPoint = new Vector2(this.width / 2, this.height / 2);
|
||||
let dotProduct = furthestPoint.dot(normal);
|
||||
|
||||
let tempPoint = new Vector2(-this.width / 2, this.height / 2);
|
||||
let tempDotProduct = tempPoint.dot(normal);
|
||||
if (tempDotProduct > dotProduct) {
|
||||
furthestPoint.copyFrom(tempPoint);
|
||||
dotProduct = tempDotProduct;
|
||||
}
|
||||
|
||||
tempPoint.setTo(-this.width / 2, -this.height / 2);
|
||||
tempDotProduct = tempPoint.dot(normal);
|
||||
if (tempDotProduct > dotProduct) {
|
||||
furthestPoint.copyFrom(tempPoint);
|
||||
dotProduct = tempDotProduct;
|
||||
}
|
||||
|
||||
tempPoint.setTo(this.width / 2, -this.height / 2);
|
||||
tempDotProduct = tempPoint.dot(normal);
|
||||
if (tempDotProduct > dotProduct) {
|
||||
furthestPoint.copyFrom(tempPoint);
|
||||
dotProduct = tempDotProduct;
|
||||
}
|
||||
|
||||
return furthestPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
55
source/src/Physics/Shapes/Line.ts
Normal file
55
source/src/Physics/Shapes/Line.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
module es {
|
||||
export class Line {
|
||||
public start: Vector2;
|
||||
public end: Vector2;
|
||||
|
||||
constructor(start: Vector2, end: Vector2) {
|
||||
this.start = start.clone();
|
||||
this.end = end.clone();
|
||||
}
|
||||
|
||||
public get direction(): Vector2 {
|
||||
return this.end.sub(this.start).normalize();
|
||||
}
|
||||
|
||||
public getNormal(): Vector2 {
|
||||
const angle = this.direction.getAngle() - Math.PI / 2;
|
||||
return new Vector2(Math.cos(angle), Math.sin(angle));
|
||||
}
|
||||
|
||||
public getDirection(out: Vector2) {
|
||||
return out.copyFrom(this.end).sub(this.start).normalize();
|
||||
}
|
||||
|
||||
public getLength() {
|
||||
return this.start.getDistance(this.end);
|
||||
}
|
||||
|
||||
public getLengthSquared() {
|
||||
return this.start.getDistanceSquared(this.end);
|
||||
}
|
||||
|
||||
public distanceToPoint(normal: Vector2, center: Vector2): number {
|
||||
return Math.abs((this.end.y - this.start.y) * normal.x - (this.end.x - this.start.x) * normal.y + this.end.x * this.start.y - this.end.y * this.start.x) / (2 * normal.magnitude());
|
||||
}
|
||||
|
||||
public getFurthestPoint(direction: Vector2): Vector2 {
|
||||
const d1 = this.start.dot(direction);
|
||||
const d2 = this.end.dot(direction);
|
||||
return d1 > d2 ? this.start : this.end;
|
||||
}
|
||||
|
||||
public getClosestPoint(point: Vector2, out: Vector2) {
|
||||
const delta = out.copyFrom(this.end).sub(this.start);
|
||||
const t = (point.sub(this.start)).dot(delta) / delta.lengthSquared();
|
||||
|
||||
if (t < 0) {
|
||||
return out.copyFrom(this.start);
|
||||
} else if (t > 1) {
|
||||
return out.copyFrom(this.end);
|
||||
}
|
||||
|
||||
return out.copyFrom(delta).multiplyScaler(t).add(this.start);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
source/src/Physics/Shapes/Projection.ts
Normal file
41
source/src/Physics/Shapes/Projection.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
module es {
|
||||
/**
|
||||
* 计算投影和重叠区域
|
||||
*/
|
||||
export class Projection {
|
||||
public min: number;
|
||||
public max: number;
|
||||
|
||||
constructor() {
|
||||
this.min = Number.MAX_VALUE;
|
||||
this.max = -Number.MAX_VALUE;
|
||||
}
|
||||
|
||||
public project(axis: Vector2, polygon: Polygon) {
|
||||
const points = polygon.points;
|
||||
let min = axis.dot(points[0]);
|
||||
let max = min;
|
||||
|
||||
for (let i = 1; i < points.length; i++) {
|
||||
const p = points[i];
|
||||
const dot = axis.dot(p);
|
||||
if (dot < min) {
|
||||
min = dot;
|
||||
} else if (dot > max) {
|
||||
max = dot;
|
||||
}
|
||||
}
|
||||
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public overlap(other: Projection): boolean {
|
||||
return this.max >= other.min && other.max >= this.min;
|
||||
}
|
||||
|
||||
public getOverlap(other: Projection): number {
|
||||
return Math.min(this.max, other.max) - Math.max(this.min, other.min);
|
||||
}
|
||||
}
|
||||
}
|
||||
205
source/src/Physics/Shapes/Sector.ts
Normal file
205
source/src/Physics/Shapes/Sector.ts
Normal file
@@ -0,0 +1,205 @@
|
||||
module es {
|
||||
/**
|
||||
* 扇形形状
|
||||
*/
|
||||
export class Sector extends Shape {
|
||||
public center: Vector2;
|
||||
public radius: number;
|
||||
public startAngle: number;
|
||||
public endAngle: number;
|
||||
public angle: number;
|
||||
public radiusSquared: number;
|
||||
public numberOfPoints: number;
|
||||
public angleStep: number;
|
||||
public points: Vector2[];
|
||||
|
||||
public get sectorAngle(): number {
|
||||
let angle = this.endAngle - this.startAngle;
|
||||
if (angle < 0) angle += 360;
|
||||
return angle;
|
||||
}
|
||||
|
||||
constructor(center: Vector2, radius: number, startAngle: number, endAngle: number) {
|
||||
super();
|
||||
this.center = center;
|
||||
this.radius = radius;
|
||||
this.startAngle = startAngle;
|
||||
this.endAngle = endAngle;
|
||||
this.angle = endAngle - startAngle;
|
||||
this.radiusSquared = radius * radius;
|
||||
this.points = this.getPoints();
|
||||
this.calculateProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* 扇形的圆心和半径计算出扇形的重心
|
||||
* @returns
|
||||
*/
|
||||
public getCentroid(): Vector2 {
|
||||
const x = (Math.cos(this.startAngle) + Math.cos(this.endAngle)) * this.radius / 3;
|
||||
const y = (Math.sin(this.startAngle) + Math.sin(this.endAngle)) * this.radius / 3;
|
||||
return new Vector2(x + this.center.x, y + this.center.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算向量角度
|
||||
* @returns
|
||||
*/
|
||||
public getAngle(): number {
|
||||
return this.startAngle;
|
||||
}
|
||||
|
||||
public recalculateBounds(collider: Collider): void {
|
||||
const localCenter = this.center.add(collider.localOffset);
|
||||
const x = localCenter.x - this.radius;
|
||||
const y = localCenter.y - this.radius;
|
||||
const width = this.radius * 2;
|
||||
const height = this.radius * 2;
|
||||
const bounds = new Rectangle(x, y, width, height);
|
||||
this.bounds = bounds;
|
||||
|
||||
this.center = localCenter;
|
||||
}
|
||||
|
||||
public overlaps(other: Shape): boolean {
|
||||
let result = new Out<CollisionResult>();
|
||||
if (other instanceof Polygon)
|
||||
return ShapeCollisionSector.sectorToPolygon(this, other, result);
|
||||
|
||||
if (other instanceof Circle) {
|
||||
if (ShapeCollisionSector.sectorToCircle(this, other, result)) {
|
||||
result.value.invertResult();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
throw new Error(`overlaps of Sector to ${other} are not supported`);
|
||||
}
|
||||
|
||||
public collidesWithShape(other: Shape, collisionResult: Out<CollisionResult>): boolean {
|
||||
if (other instanceof Box) {
|
||||
return ShapeCollisionSector.sectorToBox(this, other, collisionResult);
|
||||
}
|
||||
|
||||
if (other instanceof Polygon) {
|
||||
return ShapeCollisionSector.sectorToPolygon(this, other, collisionResult);
|
||||
}
|
||||
|
||||
if (other instanceof Circle) {
|
||||
return ShapeCollisionSector.sectorToCircle(this, other, collisionResult);
|
||||
}
|
||||
|
||||
throw new Error(`overlaps of Polygon to ${other} are not supported`);
|
||||
}
|
||||
|
||||
public collidesWithLine(start: Vector2, end: Vector2, hit: Out<RaycastHit>): boolean {
|
||||
const toStart = start.sub(this.center);
|
||||
const toEnd = end.sub(this.center);
|
||||
const angleStart = toStart.getAngle();
|
||||
const angleEnd = toEnd.getAngle();
|
||||
let angleDiff = angleEnd - angleStart;
|
||||
|
||||
if (angleDiff > Math.PI) {
|
||||
angleDiff -= 2 * Math.PI;
|
||||
} else if (angleDiff < -Math.PI) {
|
||||
angleDiff += 2 * Math.PI;
|
||||
}
|
||||
|
||||
if (angleDiff >= this.startAngle && angleDiff <= this.endAngle) {
|
||||
const r = toStart.getLength();
|
||||
const t = this.startAngle - angleStart;
|
||||
const x = r * Math.cos(t);
|
||||
const y = r * Math.sin(t);
|
||||
const intersection = new Vector2(x, y);
|
||||
|
||||
if (intersection.isBetween(start, end)) {
|
||||
const distance = intersection.sub(start).getLength();
|
||||
const fraction = distance / start.getDistance(end);
|
||||
const normal = intersection.sub(this.center).normalize();
|
||||
const point = intersection.add(this.center);
|
||||
|
||||
const raycastHit = new RaycastHit();
|
||||
raycastHit.setValues(fraction, distance, point, normal);
|
||||
hit.value = raycastHit;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public containsPoint(point: Vector2) {
|
||||
const toPoint = point.sub(this.center);
|
||||
const distanceSquared = toPoint.lengthSquared();
|
||||
|
||||
if (distanceSquared > this.radiusSquared) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const angle = toPoint.getAngle();
|
||||
const startAngle = this.startAngle;
|
||||
const endAngle = startAngle + this.angle;
|
||||
|
||||
let angleDiff = angle - startAngle;
|
||||
if (angleDiff < 0) {
|
||||
angleDiff += Math.PI * 2;
|
||||
}
|
||||
if (angleDiff > this.angle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public pointCollidesWithShape(point: Vector2, result: Out<CollisionResult>): boolean {
|
||||
if (!this.containsPoint(point)) {
|
||||
if (result) {
|
||||
result.value = null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (result) {
|
||||
result.value = new CollisionResult();
|
||||
result.value.normal = point.sub(this.center).normalize();
|
||||
result.value.minimumTranslationVector = result.value.normal.scale(
|
||||
this.radius - point.sub(this.center).getLength()
|
||||
);
|
||||
result.value.point = point;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public getPoints(): Vector2[] {
|
||||
let points = new Array<Vector2>(this.numberOfPoints);
|
||||
for (let i = 0; i < this.numberOfPoints; i++) {
|
||||
let angle = this.startAngle + i * this.angleStep;
|
||||
points[i] = Vector2.fromAngle(angle, this.radius).add(this.center);
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
private calculateProperties() {
|
||||
this.numberOfPoints = Math.max(10, Math.floor(this.radius * 0.1));
|
||||
this.angleStep = (this.endAngle - this.startAngle) / (this.numberOfPoints - 1);
|
||||
}
|
||||
|
||||
public getFurthestPoint(normal: Vector2): Vector2 {
|
||||
let maxProjection = -Number.MAX_VALUE;
|
||||
let furthestPoint = new Vector2();
|
||||
for (let i = 0; i < this.numberOfPoints; i++) {
|
||||
let projection = this.points[i].dot(normal);
|
||||
if (projection > maxProjection) {
|
||||
maxProjection = projection;
|
||||
furthestPoint.copyFrom(this.points[i]);
|
||||
}
|
||||
}
|
||||
return furthestPoint;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
module es {
|
||||
export class ShapeCollisionSector {
|
||||
public static sectorToPolygon(first: Sector, second: Polygon, result: Out<CollisionResult>): boolean {
|
||||
const numPoints = second.points.length;
|
||||
let collision = false;
|
||||
const edgeStart = new Vector2();
|
||||
const edgeEnd = new Vector2();
|
||||
const hit = new Out<RaycastHit>();
|
||||
|
||||
for (let i = 0; i < numPoints; i++) {
|
||||
const point = second.points[i];
|
||||
if (first.containsPoint(point)) {
|
||||
if (result) {
|
||||
result.value = new CollisionResult();
|
||||
result.value.point = point.clone();
|
||||
result.value.normal = point.sub(first.center).normalize();
|
||||
}
|
||||
collision = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!collision && second.containsPoint(first.center)) {
|
||||
if (result) {
|
||||
result.value = new CollisionResult();
|
||||
result.value.point = first.center.clone();
|
||||
result.value.normal = new Vector2(0, 0);
|
||||
}
|
||||
collision = true;
|
||||
}
|
||||
|
||||
if (!collision) {
|
||||
for (let i = 0; i < numPoints; i++) {
|
||||
const p1 = second.points[i];
|
||||
const p2 = second.points[(i + 1) % numPoints];
|
||||
edgeStart.copyFrom(p1);
|
||||
edgeEnd.copyFrom(p2);
|
||||
|
||||
if (first.collidesWithLine(edgeStart, edgeEnd, hit)) {
|
||||
if (result) {
|
||||
result.value = new CollisionResult();
|
||||
result.value.point.copyFrom(hit.value.point);
|
||||
result.value.normal.copyFrom(hit.value.normal);
|
||||
}
|
||||
collision = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return collision;
|
||||
}
|
||||
|
||||
public static sectorToCircle(first: Sector, second: Circle, result: Out<CollisionResult>): boolean {
|
||||
const radiusSquared = second.radius * second.radius;
|
||||
const distanceSquared = first.center.getDistanceSquared(second.center);
|
||||
const angleDiff = Math.abs(second.center.sub(first.center).getAngle() - first.getAngle());
|
||||
const sectorAngle = first.endAngle - first.startAngle;
|
||||
|
||||
if (distanceSquared <= radiusSquared && angleDiff <= sectorAngle / 2) {
|
||||
if (result) {
|
||||
result.value = new CollisionResult();
|
||||
result.value.normal = second.center.sub(first.center).normalize();
|
||||
result.value.point = second.center.clone().add(result.value.normal.clone().multiplyScaler(second.radius));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (result) {
|
||||
result.value = null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static sectorToBox(first: Sector, second: Box, result: Out<CollisionResult>): boolean {
|
||||
result.value = new CollisionResult();
|
||||
|
||||
// 获取box的四条边
|
||||
let boxEdges = second.getEdges();
|
||||
|
||||
// 遍历box的每一条边
|
||||
for (let i = 0; i < boxEdges.length; i++) {
|
||||
let normal = boxEdges[i].getNormal();
|
||||
let furthestPointBox = second.getFurthestPoint(normal);
|
||||
let furthestPointSector = first.getFurthestPoint(normal.negate());
|
||||
|
||||
let distance = normal.dot(furthestPointSector.sub(furthestPointBox));
|
||||
|
||||
// 没有相交
|
||||
if (distance > 0)
|
||||
return false;
|
||||
|
||||
if (result.value && Math.abs(distance) < result.value.minimumTranslationVector.getLength()) {
|
||||
result.value.minimumTranslationVector = normal.clone().multiplyScaler(distance);
|
||||
result.value.normal = normal;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user