![](/img/trans.png)
[英]JavaScript - Separating Axis Theorem - Collision working, but not Response?
[英]Collision detection: Separating Axis Theorem - Circle versus Polygon
我一直在嘗試根據Randy Gaul的C ++ Impulse Engine ,在圓和多邊形之間實現碰撞檢測,並嚴格遵循代碼,但是算法永遠不會返回true。
這是JSFiddle 。 (為方便起見,使用HTML5 Canvas API渲染了主體)
代碼片段(僅沖突檢測):
const circPoly = (a, b) => {
let data = {},
center = a.pos;
data.contacts = [];
center = b.mat.clone().trans().mult(center.clone().sub(b.pos));
let sep = -Number.MAX_VALUE,
faceNorm = 0;
for (let i = 0; i < b.verts2.length; ++i) {
let sep2 = b.norms[i].dot(center.clone().sub(b.verts2[i]));
if (sep2 > a.radius) return data;
if (sep2 > sep) { sep = sep2; faceNorm = i; }
}
let v1 = b.verts2[faceNorm],
v2 = b.verts2[faceNorm + 1 < b.verts2.length ? faceNorm + 1 : 0];
if (sep < 0.0001) {
data.depth = a.radius;
data.norm = b.mat.clone().mult(b.norms[faceNorm]).neg();
data.contacts[0] = data.norm.clone().vmult(a.pos.clone().sadd(a.radius));
return data;
}
let dot1 = center.clone().sub(v1).dot(v2.clone().sub(v1)),
dot2 = center.clone().sub(v2).dot(v1.clone().sub(v2));
data.depth = a.radius - sep;
if (dot1 <= 0) {
if (center.dist2(v1) > a.radius * a.radius) return data;
let norm = v1.clone().sub(center);
norm = b.mat.clone().mult(norm);
norm.norm();
data.norm = norm;
v1 = b.mat.clone().mult(v1.clone().add(b.pos));
data.contacts[0] = v1;
} else if (dot2 <= 0) {
if (center.dist2(v2) > a.radius * a.radius) return data;
let norm = v2.clone().sub(center);
norm = b.mat.clone().mult(norm);
norm.norm();
data.norm = norm;
v2 = b.mat.clone().mult(v2.clone().add(b.pos));
data.contacts[0] = v2;
} else {
let norm = b.norms[faceNorm];
if (center.clone().sub(v1).dot(norm) > a.radius) return data;
norm = b.mat.clone().mult(norm);
data.norm = norm.clone().neg();
data.contacts[0] = data.norm.clone().vmult(a.pos.clone().sadd(a.radius));
}
return data;
};
請注意, b.verts2
指的是現實世界坐標中多邊形的頂點。
我知道,Vector類沒有問題,但是由於我對轉換矩陣的經驗不多,所以該類可能是這些錯誤的根源,盡管其代碼幾乎完全源自以及脈沖引擎,因此它應該可以工作。 如前所述,即使確實發生沖突,該算法也始終返回false。 我在這里做錯了什么? 我嘗試取出早期收益,但是那只會返回奇怪的結果,例如帶有負坐標的接觸點,這顯然不太正確。
編輯:修改了矢量類的垂直函數,使其以與Impulse Engine相同的方式工作(兩種方法都是正確的,但我認為一個是順時針方向,另一個是逆時針方向-我也修改了我的頂點以反映逆時針方向)。 不幸的是,它仍然無法通過測試。
好吧,有很多問題,我真的不明白您要做什么,因為它看起來過於復雜。 例如為什么矩陣有trans
??? 為什么將Y up屏幕用作變換的坐標系??? (修辭)
在第一個循環中。
vec.dot
函數找到距離,該函數返回距離的平方。 但是您要測試半徑,應該測試if(sep2 < radius * radius)
data.contacts
您將返回數據對象,但忘記將在圓內找到的data.contacts
放在data.contacts
數組上。 您需要做的就是
檢查多邊形上的任何頂點是否比半徑更近,如果是,則您有截距(或完全位於內部)
然后您需要檢查每個線段的距離
如果不需要截線,則可以對每個線段進行以下操作(如果需要截線,則可以使用以下方法):僅使用一個或另一個。
// circle is a point {x:?,y:?}
// radius = is the you know what
// p1,p2 are the start and end points of a line
checkLineCircle = function(circle,radius,p1,p2){
var v1 = {};
var v2 = {};
var v3 = {};
var u;
// get dist to end of line
v2.x = circle.x - p1.x;
v2.y = circle.y - p1.y;
// check if end points are inside the circle
if( Math.min(
Math.hypot(p2.x - circle.x, p2.y - circle.y),
Math.hypot(v2.x, v2.y)
) <= radius){
return true;
}
// get the line as a vector
v1.x = p2.x - p1.x;
v1.y = p2.y - p1.y;
// get the unit distance of the closest point on the line
u = (v2.x * v1.x + v2.y * v1.y)/(v1.y * v1.y + v1.x * v1.x);
// is this on the line segment
if(u >= 0 && u <= 1){
v3.x = v1.x * u; // get the point on the line segment
v3.y = v1.y * u;
// get the distance to that point and return true or false depending on the
// it being inside the circle
return (Math.hypot(v3.y - v2.y, v3.x - v2.x) <= radius);
}
return false; // no intercept
}
為節省時間,可將圓心變換到多邊形局部,而不是變換多邊形上的每個點。
如果需要截取點,請使用以下功能
// p1,p2 are the start and end points of a line
// returns an array empty if no points found or one or two points depending on the number of intercepts found
// If two points found the first point in the array is the point closest to the line start (p1)
function circleLineIntercept(circle,radius,p1,p2){
var v1 = {};
var v2 = {};
var ret = [];
var u1,u2,b,c,d;
// line as vector
v1.x = p2.x - p1.x;
v1.y = p2.y - p1.y;
// vector to circle center
v2.x = p1.x - circle.x;
v2.y = p1.y - circle.y;
// dot of line and circle
b = (v1.x * v2.x + v1.y * v2.y) * -2;
// length of line squared * 2
c = 2 * (v1.x * v1.x + v1.y * v1.y);
// some math to solve the two triangles made by the intercept points, the circle center and the perpendicular line to the line.
d = Math.sqrt(b * b - 2 * c * (v2.x * v2.x + v2.y * v2.y - radius * radius));
// will give a NaN if no solution
if(isNaN(d)){ // no intercept
return ret;
}
// get the unit distance of each intercept to the line
u1 = (b - d) / c;
u2 = (b + d) / c;
// check the intercept is on the line segment
if(u1 <= 1 && u1 >= 0){
ret.push({x:line.p1.x + v1.x * u1, y : line.p1.y + v1.y * u1 });
}
// check the intercept is on the line segment
if(u2 <= 1 && u2 >= 0){
ret.push({x:line.p1.x + v1.x * u2, y : line.p1.y + v1.y * u2});
}
return ret;
}
我將由您決定進行多邊形迭代。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.