Hi, All
I just start to learn Box2D for my own iPhone Projects.
What I got understanding is ...
1. Create the "world".
2. Create the "Walls".
3. And I success to move "UIView" Objects, adding to self.view, automatically with gravity I set.
So, I've deside to use b2CircleShape for next step.
The code i wrote is build sucess, but any circles does't appear.....
Any advice !
My code is following..
I just start to learn Box2D for my own iPhone Projects.
What I got understanding is ...
1. Create the "world".
2. Create the "Walls".
3. And I success to move "UIView" Objects, adding to self.view, automatically with gravity I set.
So, I've deside to use b2CircleShape for next step.
The code i wrote is build sucess, but any circles does't appear.....
Any advice !
My code is following..
Code: Select all
-(void)createPhysicsWorld
{
CGSize screenSize = self.view.bounds.size;
// Define the gravity vector.
b2Vec2 gravity;
gravity.Set(0.0f, -9.81f);
// Do we want to let bodies sleep?
// This will speed up the physics simulation
bool doSleep = true;
// Construct a world object, which will hold and simulate the rigid bodies.
world = new b2World(gravity, doSleep);
world->SetContinuousPhysics(true);
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world->CreateBody(&groundBodyDef);
// Define the ground box shape.
b2EdgeShape groundBox;
// bottom
groundBox.Set(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox, 0);
// top
groundBox.Set(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
// left
groundBox.Set(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox, 0);
// right
groundBox.Set(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox, 0);
}
- (void)createCircle
{
//Circle
b2FixtureDef _fixtureDef;
b2CircleShape circleDef;
_fixtureDef.friction = 10;
_fixtureDef.density = 10;
_fixtureDef.restitution = 0.9;
_fixtureDef.shape = &circleDef;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
circleDef.m_radius = 5.0;
bodyDef.position.Set(20/PTM_RATIO, (460.0 - 50)/PTM_RATIO);
b2Body *circleBody = world->CreateBody(&bodyDef);
circleBody->CreateFixture(&_fixtureDef);
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self createPhysicsWorld]; // create the world
[self createCircle]; // create the circle
}