box2d创建过程

创建重力           
b2Vec2 gravity 

设置重力 	   
gravity.Set(0.0f, -10.0f);	

使用重力先创建一个地球  
b2World world = new b2World(gravity);

//////////////////////////////////////////////////////////////////////////

创建地面体         
b2BodyDef groundBodyDef

设置地面体初始位置 
groundBodyDef.position.Set(0, 0)

使用地面体   通过地球创建地面  
b2Body* groundBody = world->CreateBody(&groundBodyDef);

//////////////////////////////////////////////////////////////////////////

创建地面形状   
b2EdgeShape groundBox;

设置地面形状  	
groundBox.Set(b2Vec2(x1, y1), b2Vec2(x2, y2));

地面使用地面形状 
groundBody->CreateFixture(&groundBox,0);

//////////////////////////////////////////////////////////////////////////

创建物体体     
b2BodyDef bodyDef;

设置物体体的类型   
bodyDef.type = b2_dynamicBody;

设置物体体的初始位置  
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);

使用物体体  通过地球创建物体   
b2Body *body = world->CreateBody(&bodyDef);

创建物体形状 
b2PolygonShape dynamicBox;

设置物体形状
dynamicBox.SetAsBox(.5f, .5f);

创建物体属性   
b2FixtureDef fixtureDef;

设置物体属性    
fixtureDef.shape = &dynamicBox;    
fixtureDef.density = 0.3f;
fixtureDef.friction = 0.3f; 

使用物体属性    
body->CreateFixture(&fixtureDef);    

//////////////////////////////////////////////////////////////////////////

创建物体精灵  
PhysicsSprite* sprite = PhysicsSprite::createWithTexture(_spriteTexture,Rect(0,0,32,32));

添加物体      
sprite->setB2Body(body);

设置现实和虚拟比  
sprite->setPTMRatio(PTM_RATIO);

设置精灵位置   
sprite->setPosition( Point( p.x, p.y) );
addChild(sprite);

更新物体
int velocityIterations = 8;
int positionIterations = 1;

world->Step(dt, velocityIterations, positionIterations);
//////////////////////////////////////////////////////////////////////////


你可能感兴趣的:(box2d创建过程)