FooTest()
{
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody; // this will be a dynamic body
myBodyDef.position.Set(-10, 20); // a little to the left
b2Body* dynamicBody1 = m_world->CreateBody(&myBodyDef);
};
b2CircleShape circleShape;
circleShape.m_p.Set(0, 0); // position, relative to body position
circleShape.m_radius = 1; // radius
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &circleShape; // this is a pointer to the shape above
dynamicBody1->CreateFixture(&myFixtureDef); // add a fixture to the body
b2Vec2 vertices[5];
vertices[0].Set(-1, 2);
vertices[1].Set(-1, 0);
vertices[2].Set( 0, -3);
vertices[3].Set( 1, 0);
vertices[4].Set( 1, 1);
b2PolygonShape polygonShape;
polygonShape.Set(vertices, 5); // pass array to the shape
myFixtureDef.shape = &polygonShape; // change the shape of the fixture
myBodyDef.position.Set(0, 20); // in the middle
b2Body* dynamicBody2 = m_world->CreateBody(&myBodyDef);
dynamicBody2->CreateFixture(&myFixtureDef); // add a fixture to the body
polygonShape.SetAsBox(2, 1); // a 4x2 rectangle
myBodyDef.position.Set(10,20); // a bit to the right
b2Body* dynamicBody3 = m_world->CreateBody(&myBodyDef);
dynamicBody3->CreateFixture(&myFixtureDef); // add a fixture to the body
myBodyDef.type = b2_staticBody; // change body type
myBodyDef.position.Set(0,0); // middle, bottom
polygonShape.SetAsEdge( b2Vec2(-15,0), b2Vec2(15,0) ); // ends of the line
b2Body* staticBody = m_world->CreateBody(&myBodyDef);
staticBody->CreateFixture(&myFixtureDef); // add a fixture to the body
b2EdgeShape edgeShape;
edgeShape.Set(b2Vec2(-15,0), b2Vec2(15,0));
myFixtureDef.shape = &edgeShape;
b2FixtureDef myFixtureDef;
// ...
myFixtureDef.density = 1; //new code
// for the custom polygon, add 10 to each x-coord
vertices[0].Set(-1 +10, 2);
vertices[1].Set(-1 +10, 0);
vertices[2].Set( 0 +10, -3);
vertices[3].Set( 1 +10, 0);
vertices[4].Set( 1 +10, 1);
// ...
// for the box, use an extended version of the SetAsBox function which allows
// us to set a location and angle (location is offset from body position)
polygonShape.SetAsBox(2, 1, b2Vec2(20,0), 0); //moved 20 units right, same angle
Test()
{
// set up a dynamic body
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody;
myBodyDef.position.Set(0, 20); //middle
b2Body* dynamicBody = m_world->CreateBody(&myBodyDef);
// prepare a shape definition
b2PolygonShape polygonShape;
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &polygonShape;
myFixtureDef.density = 1;
// add four square shaped fixtures around the body center
for ( int i = 0; i < 4; i++)
{
b2Vec2 pos( sinf(i*90*DEGTORAD), cosf(i*90*DEGTORAD) ); // radial placement
polygonShape.SetAsBox(1, 1, pos, 0 ); // a 2x2 rectangle
dynamicBody->CreateFixture(&myFixtureDef); // add a fixture to the body
}
// make a static floor to drop things on
myBodyDef.type = b2_staticBody;
myBodyDef.position.Set(0, 0); // middle, bottom
b2Body* staticBody = m_world->CreateBody(&myBodyDef);
polygonShape.SetAsEdge( b2Vec2(-15,0), b2Vec2(15,3) ); // slightly sloped
staticBody->CreateFixture(&myFixtureDef); // add a fixture to the body
}
myFixtureDef.density = 1;
myFixtureDef.friction = 0; //new code
myFixtureDef.friction = i/4.0;
myFixtureDef.friction = ...;
myFixtureDef.restitution = 0; //new code
fixture->SetDensity( ... );
fixture->SetRestitution( ... );
fixture->SetFriction( ... );
for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext())
{
// do something with the fixture 'f'
}
b2Fixture* f = body->GetFixtureList();
// do something with the fixture 'f'
b2Fixture* myFixture = dynamicBody->CreateFixture(&myFixtureDef);
...
dynamicBody->DestroyFixture(myFixture);