2012-01-19 34 views
2

XNA包含一個BoundingFrustum類,該類定義了一個平截頭體並促進了與射線和其他對象的碰撞。但是,Frustum只能用Matrix來構造。我有一個使用8個頂點在平截頭體形狀中創建的某個對象;我應該從這些頂點創建什麼樣的矩陣來創建一個Frustum來表示它?從8個角落創建BoundingFrustum

有問題的物體是一個球體的塊 - 球體表面上的4個點以正方形的形式向下延伸到球體的原點。

+0

你只是想檢查球體表面和方塊內是否有東西,或者你是否需要檢查球體的內部? –

+0

內部也是如此。球體是由平截體塊組成的,我試圖通過大塊來進行採摘。 –

回答

2

由於網卡的靈感和朋友的幫助下,我能寫這個類,它代表由具有平坦的側面,如截錐或棱鏡8點定義的區域。

Here is the class

重要的是要注意,在傳遞構造函數參數時,您選擇一個有利於查看您的區域並堅持使用它的有利位置。

希望這可以幫助任何可能遇到此問題的人解決問題。

2

通常使用BoundingFrustum你傳遞一個矩陣是視圖矩陣乘以一個投影矩陣:

BoundingFrustum frustum = new BoundingFrustum(this.viewMatrix * this.projectionMatrix);

還有就是用那個類做你的描述,除非你什麼沒有簡單的方法在手工創建矩陣方面特別熟練,它將通常在視圖矩陣和投影矩陣中的東西組合成代表您8個角的東西。

我會推薦的是寫一個算法來解決你的問題。

// Do something like this for all 8 sides of the frustum, if the sphere lies outside 
// of any of the 8 sides then it isn't in the frustum. 

// Each plane will have a normal direction (the direction the inside is facing) 
Vector3 normal = Vector3.UnitY; 

// Creates a plane 
Plane plane = new Plane(normal, 20.0f); 

BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 10.0f); 

// This type is an enum that will tell you which side the intersection is on 
PlaneIntersectionType type = sphere.Intersects(plane); 
+0

我不確定使用平面交叉點來定義區域。看起來相當複雜。我試圖用Ray來交叉這個區域,所以我不會得到一個IntersectionType,但我想我可以以某種方式使用點積...因爲平面和光線都是無限的,所以很難高效計算,我認爲。 此外,我不知道我是否不能使用我的球體的屬性來構建該區域的視圖和投影矩陣。我希望更多地瞭解這些矩陣是如何構建的。 –