2015-04-07 64 views
2

我使用LiquidFun與目標C spritekit,在我的Box2D世界,我想創建b2PolygonShape我的精靈,我的精靈顯示下面的圖片:設置polygonShape以六邊形

enter image description here

我嘗試下面的代碼:

b2PolygonShape polygonShape; 
b2Vec2 vertices[6]; 
for (int i = 0 ; i < 6 ; i++) { 
floate angle = -i/6.0 * 360 * DEGTORAD; 
vertices[i].Set(sinf(angle), cosf(angle)); 
} 

polygonShape.Set(vertices, 6); 

它的工作原理,但它比精靈的形象更小,我不能看到物理學系,因爲我可以通過設置看到他們在spritekit的物理世界:

SkView.showsPhysics = YES; 

我的問題是:如何設置polygonShape到我的精靈圖像大小?

謝謝。

回答

2

這是一個真棒的感覺,當沒有人回答你的問題,那麼你發帖回答您的問題:)

正是因爲這些行一樣簡單:

b2Vec2 Verts[6]; 

    Verts[5].Set(-(sprite.frame.size.width/2)/DISPLAY_SCALE,0/DISPLAY_SCALE); 
    Verts[4].Set(-(0.5*sprite.frame.size.width/2)/DISPLAY_SCALE,(sprite.frame.size.height/2)/DISPLAY_SCALE); 
    Verts[3].Set((0.5*sprite.frame.size.width/2)/DISPLAY_SCALE,(sprite.frame.size.height/2)/DISPLAY_SCALE); 
    Verts[2].Set((sprite.frame.size.width/2)/DISPLAY_SCALE,0/DISPLAY_SCALE); 
    Verts[1].Set((0.5*sprite.frame.size.width/2)/DISPLAY_SCALE,-(sprite.frame.size.height/2)/DISPLAY_SCALE); 
    Verts[0].Set(-(0.5*sprite.frame.size.width/2)/DISPLAY_SCALE,-(sprite.frame.size.height/2)/DISPLAY_SCALE); 

    b2PolygonShape Shape; 
    Shape.Set(Verts,num); 

需要注意的是:

const float DISPLAY_SCALE = 32.0;