I want to create an unique physics body for use on a sprite. This sprite will need to have two portions where there is a colliding surface and an open space.
Example:
In the example provided above, the sprite would need to have a physics box2d body where the two green spaces need physics and the brown middle would need to be "empty" or have no physical attributes. Is this possible or do I need to create two sprites?
Yes, it is possibly. You need to create one body and two fixtures for each green part.
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody;
b2Body* dynamicBody = m_world->CreateBody(&myBodyDef);
b2PolygonShape polygonShape;
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &polygonShape;
// Left green rectangle
b2Vec2 vertices[4];
vertices[0].Set(0, 0);
vertices[1].Set(1, 0);
vertices[2].Set(1, 0.5);
vertices[3].Set(0, 0.5);
polygonShape.Set(vertices, 4);
dynamicBody->CreateFixture(&myFixtureDef);
// Right green rectangle
b2Vec2 vertices[4];
vertices[0].Set(5, 0);
vertices[1].Set(6, 0);
vertices[2].Set(6, 0.5);
vertices[3].Set(5, 0.5);
polygonShape.Set(vertices, 4);
dynamicBody->CreateFixture(&myFixtureDef);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.