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

// 值得注意的是都要除以一个 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);
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值