立方体中任意射线的出射点

时间:2018-07-09 19:10:07

标签: collision-detection game-engine

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

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

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

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

1 个答案:

答案 0 :(得分:1)

我不记得有足够的线性代数来解决这种问题,但是我认为可以像这样解决问题。另外,我已经用伪统一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

我相信您可能还需要检查点实际上是否在多维数据集中。但这很简单。

我将把看起来更简单的线性代数解决方案留给别人。