首页 > 解决方案 > 立方体内任意光线的出射点

问题描述

一旦确定射线的原点包含在定义的边界框中,我将尝试确定射线的出口点。我的给定是:

- 由最小和最大角定义的边界框(中心可计算)
- 边界框中
的任意点 - 与任意点关联的方向向量

目标是尽可能有效地从来自给定点并沿方向向量延伸的射线的边界框找到出口点 (x, y, z)。我的线性代数技能充其量是缺乏的,因此非常感谢任何信息。

对于上下文,这将用于确定当弹丸/实体进入另一个立方体门户时从立方体门户的出口点(任意点是实体在与入口门户重叠的框架上的中心点)

标签: collision-detectiongame-engine

解决方案


我不记得足够多的线性代数来解决它,但我认为这个问题可以这样解决。我也用伪统一 C# 给出了答案,因为那是我最熟悉的。

//-----Givens-----:

//Calculate the box's size in all dimensions and center given extents
Vector3 size,origin;

//The ray's start in world coordinates
Vector3 rawPoint;

//The ray's direction (magnitude doesn't really matter here)
Vector3 rawVector;

//-----Process-----:    

//Normalize direction
Vector3 vector=rawVector.normalize();

//Redefine the ray start reference frame to be from the center of the box
Vector3 point=rawPoint-origin;

//X-intercept
//Solving intercept of y=.5*size.y 
//and equation of line in x,y plane: y-point.y=(vector.y/vector.x)(x-point.x) gives:
float xIntercept=((.5*size.y-point.y)*(vector.x/vector.y))+point.x;

//But we need to make sure we don't exceed the box's size (the intercept can be outside the box)
if (xIntercept>.5*size.x){
    xIntercept=.5*size.x;
}
if(xIntercept<-.5*size.x){
    xIntercept=-.5*size.x;
}

//Then just do the same thing twice more for the other dimensions.
//...

//This is the intercept point as defined from the center of the box
Vector3 localIntercept=new Vector3(xIntercept,yIntercept,zIntercept);

//So we just need to shift it back again to world coordiantes
Vector3 intercept=localIntercept+origin

我相信您可能还需要首先检查该点实际上是否在立方体中。但这很简单。

我将把看起来简单得多的线性代数解决方案留给其他人。


推荐阅读