我怎样才能在两点之间进行碰撞检查? (C++)
How could I do a collision check between two points? (C++)
基本上,我正在制作的程序将允许用户导入 3d 模型(作为 fbx 或 obj)。然后它将使用 openGL 在 window 中呈现,然后用户将能够在模型上放置点。
所以,我的问题是,如何在这两点之间进行碰撞检查。因此,如果从一点到另一点绘制一条直线,如果它完全碰到 3d 模型,它可以 return 'true' 例如。如果根本不通过模型,就会return 'false'.
下图显示了我将如何应用它。 (在下面显示的图像中,线迹在与模型发生碰撞后变为绿色,我只是在搅拌机中制作图像以帮助描述我的意思。)
Example of the use
伪代码:
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。
可以进行大量优化,例如通过将模型拆分为 octree。交集变为 O(log n) 而不是 O(n)。请参阅射线八叉树交叉点:Ray - Octree intersection algorithms.
基本上,我正在制作的程序将允许用户导入 3d 模型(作为 fbx 或 obj)。然后它将使用 openGL 在 window 中呈现,然后用户将能够在模型上放置点。
所以,我的问题是,如何在这两点之间进行碰撞检查。因此,如果从一点到另一点绘制一条直线,如果它完全碰到 3d 模型,它可以 return 'true' 例如。如果根本不通过模型,就会return 'false'.
下图显示了我将如何应用它。 (在下面显示的图像中,线迹在与模型发生碰撞后变为绿色,我只是在搅拌机中制作图像以帮助描述我的意思。) Example of the use
伪代码:
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。
可以进行大量优化,例如通过将模型拆分为 octree。交集变为 O(log n) 而不是 O(n)。请参阅射线八叉树交叉点:Ray - Octree intersection algorithms.