2015-04-07 60 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;