理解光线与椭球碰撞

huangapple go评论69阅读模式
英文:

Understanding ray against ellipsoid collision

问题

所以我已经找到了一个更好的解释关于Ray与椭球碰撞检测以及在碰撞点处的法线计算。我发现了这个有用的网站 链接 并且它有一个实现:

// 椭球以原点为中心,半径为ra
vec2 eliIntersect( in vec3 ro, in vec3 rd, in vec3 ra )
{
    vec3 ocn = ro/ra;
    vec3 rdn = rd/ra;
    float a = dot( rdn, rdn );
    float b = dot( ocn, rdn );
    float c = dot( ocn, ocn );
    float h = b*b - a*(c-1.0);
    if( h<0.0 ) return vec2(-1.0); //没有相交
    h = sqrt(h);
    return vec2(-b-h,-b+h)/a;
}

vec3 eliNormal( in vec3 pos, in vec3 ra )
{
    return normalize( pos/(ra*ra) );
}

我只想理解这是如何工作的,以及如果它不是以原点为中心,它会如何工作?

英文:

So I have looked around for a better explanation for Ray against an Ellipsoid collision detection as well as its normal (on the collision point) calculation. I found this useful website and it has an implementation:

// ellipsoid centered at the origin with radii ra
vec2 eliIntersect( in vec3 ro, in vec3 rd, in vec3 ra )
{
    vec3 ocn = ro/ra;
    vec3 rdn = rd/ra;
    float a = dot( rdn, rdn );
    float b = dot( ocn, rdn );
    float c = dot( ocn, ocn );
    float h = b*b - a*(c-1.0);
    if( h&lt;0.0 ) return vec2(-1.0); //no intersection
    h = sqrt(h);
    return vec2(-b-h,-b+h)/a;
}

vec3 eliNormal( in vec3 pos, in vec3 ra )
{
    return normalize( pos/(ra*ra) );
}

I just want to understand how this works and also how it would work if it's not centered at the origin?

答案1

得分: 0

一个椭球不仅有任意中心,还有任意轴方向,成对正交。我在讨论一般情况。

你可以通过从向量 ro 中减去中心坐标来处理椭球中心。

这样做后,如果需要,你可以通过用规范化的轴向量构造一个 3x3 矩阵来处理轴方向,并将逆矩阵应用于新的 rord。如果椭球是轴对齐的,矩阵是单位矩阵,你就不必担心。

现在,我们写出射线的向量方程,ro + t.rd(其中 t≥0 代表半射线),并将其代入椭球的隐式方程,

x&#178;/ra.x&#178; + y&#178;/ra.y&#178; + z&#178;/ra.z&#178; = 1。

这得到

(ro.x + t rd.x)&#178;/ra.x&#178; + (ro.y + t rd.y)&#178;/ra.y&#178; + (ro.y + t rd.y)&#178;/ra.z&#178; = 1。

展开后,我不会详细介绍,我们得到一个关于 t 的二次方程。如果判别式为负,没有实根,射线与椭球无交点。否则,我们得到两个交点,应该在射线的正面。

根据 t 和射线方程,你可以得到交点的坐标。别忘了应用矩阵并加上椭球中心,以恢复到原始坐标。

现在对于法向量,我们计算隐式方程的梯度,得到分量 x/ra.x&#178;y/ra.y&#178;z/ra.z&#178;。你可以从交点的 x, y, z 计算这些值。最后,如果需要,应用矩阵并归一化该向量。

英文:

An ellipsoid not only has an arbitrary center, but also arbitrary axis directions, pairwise orthogonal. I am discussing the general case.

You can deal with the ellipsoid center by subtracting the center coordinates from the vector ro.

When this is done, if necessary you can deal with the axis direction by forming a 3x3 matrix with the normalized axis vectors and appling the inverse matrix to the new ro and to rd. If the ellipsoid is axis-aligned, the matrix is unit and you don't have to care.

Now, we write the vector equation of the ray, ro + t.rd (with t≥0 for a half-ray) and plug it in the implicit equation of the ellipsoid,

x&#178;/ra.x&#178; + y&#178;/ra.y&#178; + z&#178;/ra.z&#178; = 1.

This gives

(ro.x + t rd.x)&#178;/ra.x&#178; + (ro.y + t rd.y)&#178;/ra.y&#178; + (ro.y + t rd.y)&#178;/ra.z&#178; = 1.

After expansion, which I will not detail, we get a quadratic equation in t. If the discriminant is negative, there are no real roots, so the ray misses the ellipsoid. Otherwise we get two intersection points, which should be on the positive side of the ray.

From t and the ray equation, you obtain the coordinates of an intersection. Don't forget to apply the matrix and add the ellipsoid center to return to the original coordinates.

Now for the normal vector, we compute the gradient of the implicit equation, giving the components x/ra.x&#178;, y/ra.y&#178;, z/ra.z&#178;. You can compute these from the x, y, z of the intersection. Finally, apply the matrix if necessary and normalize the vector.

huangapple
  • 本文由 发表于 2023年6月13日 03:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定