2012-07-08 79 views
1

我最近在長期運行的項目中將box2d的版本升級到了v2.2.1,並且它在現有項目代碼中導致了一些向後兼容性問題。大多數已解決,除了這一個box2d raycast向後兼容性問題

b2Fixture *f = body->GetFixtureList(); 
b2RayCastOutput output; 
b2RayCastInput input; 
f->RayCast(&output, input) // broken call 

現在被打破,期待第三個參數。我看到的Box2D源代碼的函數簽名是

inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const; 

,但我找不到什麼childIndex應該是任何例子。有人能提供一個如何使用這個更新的RayCast功能的例子嗎?

編輯:我注意到設置childIndex爲0似乎工作,但我不知道爲什麼。

回答

2

此參數僅適用於b2ChainShape設備。對於其他形狀類型,它只是在那裏符合虛擬功能簽名。

鍊形狀的功能實際上是由多個b2EdgeShapes完成的,並且鍊形狀本身可以被認爲是幫助組織這些邊緣形狀'兒童'的助手。它分配內存,爲你設置鬼點,並將諸如collsion檢查之類的東西委託給邊緣形狀。

如果你不投射光線對鍊形狀,你可以把它留爲零。如果你是,你可以使用b2ChainShape這些功能投下光線對每個孩子邊:

int32 GetChildCount() const; 
void GetChildEdge(b2EdgeShape* edge, int32 index) const; 

那些二是用這樣的:

b2EdgeShape edgeShape; 
chainShape->GetChildEdge(&edgeShape, 123); 

你需要投的形狀到b2ChainShape *第一:

if (e_chain == fixture->GetType()) { 
    b2ChainShape* chainShape = (b2ChainShape*)fixture->GetShape(); 
    .... 
} 

...它會更容易和更有效地使用b2World的光線投射功能:)

+0

謝謝,非常翔實的答案。鏈接到你如何知道這一點? – 2012-07-10 13:49:38

+0

我對過去的經歷有一個粗略的想法,並檢查了來源以確認:http://code.google.com/p/box2d/source/browse/#svn%2Ftrunk%2FBox2D%2FBox2D%2FCollision%2FShapes查看我的其他信息的網站:www.iforce2d.net/b2dtut – iforce2d 2012-07-10 14:13:09