你能帮我吗。想要绘制一个多边形(不同角度的光束)并将二维盒子应用到它上面。你能告诉我如何创建一个多边形形状的 CCSprite 任何例子都会帮助干杯
问问题
3583 次
3 回答
2
创建多边形主体。
-(void) createDynamicPoly { b2BodyDef bodyDefPoly; bodyDefPoly.type = b2_dynamicBody; bodyDefPoly.position.Set(3.0f, 10.0f); b2Body *polyBody = world->CreateBody(&bodyDefPoly); int count = 8; b2Vec2 vertices[8]; vertices[0].Set(0.0f / PTM_RATIO,0.0f / PTM_RATIO); vertices[1].Set(48.0f/PTM_RATIO,0.0f/PTM_RATIO); vertices[2].Set(48.0f/PTM_RATIO,30.0f/PTM_RATIO); vertices[3].Set(42.0f/PTM_RATIO,30.0f/PTM_RATIO); vertices[4].Set(30.0f/PTM_RATIO,18.0f/PTM_RATIO); vertices[5].Set(18.0f/PTM_RATIO,12.0f/PTM_RATIO); vertices[6].Set(6.0f/PTM_RATIO,18.0f/PTM_RATIO); vertices[7].Set(0.0f/PTM_RATIO,30.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); }
创建你的精灵
通过 Fixture 和 UserData 将您的精灵附加到多边形主体
fixtureDefPoly.SetUserData() = spriteObject; b2Fixture *fixture; fixture = circleBody->CreateFixture(&fixtureDefPoly); fixture->SetUserData(@"spriteObject");
然后在您的更新方法中将精灵迭代到主体。
于 2011-03-23T12:44:53.100 回答
0
最简单的方法是打开图像编辑器(例如绘画或 photoshop)并创建所需的图像。在你的程序中使用它。
使用 cocos2d box2d 模板创建 xcode 应用程序时还有一个 helloWorld 场景。它创建了一组带有纹理的正方形。
于 2011-03-19T08:19:52.097 回答
0
CGPoint startPt = edge.start ;
CGPoint endpt = edge.end ;
//length of the stick body
float len = abs(ccpDistance(startPt, endpt))/PTM_RATIO;
//to calculate the angle and position of the body.
float dx = endpt.x-startPt.x;
float dy = endpt.y-startPt.y;
//position of the body
float xPos = startPt.x+dx/2.0f;
float yPos = startPt.y+dy/2.0f;
//width of the body.
float width = 1.0f/PTM_RATIO;
b2BodyDef bodyDef;
bodyDef.position.Set(xPos/PTM_RATIO, yPos/PTM_RATIO);
bodyDef.angle = atan(dy/dx);
NSLog([NSString stringWithFormat:@"Setting angle %f",bodyDef.angle]);
CCSprite *sp = [CCSprite spriteWithFile:@"material-wood.png" rect:CGRectMake(0, 0, 12, 12)];
//TODO: fix shape
[self addChild:sp z:1 ];
bodyDef.userData = sp;
bodyDef.type = b2_dynamicBody;
b2Body* body = world->CreateBody(&bodyDef);
b2PolygonShape shape;
b2Vec2 rectangle1_vertices[4];
rectangle1_vertices[0].Set(-len/2, -width/2);
rectangle1_vertices[1].Set(len/2, -width/2);
rectangle1_vertices[2].Set(len/2, width/2);
rectangle1_vertices[3].Set(-len/2, width/2);
shape.Set(rectangle1_vertices, 4);
b2FixtureDef fd;
fd.shape = &shape;
fd.density = 1.0f;
fd.friction = 0.300000f;
fd.restitution = 0.600000f;
body->CreateFixture(&fd);
于 2011-03-23T18:53:56.343 回答