如果您有交點和AABB(邊界框)中心,您可以快速計算以獲得與您擊中的面對應的索引。
然後用存儲法線的數組,您可以獲取數據。
Vector3 ComputeNormal(Vector3 inter, Vector3 aabbCenter)
{
static const Vector3 normals[] = { // A cube has 3 possible orientations
Vector3(1,0,0),
Vector3(0,1,0),
Vector3(0,0,1)
};
const Vector3 interRelative = inter - aabbCenter;
const float xyCoef = interRelative.y/interRelative.x;
const float zyCoef = interRelative.y/interRelative.z;
const int coef = (isBetweenInclusive<1,-1>(xyCoef) ? 1 :
(isBetweenExclusive<1,-1>(zyCoef) ? 2 : 0));
// Here it's exclusive to avoid coef to be 3
return normals[coef] * SIGN(interRelative); // The sign he is used to know direction of the normal
}
我還沒有測試過,所以不要驚訝,如果它不直接工作;)但它應該做的伎倆。