基于an answer to a previous question of mine about angular velocity,我已经修改了给定的演示,并实现了分离轴定理(碰撞检测)以及基本的线性脉冲解决方案。(here's the JSFiddle)。然而,在响应中有一个小问题。
如果两个物体设法互相穿透(偶尔会发生),穿透的物体暂时消失,然后当它们不再穿透时再次出现。但是为什么呢?
let aVel = [a.dx, a.dy];
let bVel = [b.dx, b.dy];
const invA = a.static ? 0 : 1 / a.mass;
const invB = b.static ? 0 : 1 / b.mass;
const relativeVel = Sub(bVel, aVel);
const velAlongNorm = DotProduct(relativeVel, data.unit);
if (velAlongNorm > 0)
return;
const cor = a.cor * b.cor;
let _j = -(1 + cor) * velAlongNorm;
_j /= invA + invB;
const impulse = ScalarMultiply(data.unit, _j);
aVel = Sub(aVel, ScalarMultiply(impulse, invA));
bVel = Add(bVel, ScalarMultiply(impulse, invB));
a.dx = aVel[0];
a.dy = aVel[1];
b.dx = bVel[0];
b.dy = bVel[1];
const percent = 0.2;
const slop = 0.01;
const correction = Math.max(data.overlap - slop, 0) / (invA + invB) * percent;
a.x -= invA * correction;
a.y -= invA * correction;
b.x += invB * correction;
b.y += invB * correction;请注意,dx和dy分别是指物体速度的x和y分量,COR是指恢复系数。(弹力) invA和invB是相反的质量。
如何解决穿透体消失的问题?
发布于 2016-06-06 19:05:54
啊,我想通了。我只是忘记了用最小平移向量(MTV)来平移by。
它的计算方法是将碰撞法线乘以重叠。(又称渗透率、深度等)
https://stackoverflow.com/questions/37649621
复制相似问题