我怎么做两点之间的碰撞检查呢?(c++)

How could I do a collision check between two points? (C++)

本文关键字:碰撞 检查 c++ 之间 两点      更新时间:2023-10-16

基本上,我正在制作的程序将允许用户导入3d模型(作为fbx或obj)。然后它将在窗口中使用openGL进行渲染,然后用户将能够在模型上放置点。

我的问题是,如何在这两个点之间做碰撞检查。所以如果当一条直线从一个点画到另一个点,如果它碰到3d模型,它可以返回" true "如果它根本没有通过模型,它将返回'false'。

下面的图片显示了我将如何应用它。(在下图中,线迹在与模型碰撞后变成绿色,我只是在blender中制作了这张照片,以帮助描述我的意思。)使用

的例子

伪代码:

function rayhitsmodel(model, pointA, pointB):
    max-distance <- distance-between(pointA, pointB)
    ray-source <- pointA
    ray-direction <- normalize-vector(pointB - pointA)
    for each triangle-index in model.triangle-indices:
        p1 <- model.points[triangle-index.point1]
        ... similarly for p2, p3
        triangle <- p1, p2, p3
        intersection <- intersect-with-triangle(ray-source, ray-direction, triangle)
        if intersection is not nothing
            and distance-between(intersection.point, ray-source) <= max-distance
                return true
    return false

射线与三角形相交:https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm.

可以大量优化,例如,通过将模型拆分为八叉树。交点变成O(log n)而不是O(n)关于射线-八叉树相交,请参阅:射线-八叉树相交算法。