新增raycast射线
This commit is contained in:
57
source/src/Physics/RaycastHit.ts
Normal file
57
source/src/Physics/RaycastHit.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
module es {
|
||||
export class RaycastHit {
|
||||
/**
|
||||
* 对撞机被射线击中
|
||||
*/
|
||||
public collider: Collider;
|
||||
|
||||
/**
|
||||
* 撞击发生时沿射线的距离。
|
||||
*/
|
||||
public fraction: number = 0;
|
||||
|
||||
/**
|
||||
* 从射线原点到碰撞点的距离
|
||||
*/
|
||||
public distance: number = 0;
|
||||
|
||||
/**
|
||||
* 世界空间中光线击中对撞机表面的点
|
||||
*/
|
||||
public point: Vector2 = Vector2.zero;
|
||||
|
||||
/**
|
||||
* 被射线击中的表面的法向量
|
||||
*/
|
||||
public normal: Vector2 = Vector2.zero;
|
||||
|
||||
/**
|
||||
* 用于执行转换的质心。使其接触的形状的位置。
|
||||
*/
|
||||
public centroid: Vector2;
|
||||
|
||||
constructor(collider: Collider, fraction: number, distance: number, point: Vector2, normal: Vector2){
|
||||
this.collider = collider;
|
||||
this.fraction = fraction;
|
||||
this.distance = distance;
|
||||
this.point = point;
|
||||
this.centroid = Vector2.zero;
|
||||
}
|
||||
|
||||
public setValues(collider: Collider, fraction: number, distance: number, point: Vector2){
|
||||
this.collider = collider;
|
||||
this.fraction = fraction;
|
||||
this.distance = distance;
|
||||
this.point = point;
|
||||
}
|
||||
|
||||
public reset(){
|
||||
this.collider = null;
|
||||
this.fraction = this.distance = 0;
|
||||
}
|
||||
|
||||
public toString(){
|
||||
return `[RaycastHit] fraction: ${this.fraction}, distance: ${this.distance}, normal: ${this.normal}, centroid: ${this.centroid}, point: ${this.point}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ module es {
|
||||
public overlaps(other: Shape) {
|
||||
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
||||
if (this.isUnrotated) {
|
||||
if (other instanceof Box)
|
||||
if (other instanceof Box && other.isUnrotated)
|
||||
return this.bounds.intersects(other.bounds);
|
||||
|
||||
if (other instanceof Circle)
|
||||
@@ -83,5 +83,12 @@ module es {
|
||||
|
||||
return super.containsPoint(point);
|
||||
}
|
||||
|
||||
public pointCollidesWithShape(point: es.Vector2, result: es.CollisionResult): boolean {
|
||||
if (this.isUnrotated)
|
||||
return ShapeCollisions.pointToBox(point, this, result);
|
||||
|
||||
return super.pointCollidesWithShape(point, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,47 @@
|
||||
module es {
|
||||
export class CollisionResult {
|
||||
/**
|
||||
* 与之相撞的对撞机
|
||||
*/
|
||||
public collider: Collider;
|
||||
public minimumTranslationVector: Vector2 = Vector2.zero;
|
||||
/**
|
||||
* 被形状击中的表面的法向量
|
||||
*/
|
||||
public normal: Vector2 = Vector2.zero;
|
||||
/**
|
||||
* 应用于第一个形状以推入形状的转换
|
||||
*/
|
||||
public minimumTranslationVector: Vector2 = Vector2.zero;
|
||||
/**
|
||||
* 不是所有冲突类型都使用!在依赖这个字段之前,请检查ShapeCollisions切割类!
|
||||
*/
|
||||
public point: Vector2 = Vector2.zero;
|
||||
|
||||
/**
|
||||
* 改变最小平移向量,如果没有相同方向上的运动,它将移除平移的x分量。
|
||||
* @param deltaMovement
|
||||
*/
|
||||
public removeHorizontal(deltaMovement: Vector2){
|
||||
// 检查是否需要横向移动,如果需要,移除并固定响应
|
||||
if (Math.sign(this.normal.x) != Math.sign(deltaMovement.x) || (deltaMovement.x == 0 && this.normal.x != 0)){
|
||||
let responseDistance = this.minimumTranslationVector.length();
|
||||
let fix = responseDistance / this.normal.y;
|
||||
|
||||
// 检查一些边界情况。因为我们除以法线 使得x == 1和一个非常小的y这将导致一个巨大的固定值
|
||||
if (Math.abs(this.normal.x) != 1 && Math.abs(fix) < Math.abs(deltaMovement.y * 3)){
|
||||
this.minimumTranslationVector = new Vector2(0, -fix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public invertResult() {
|
||||
this.minimumTranslationVector = Vector2.negate(this.minimumTranslationVector);
|
||||
this.normal = Vector2.negate(this.normal);
|
||||
return this;
|
||||
}
|
||||
|
||||
public toString(){
|
||||
return `[CollisionResult] normal: ${this.normal}, minimumTranslationVector: ${this.minimumTranslationVector}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,85 +46,6 @@ module es {
|
||||
return this._edgeNormals;
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立一个对称的多边形(六边形,八角形,n角形)并返回点
|
||||
* @param vertCount
|
||||
* @param radius
|
||||
*/
|
||||
public static buildSymmetricalPolygon(vertCount: number, radius: number) {
|
||||
let verts = new Array(vertCount);
|
||||
|
||||
for (let i = 0; i < vertCount; i++) {
|
||||
let a = 2 * Math.PI * (i / vertCount);
|
||||
verts[i] = new Vector2(Math.cos(a), Math.sin(a) * radius);
|
||||
}
|
||||
|
||||
return verts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重定位多边形的点
|
||||
* @param points
|
||||
*/
|
||||
public static recenterPolygonVerts(points: Vector2[]) {
|
||||
let center = this.findPolygonCenter(points);
|
||||
for (let i = 0; i < points.length; i++)
|
||||
points[i] = Vector2.subtract(points[i], center);
|
||||
}
|
||||
|
||||
/**
|
||||
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
|
||||
* @param points
|
||||
*/
|
||||
public static findPolygonCenter(points: Vector2[]) {
|
||||
let x = 0, y = 0;
|
||||
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
x += points[i].x;
|
||||
y += points[i].y;
|
||||
}
|
||||
|
||||
return new Vector2(x / points.length, y / points.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 迭代多边形的所有边,并得到任意边上离点最近的点。
|
||||
* 通过最近点的平方距离和它所在的边的法线返回。
|
||||
* 点应该在多边形的空间中(点-多边形.位置)
|
||||
* @param points
|
||||
* @param point
|
||||
* @param distanceSquared
|
||||
* @param edgeNormal
|
||||
*/
|
||||
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;
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
let j = i + 1;
|
||||
if (j == points.length)
|
||||
j = 0;
|
||||
|
||||
let closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
|
||||
tempDistanceSquared = Vector2.distanceSquared(point, closest);
|
||||
|
||||
if (tempDistanceSquared < distanceSquared) {
|
||||
distanceSquared = tempDistanceSquared;
|
||||
closestPoint = closest;
|
||||
|
||||
// 求直线的法线
|
||||
let line = Vector2.subtract(points[j], points[i]);
|
||||
edgeNormal = new Vector2(-line.y, line.x);
|
||||
}
|
||||
}
|
||||
|
||||
edgeNormal = Vector2Ext.normalize(edgeNormal);
|
||||
|
||||
return closestPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置点并重新计算中心和边缘法线
|
||||
* @param points
|
||||
@@ -172,6 +93,121 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立一个对称的多边形(六边形,八角形,n角形)并返回点
|
||||
* @param vertCount
|
||||
* @param radius
|
||||
*/
|
||||
public static buildSymmetricalPolygon(vertCount: number, radius: number) {
|
||||
let verts = new Array(vertCount);
|
||||
|
||||
for (let i = 0; i < vertCount; i++) {
|
||||
let a = 2 * Math.PI * (i / vertCount);
|
||||
verts[i] = Vector2.multiply(new Vector2(Math.cos(a), Math.sin(a)), new Vector2(radius));
|
||||
}
|
||||
|
||||
return verts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重定位多边形的点
|
||||
* @param points
|
||||
*/
|
||||
public static recenterPolygonVerts(points: Vector2[]) {
|
||||
let center = this.findPolygonCenter(points);
|
||||
for (let i = 0; i < points.length; i++)
|
||||
points[i] = Vector2.subtract(points[i], center);
|
||||
}
|
||||
|
||||
/**
|
||||
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
|
||||
* @param points
|
||||
*/
|
||||
public static findPolygonCenter(points: Vector2[]) {
|
||||
let x = 0, y = 0;
|
||||
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
x += points[i].x;
|
||||
y += points[i].y;
|
||||
}
|
||||
|
||||
return new Vector2(x / points.length, y / points.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不知道辅助顶点,所以取每个顶点,如果你知道辅助顶点,执行climbing算法
|
||||
* @param points
|
||||
* @param direction
|
||||
*/
|
||||
public static getFarthestPointInDirection(points: Vector2[], direction: Vector2): Vector2{
|
||||
let index = 0;
|
||||
let maxDot = Vector2.dot(points[index], direction);
|
||||
|
||||
for (let i = 1; i < points.length; i ++){
|
||||
let dot = Vector2.dot(points[i], direction);
|
||||
if (dot > maxDot){
|
||||
maxDot = dot;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
return points[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* 迭代多边形的所有边,并得到任意边上离点最近的点。
|
||||
* 通过最近点的平方距离和它所在的边的法线返回。
|
||||
* 点应该在多边形的空间中(点-多边形.位置)
|
||||
* @param points
|
||||
* @param point
|
||||
* @param distanceSquared
|
||||
* @param edgeNormal
|
||||
*/
|
||||
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;
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
let j = i + 1;
|
||||
if (j == points.length)
|
||||
j = 0;
|
||||
|
||||
let closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
|
||||
tempDistanceSquared = Vector2.distanceSquared(point, closest);
|
||||
|
||||
if (tempDistanceSquared < distanceSquared) {
|
||||
distanceSquared = tempDistanceSquared;
|
||||
closestPoint = closest;
|
||||
|
||||
// 求直线的法线
|
||||
let line = Vector2.subtract(points[j], points[i]);
|
||||
edgeNormal = new Vector2(-line.y, line.x);
|
||||
}
|
||||
}
|
||||
|
||||
Vector2Ext.normalize(edgeNormal);
|
||||
|
||||
return closestPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* 旋转原始点并复制旋转的值到旋转的点
|
||||
* @param radians
|
||||
* @param originalPoints
|
||||
* @param rotatedPoints
|
||||
*/
|
||||
public static rotatePolygonVerts(radians: number, originalPoints: Vector2[], rotatedPoints){
|
||||
let cos = Math.cos(radians);
|
||||
let sin = Math.sign(radians);
|
||||
|
||||
for (let i = 0; i < originalPoints.length; i ++){
|
||||
let position = originalPoints[i];
|
||||
rotatedPoints[i] = new Vector2(position.x * cos + position.y * -sin, position.x * sin + position.y * cos);
|
||||
}
|
||||
}
|
||||
|
||||
public recalculateBounds(collider: Collider) {
|
||||
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
||||
this.center = collider.localOffset;
|
||||
|
||||
@@ -205,6 +205,18 @@ module es {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static pointToBox(point: Vector2, box: Box, result: CollisionResult){
|
||||
if (box.containsPoint(point)){
|
||||
// 在方框的空间里找到点
|
||||
result.point = box.bounds.getClosestPointOnRectangleBorderToPoint(point, result.normal);
|
||||
result.minimumTranslationVector = Vector2.subtract(point, result.point);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param lineA
|
||||
@@ -277,7 +289,7 @@ module es {
|
||||
return false;
|
||||
|
||||
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
|
||||
result.normal.normalize();
|
||||
result.normal = result.normal.normalize();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -285,7 +297,7 @@ module es {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static minkowskiDifference(first: Box, second: Box) {
|
||||
private static minkowskiDifference(first: Box, second: Box): Rectangle {
|
||||
// 我们需要第一个框的左上角
|
||||
// 碰撞器只会修改运动的位置所以我们需要用位置来计算出运动是什么。
|
||||
let positionOffset = Vector2.subtract(first.position, Vector2.add(first.bounds.location, Vector2.divide(first.bounds.size, new Vector2(2))));
|
||||
|
||||
Reference in New Issue
Block a user