【问题标题】:How could I do a collision check between two points? (C++)如何在两点之间进行碰撞检查? (C++)
【发布时间】:2016-07-04 12:30:49
【问题描述】:

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

所以,我的问题是,如何在这两点之间进行碰撞检查。因此,如果从一个点到另一个点绘制一条直线,如果它完全撞到 3d 模型,它可以返回“true”,例如。如果根本没有通过模型,就会返回'false'。

下图显示了我将如何应用它。 (在下面显示的图像中,线迹与模型碰撞后变为绿色,我只是在搅拌机中制作了图像以帮助描述我的意思。) Example of the use

【问题讨论】:

  • 关于如何最好地执行多边形之间的碰撞检测的整本书都写了。
  • 听起来更像是一个数学问题而不是编程问题。恕我直言,stackoverflow 的主题外。尝试不同的堆栈交换?

标签: c++ collision trace points


【解决方案1】:

伪代码:

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)。射线八叉树交点见此:Ray - Octree intersection algorithms

【讨论】:

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