自己做的小游戏在应用宝上线了,大家可以去应用宝搜索showtime,本系列教程会包含游戏的实现到广告的添加(别打脸),这个游戏还是画了些心思的(虽然画面有点挫),好了废话不多说,开始今天的教程(版本3.0rc0,box2d,cocostudio):
物理世界及摩托车的实现:
showtime.rar (319 KB)
部分代码:
物理世界:
b2Vec2gravity(0.0f,-30.0f);
bool doSleep=true;
m_world=new b2World(gravity);
//设置是否休眠
m_world->SetAllowSleeping(doSleep);
//是否持续物理模拟
m_world->SetContinuousPhysics(true);
/////////////////////////////
// 2. add a menu item with "X" image,which is clicked to quit the program
// you maymodify it.
m_debugDraw = new GLESDebugDraw( 32 );
m_world->SetDebugDraw(m_debugDraw);
//注册到Box2d的world对象里面
uint32 flags = 0;
/* flags += b2Draw::e_shapeBit; //要绘制的信息,这里只绘制Box2d的Shape。
flags +=b2Draw::e_aabbBit;
flags +=b2Draw::e_pairBit;
flags += b2Draw::e_centerOfMassBit;*/
m_debugDraw->SetFlags(flags);
英雄及摩托
b2PolygonShape chejiashape;
chejiashape.SetAsBox(chejia->getContentSize().widthscalenum/32/2,chejia->getContentSize().heightscalenum/32/2);
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.userData=chejia;
bd.position.Set(chejia->getPositionX()/32,chejia->getPositionY()/32);
b2Body* m_car = ((CGameScene*)(this->getParent()->getParent()))->getWorld()->CreateBody(&bd);
b2FixtureDef fd;
fd.shape = &chejiashape;
fd.density = 0.10f;
fd.friction = 0.6f;
fd.restitution=0;
m_car->CreateFixture(&fd);
b2PolygonShape bodyshape;
bodyshape.SetAsBox(body->getContentSize().width/32/12,body->getContentSize().height/32/2);
b2BodyDef bd1;
bd1.type = b2_dynamicBody;
bd1.userData=body;
bd1.position.Set(body->getPositionX()/32,body->getPositionY()/32);
b2Body* bodybody = ((CGameScene*)(this->getParent()->getParent()))->getWorld()->CreateBody(&bd1);
b2FixtureDef fd1;
fd1.shape = &bodyshape;
fd1.density = 0.1f;
fd1.friction = 0.9f;
fd1.restitution=0;
bodybody->CreateFixture(&fd1);
b2CircleShape lunzi1shape;
lunzi1shape.m_radius=lunzi1->getContentSize().width*scalenum/32/2;
// bodyshape.SetAsBox(body->getContentSize().width/32,body->getContentSize().height/32);
b2BodyDef bd2;
bd2.type = b2_dynamicBody;
bd2.userData=lunzi1;
bd2.position.Set(lunzi1->getPositionX()/32,lunzi1->getPositionY()/32);
lunzi1body = ((CGameScene*)(this->getParent()->getParent()))->getWorld()->CreateBody(&bd2);
b2FixtureDef fd2;
fd2.shape = &lunzi1shape;
fd2.density =50.0f;
fd2.friction = 0.8f;
fd2.restitution=0;
m_pWheel1=lunzi1body->CreateFixture(&fd2);
b2CircleShape lunzi2shape;
lunzi2shape.m_radius=lunzi2->getContentSize().width*scalenum/32/2;
//bodyshape.SetAsBox(body->getContentSize().width/32,body->getContentSize().height/32);
b2BodyDef bd3;
bd3.type = b2_dynamicBody;
bd3.userData=lunzi2;
bd3.position.Set(lunzi2->getPositionX()/32,lunzi2->getPositionY()/32);
lunzi2body = ((CGameScene*)(this->getParent()->getParent()))->getWorld()->CreateBody(&bd3);
b2FixtureDef fd3;
fd3.shape = &lunzi2shape;
fd3.density = 1.0f;
fd3.friction = 0.8f;
fd3.restitution=0;
m_pWheel2=lunzi2body->CreateFixture(&fd3);
CUtils::forceVec.clear();
CUtils::forceVec.push_back(m_pWheel2);
//lunzi2body->SetAngularVelocity(-3.1430000.0f);
//lunzi1body->SetAngularVelocity(-3.1430000.0f);
lunzi1body->SetAngularDamping(0);
lunzi1body->ApplyAngularImpulse(-500003.14,true);
lunzi2body->SetAngularDamping(0);
lunzi2body->ApplyAngularImpulse(-500003.14,true);
//m_car->SetFixedRotation(false);
b2WheelJointDef jd;
b2Vec2 axis(0.0f, 1.0f);
jd.Initialize(m_car, lunzi1body,lunzi1body->GetPosition(), axis);
jd.motorSpeed =5000*3.14;
jd.maxMotorTorque = 10.0f;
jd.enableMotor = false;
jd.frequencyHz = m_hz;
jd.dampingRatio = m_zeta;
m_spring1 = (b2WheelJoint*)((CGameScene*)(this->getParent()->getParent()))->getWorld()->CreateJoint(&jd);
jd.Initialize(m_car, lunzi2body,lunzi2body->GetPosition(), axis);
jd.motorSpeed =50000*3.14f;
jd.maxMotorTorque = 3.0f;
jd.enableMotor = true;
jd.frequencyHz = m_hz;
jd.dampingRatio = m_zeta;
m_spring2 = (b2WheelJoint*)((CGameScene*)(this->getParent()->getParent()))->getWorld()->CreateJoint(&jd);
b2PrismaticJointDef jd3;
jd3.Initialize(m_car, bodybody,bodybody->GetPosition(), b2Vec2(0.0f, -1.0f));
jd3.lowerTranslation = 0.0f;
jd3.upperTranslation = 0.0f;
jd3.enableLimit = true;
m_joint3 = (b2PrismaticJoint*)((CGameScene*)(this->getParent()->getParent()))->getWorld()->CreateJoint(&jd3);
效果
地址:
http://blog.csdn.net/gaolijungame/article/details/30441667 showtime.rar (319 KB)