修复rectangle中rayinterest方法返回错误信息

This commit is contained in:
yhh
2023-03-13 15:23:02 +08:00
parent 72fdabd099
commit 78079252c9
5 changed files with 39 additions and 34 deletions

View File

@@ -544,7 +544,7 @@ declare module es {
*/ */
static negate(value: Vector2): Vector2; static negate(value: Vector2): Vector2;
/** /**
* 创建一个新的Vector2其中包含给定矢量和法线的反射矢量 * 向量的反射输入为两个二维向量vector和normal。函数返回一个新的向量即vector相对于normal的反射
* @param vector * @param vector
* @param normal * @param normal
* @returns * @returns

View File

@@ -1339,14 +1339,16 @@ var es;
return value; return value;
}; };
/** /**
* 创建一个新的Vector2其中包含给定矢量和法线的反射矢量 * 向量的反射输入为两个二维向量vector和normal。函数返回一个新的向量即vector相对于normal的反射
* @param vector * @param vector
* @param normal * @param normal
* @returns * @returns
*/ */
Vector2.reflect = function (vector, normal) { Vector2.reflect = function (vector, normal) {
var result = es.Vector2.zero; var result = es.Vector2.zero;
// 计算向量与法线的点积并将结果乘2
var val = 2 * ((vector.x * normal.x) + (vector.y * normal.y)); var val = 2 * ((vector.x * normal.x) + (vector.y * normal.y));
// 计算反射向量
result.x = vector.x - (normal.x * val); result.x = vector.x - (normal.x * val);
result.y = vector.y - (normal.y * val); result.y = vector.y - (normal.y * val);
return result; return result;
@@ -7784,44 +7786,45 @@ var es;
this.top < value.bottom; this.top < value.bottom;
}; };
Rectangle.prototype.rayIntersects = function (ray) { Rectangle.prototype.rayIntersects = function (ray) {
var _a, _b;
var res = { intersected: false, distance: 0 }; var res = { intersected: false, distance: 0 };
var maxValue = Number.MAX_VALUE; var maxValue = Infinity;
if (Math.abs(ray.direction.x) < 1E-06) { if (Math.abs(ray.direction.x) < 1E-06) {
if ((ray.start.x < this.x) || (ray.start.x > this.x + this.width)) if (ray.start.x < this.x || ray.start.x > this.x + this.width) {
return res; return res;
} }
}
else { else {
var num11 = 1 / ray.direction.x; var num11 = 1 / ray.direction.x;
var num8 = (this.x - ray.start.x) * num11; var num8 = (this.x - ray.start.x) * num11;
var num7 = (this.x + this.width - ray.start.x) * num11; var num7 = (this.x + this.width - ray.start.x) * num11;
if (num8 > num7) { if (num8 > num7) {
var num14 = num8; _a = __read([num8, num7], 2), num7 = _a[0], num8 = _a[1];
num8 = num7;
num7 = num14;
} }
res.distance = Math.max(num8, res.distance); res.distance = Math.max(num8, res.distance);
maxValue = Math.min(num7, maxValue); maxValue = Math.min(num7, maxValue);
if (res.distance > maxValue) if (res.distance > maxValue) {
return res; return res;
} }
}
if (Math.abs(ray.direction.y) < 1e-06) { if (Math.abs(ray.direction.y) < 1e-06) {
if ((ray.start.y < this.y) || (ray.start.y > this.y + this.height)) if (ray.start.y < this.y || ray.start.y > this.y + this.height) {
return res; return res;
} }
}
else { else {
var num10 = 1 / ray.direction.y; var num10 = 1 / ray.direction.y;
var num6 = (this.y - ray.start.y) * num10; var num6 = (this.y - ray.start.y) * num10;
var num5 = (this.y + this.height - ray.start.y) * num10; var num5 = (this.y + this.height - ray.start.y) * num10;
if (num6 > num5) { if (num6 > num5) {
var num13 = num6; _b = __read([num6, num5], 2), num5 = _b[0], num6 = _b[1];
num6 = num5;
num5 = num13;
} }
res.distance = Math.max(num6, res.distance); res.distance = Math.max(num6, res.distance);
maxValue = Math.max(num5, maxValue); maxValue = Math.min(num5, maxValue);
if (res.distance > maxValue) if (res.distance > maxValue) {
return res; return res;
} }
}
res.intersected = true; res.intersected = true;
return res; return res;
}; };

File diff suppressed because one or more lines are too long

View File

@@ -215,45 +215,45 @@ module es {
public rayIntersects(ray: Ray2D): { intersected: boolean; distance: number } { public rayIntersects(ray: Ray2D): { intersected: boolean; distance: number } {
const res = { intersected: false, distance: 0 }; const res = { intersected: false, distance: 0 };
let maxValue = Number.MAX_VALUE; let maxValue = Infinity;
if (Math.abs(ray.direction.x) < 1E-06) { if (Math.abs(ray.direction.x) < 1E-06) {
if ((ray.start.x < this.x) || (ray.start.x > this.x + this.width)) if (ray.start.x < this.x || ray.start.x > this.x + this.width) {
return res; return res;
}
} else { } else {
const num11 = 1 / ray.direction.x; const num11 = 1 / ray.direction.x;
let num8 = (this.x - ray.start.x) * num11; let num8 = (this.x - ray.start.x) * num11;
let num7 = (this.x + this.width - ray.start.x) * num11; let num7 = (this.x + this.width - ray.start.x) * num11;
if (num8 > num7) { if (num8 > num7) {
const num14 = num8; [num7, num8] = [num8, num7];
num8 = num7;
num7 = num14;
} }
res.distance = Math.max(num8, res.distance); res.distance = Math.max(num8, res.distance);
maxValue = Math.min(num7, maxValue); maxValue = Math.min(num7, maxValue);
if (res.distance > maxValue) if (res.distance > maxValue) {
return res; return res;
} }
}
if (Math.abs(ray.direction.y) < 1e-06) { if (Math.abs(ray.direction.y) < 1e-06) {
if ((ray.start.y < this.y) || (ray.start.y > this.y + this.height)) if (ray.start.y < this.y || ray.start.y > this.y + this.height) {
return res; return res;
}
} else { } else {
const num10 = 1 / ray.direction.y; const num10 = 1 / ray.direction.y;
let num6 = (this.y - ray.start.y) * num10; let num6 = (this.y - ray.start.y) * num10;
let num5 = (this.y + this.height - ray.start.y) * num10; let num5 = (this.y + this.height - ray.start.y) * num10;
if (num6 > num5) { if (num6 > num5) {
const num13 = num6; [num5, num6] = [num6, num5];
num6 = num5;
num5 = num13;
} }
res.distance = Math.max(num6, res.distance); res.distance = Math.max(num6, res.distance);
maxValue = Math.max(num5, maxValue); maxValue = Math.min(num5, maxValue);
if (res.distance > maxValue) if (res.distance > maxValue) {
return res; return res;
} }
}
res.intersected = true; res.intersected = true;
return res; return res;

View File

@@ -174,14 +174,16 @@ module es {
} }
/** /**
* 创建一个新的Vector2其中包含给定矢量和法线的反射矢量 * 向量的反射输入为两个二维向量vector和normal。函数返回一个新的向量即vector相对于normal的反射
* @param vector * @param vector
* @param normal * @param normal
* @returns * @returns
*/ */
public static reflect(vector: Vector2, normal: Vector2) { public static reflect(vector: Vector2, normal: Vector2) {
let result: Vector2 = es.Vector2.zero; let result: Vector2 = es.Vector2.zero;
// 计算向量与法线的点积并将结果乘2
let val = 2 * ((vector.x * normal.x) + (vector.y * normal.y)); let val = 2 * ((vector.x * normal.x) + (vector.y * normal.y));
// 计算反射向量
result.x = vector.x - (normal.x * val); result.x = vector.x - (normal.x * val);
result.y = vector.y - (normal.y * val); result.y = vector.y - (normal.y * val);
return result; return result;