【问题标题】:Collision response - Body temporarily disappears while penetrating another body碰撞反应 - 身体在穿透另一个身体时暂时消失
【发布时间】:2016-06-06 04:38:00
【问题描述】:

基于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;

请注意,dxdy 分别指的是物体速度的 x 和 y 分量,COR 指的是恢复系数。 (弹性)invAinvB 是反质量。

如何解决穿透体消失的问题?

【问题讨论】:

    标签: javascript html canvas game-physics


    【解决方案1】:

    啊,我想通了。我只是忘了用最小平移向量(MTV)来平移身体。

    它是通过将碰撞法线乘以重叠来计算的。 (AKA 穿透、深度等)

    【讨论】:

      猜你喜欢
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多