3D线段和平面相交 [英] 3D Line Segment and Plane Intersection

查看:112
本文介绍了3D线段和平面相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现线段和平面相交测试,该测试将根据其是否与平面相交而返回true或false.它还会返回直线相交的平面上的接触点,如果直线不相交,则如果直线是射线,函数仍应返回相交点.我使用了克里斯特·埃里克森(Christer Ericson)的实时碰撞检测中的信息和代码,但我认为无法正确实现它.

I'm trying to implement a line segment and plane intersection test that will return true or false depending on whether or not it intersects the plane. It also will return the contact point on the plane where the line intersects, if the line does not intersect, the function should still return the intersection point had the line segmenent had been a ray. I used the information and code from Christer Ericson's Real-time Collision Detection but I don't think im implementing it correctly.

im使用的平面是从三角形的法线和顶点派生的.我想要在平面上找到相交的位置,而不管它是否位于我用来导出平面的三角形上.

The plane im using is derived from the normal and vertice of a triangle. Finding the location of intersection on the plane is what i want, regardless of whether or not it is located on the triangle i used to derive the plane.

该函数的参数如下:

contact = the contact point on the plane, this is what i want calculated
ray = B - A, simply the line from A to B
rayOrigin = A, the origin of the line segement
normal = normal of the plane (normal of a triangle)
coord = a point on the plane (vertice of a triangle)

这是我使用的代码:

bool linePlaneIntersection(Vector& contact, Vector ray, Vector rayOrigin, Vector normal, Vector coord) {

    // calculate plane
    float d = Dot(normal, coord);

    if (Dot(normal, ray)) {
        return false; // avoid divide by zero
    }

    // Compute the t value for the directed line ray intersecting the plane
    float t = (d - Dot(normal, rayOrigin)) / Dot(normal, ray);

    // scale the ray by t
    Vector newRay = ray * t;

    // calc contact point
    contact = rayOrigin + newRay;

    if (t >= 0.0f && t <= 1.0f) {
        return true; // line intersects plane
    }
    return false; // line does not
}

在我的测试中,它永远不会返回true ...任何想法?

In my tests, it never returns true... any ideas?

推荐答案

对此我可能是错的,但是代码中有些地方似乎非常可疑.首先,请考虑以下这一行:

I could be wrong about this, but there are a few spots in the code that seem very suspicious. To begin, consider this line:

// calculate plane
float d = Dot(normal, coord);

在这里,您的值d对应于平面法线(向量)与空间中的点(平面上的点)之间的点积.这似乎是错误的.特别是,如果您有任何平面穿过原点,并将原点用作坐标点,那么最终将进行计算

Here, your value d corresponds to the dot product between the plane normal (a vector) and a point in space (a point on the plane). This seems wrong. In particular, if you have any plane passing through the origin and use the origin as the coordinate point, you will end up computing

d = Dot(normal, (0, 0, 0)) = 0

并立即返回false.我不确定您打算在这里做什么,但是我很确定这不是您的意思.

And immediately returning false. I'm not sure what you intended to do here, but I'm pretty sure that this isn't what you meant.

代码中似乎可疑的另一个地方是这一行:

Another spot in the code that seems suspicious is this line:

// Compute the t value for the directed line ray intersecting the plane
float t = (d - Dot(normal, rayOrigin)) / Dot(normal, ray);

请注意,您正在计算平面的法线向量(向量)和射线的原点(空间中的点)之间的点积.这似乎很奇怪,因为这意味着根据射线在空间中的起源,用于射线的缩放比例会发生变化.我建议再看一遍这段代码,看看这是否真的是您的意思.

Note that you're computing the dot product between the plane's normal vector (a vector) and the ray's origin point (a point in space). This seems weird because it means that depending on where the ray originates in space, the scaling factor you use for the ray changes. I would suggest looking at this code one more time to see if this is really what you meant.

希望这会有所帮助!

这篇关于3D线段和平面相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆