立方体内任意光线的出射点

exit point of an arbitrary ray within a cube

一旦确定射线的原点包含在定义的边界框内,我就会尝试确定射线的出射点。我的礼物是:

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

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

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

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

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

我会将可能看起来更简单的线性代数解决方案留给其他人。