2012-01-31 106 views
1

我是新來的Cocos2D和第一我學會了如何做一個圓,然後方,現在我學會了如何創建一個多邊形,我有選擇一個頂點的量「 b2polygonshape多邊形」這裏是我對於在動態多邊形改變多邊形頂點在cocos2d

-(void) createDynamicPoly:(CGPoint)p; 
{ 

b2BodyDef bodyDefPoly; 
bodyDefPoly.type = b2_dynamicBody; 
bodyDefPoly.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); 
b2Body *polyBody = world->CreateBody(&bodyDefPoly); 

int count = 8; 
b2Vec2 vertices[8]; 

vertices[0].Set(10.0f/PTM_RATIO,0.0/PTM_RATIO); 
vertices[1].Set(20.0f/PTM_RATIO,0.0f/PTM_RATIO); 
vertices[2].Set(30.0f/PTM_RATIO,10.0f/PTM_RATIO); 
vertices[3].Set(30.0f/PTM_RATIO,20.0f/PTM_RATIO); 
vertices[4].Set(20.0f/PTM_RATIO,30.0f/PTM_RATIO); 
vertices[5].Set(10.0f/PTM_RATIO,30.0f/PTM_RATIO); 
vertices[6].Set(00.0f/PTM_RATIO,20.0f/PTM_RATIO); 
vertices[7].Set(0.0f/PTM_RATIO,10.0f/PTM_RATIO); 

b2PolygonShape polygon; 
polygon.Set(vertices, count); 

b2FixtureDef fixtureDefPoly; 
fixtureDefPoly.shape = &polygon; 
fixtureDefPoly.density = 1.0f; 
fixtureDefPoly.friction = 0.3f; 
polyBody->CreateFixture(&fixtureDefPoly); 


} 

代碼我的問題是如何能夠積極改變這個多邊形的頂點,並改變它的形狀我的屏幕上,無需繪製新的形狀。我的目標是創造一個自由流動的一滴。

三江源

回答

2

修改你的最後一條語句返回的指針所得到的b2Fixture對象。這可以保持作爲一個類變量(即b2Fixture *在你的類接口夾具)。

fixture = polyBody->CreateFixture(&fixtureDefPoly); 

然後,無論你想改變多邊形的頂點,抓住一個指向與您的燈具相關的形狀對象:

b2PolygonShape* shape = (b2PolygonShape*) fixture->GetShape(); 

並修改頂點酌情:

shape->m_vertices[0].Set(new_x0/PTM_RATIO,new_y0/PTM_RATIO); 
shape->m_vertices[1].Set(new_x1/PTM_RATIO,new_y1/PTM_RATIO); 
shape->m_vertices[2].Set(new_x2/PTM_RATIO,new_y2/PTM_RATIO); 
... 
shape->m_vertices[7].Set(new_x7/PTM_RATIO,new_y7/PTM_RATIO); 

祝你好運!

+0

謝謝你這麼多隻是一個問題...它說,有沒有這樣的方法「GetShape」你把在第二行代碼 – 2012-01-31 23:51:08