您的位置:首页 > 移动开发 > Objective-C

objective-c创建box2d世界以及地面(或者 edge)

2011-08-13 17:29 267 查看
// 值得注意的是都要除以一个 PTM_RATIO 才能得到精确定制的地面尺寸~
- (void) createGround {
// 边界设置如果采用的是地面盒的话,groudBody 的位置要居中
// 如果采用的是 Edge 的话,groundBody 的位置必须为左下角即为 (0.0f, 0.0f) 这个点~

// *************************** 第一种选择:创建地面盒 ****************************
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(160.0f/PTM_RATIO, -5.0f/PTM_RATIO);	// 设置位置
b2Body *groundBody = _world->CreateBody(&groundBodyDef);

b2PolygonShape groundBox;
groundBox.SetAsBox(160.0f/PTM_RATIO, 5.0f/PTM_RATIO);
groundBody->CreateFixture(&groundBox, 0.0f);

// *************************** 第二种选择:创建上下左右的四个 Edge~ ******************
//	b2BodyDef groundBodyDef;
//	groundBodyDef.position.Set(0.0f, 0.0f);	// 设置位置
//	b2Body *groundBody = _world->CreateBody(&groundBodyDef);
//
//	b2PolygonShape shape;
//
//	b2FixtureDef sd;
//	sd.shape = &shape;
//	sd.density = 0.0f;
//	sd.restitution = 0.4f;	// Edge 的弹性如何~
//
//	// Left vertical
//	shape.SetAsEdge(b2Vec2(5.0f/PTM_RATIO, 5.0f/PTM_RATIO), b2Vec2(5.0f/PTM_RATIO, 475.0f/PTM_RATIO));
//	groundBody->CreateFixture(&sd);
//
//	// Right vertical
//	shape.SetAsEdge(b2Vec2(315.0f/PTM_RATIO, 5.0f/PTM_RATIO), b2Vec2(315.0f/PTM_RATIO, 475.0f/PTM_RATIO));
//	groundBody->CreateFixture(&sd);
//
//	// Top horizontal
//	shape.SetAsEdge(b2Vec2(5.0f/PTM_RATIO, 475.0f/PTM_RATIO), b2Vec2(315.0f/PTM_RATIO, 475.0f/PTM_RATIO));
//	groundBody->CreateFixture(&sd);
//
//	// Bottom horizontal
//	shape.SetAsEdge(b2Vec2(5.0f/PTM_RATIO, 5.0f/PTM_RATIO), b2Vec2(315.0f/PTM_RATIO, 5.0f/PTM_RATIO));
//	groundBody->CreateFixture(&sd);
}

-(void) createBox2dWorld {
// Define the gravity vector.
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);

// Do we want to let bodies sleep? This will speed up the physics simulation
bool doSleep = true;

_world = new b2World(gravity, doSleep);

// Construct a world object, which will hold and simulate the rigid bodies.
_world->SetContinuousPhysics(true);

// Debug Draw functions
_m_debugDraw = new GLESDebugDraw(PTM_RATIO);
_world->SetDebugDraw(_m_debugDraw);

// 创建地面盒或者屏幕边界~
//	[self createGround];

// Add contact listener
MyContactListener *_contactListener = new MyContactListener();
_world->SetContactListener(_contactListener);

// 如果 DEBUG_DRAW 的开关打开了的话,还可以对 DEBUG_DRAW 具体要画出些什么内容进行详细配置~
uint32 flags = 1;
if(DEBUG_DRAW) {
flags = 0;
}
flags += b2DebugDraw::e_shapeBit;
//	flags += b2DebugDraw::e_jointBit;
//	flags += b2DebugDraw::e_aabbBit;
//	flags += b2DebugDraw::e_pairBit;
//	flags += b2DebugDraw::e_centerOfMassBit;
_m_debugDraw->SetFlags(flags);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  object up