这里我们修改HelloWorldScene场景,在这里实现
我们的思路是首先初始化一些刚体,,然后实现点击屏幕产生小鸟,松开小鸟,小鸟飞出,撞击初始化好的那些刚体,产生碰撞检测效果。
1.在HelloWorld.h中
#include"cocos2d.h"
#include"Box2d/Box2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
// there'sno 'ID' in cpp,so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here'sa difference. Method 'init' in cocos2d-x returns bool,instead of returning'ID' in cocos2d-iphone
virtual bool init();
// aselector callback
voID menuCloseCallback(cocos2d::Ref* pSender);
//implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
b2World*world;//创建一个物理世界
virtual bool ontouchBegan(touch *touch, Event *unused_event);
virtual voID ontouchmoved(touch *touch, Event *unused_event);
virtual voID ontouchended(touch *touch, Event *unused_event);
voID addNewPigAtPoint(int x,int y);
int pigIndex;//记录猪的编号
voID update(float t);//
voID initpig();//初始化一些猪
int birdindex;//鸟德编号
voID pigSmile(Sprite*spig,float x,float y,floatang);//猪笑了
voID pigCry(Sprite*spig,floatang);//猪哭了
voID birdContact(Sprite*spbird,float ang);//鸟掉毛
};
#endif
2.在Helloworld.cpp中
#include"HelloWorldScene.h"
#include"Box2d/Box2d.h"
#include"BirdContact.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene'is an autorelease object
auto scene = Scene::create();
// 'layer'is an autorelease object
auto layer = HelloWorld::create();
// addlayer as a child to scene
scene->addChild(layer);
// returnthe scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1.super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto bg=Sprite::create("startbg.png");
this->addChild(bg);
bg->setposition(Director::getInstance()->getWinSize().wIDth/2,
Director::getInstance()->getWinSize().height/2);
//创建一个物理世界
b2Vec2 gravite;
gravite.Set(0.0f,-9.8f);//定义一个初始化的坐标--牛顿的重力
this->world=new b2World(gravite);//这个参数作为这个物理世界的特性
//定义物理世界的侦听----碰撞检测
BirdContact*@R_403_6818@enerb=new BirdContact();//
world->SetContact@R_403_6818@ener(@R_403_6818@enerb);//侦听和物理世界相互绑定
//创建一个静态的刚体--地板
b2BodyDef grounddef;
grounddef.position.Set(0.0f,120/32.0f);
b2Body*ground=world->CreateBody(&grounddef);
//创建家具定义刚体的形状
b2polygonShape groundshape;//定义刚体的形状--为多边形
groundshape.SetAsBox(visibleSize.wIDth/32,10/32);//定义一个多边型的盒子
ground->CreateFixture(&groundshape,0);//让地板的刚体绑定这个形状
//添加一个侦听
auto @R_403_6818@ener=Event@R_403_6818@enertouchOneByOne::create();
@R_403_6818@ener->ontouchBegan=CC_CALLBACK_2(HelloWorld::ontouchBegan,this);
@R_403_6818@ener->ontouchmoved=CC_CALLBACK_2(HelloWorld::ontouchmoved,this);
@R_403_6818@ener->ontouchended=CC_CALLBACK_2(HelloWorld::ontouchended,this);
Director::getInstance()->getEventdispatcher()->addEvent@R_403_6818@enerWithSceneGraPHPriority(@R_403_6818@ener,this);
//记录猪的编号,初始化的时候=0
pigIndex=0;
//必须模拟物理世界
this->scheduleUpdate();
//添加一个d弓
auto sprite1=Sprite::create("leftshot.png");
sprite1->setposition(Vec2(200,170));
sprite1->setScale(3);
this->addChild(sprite1);
auto sprite2=Sprite::create("rightshot.png");
sprite2->setposition(Vec2(200,170));
sprite2->setScale(3);
this->addChild(sprite2);
//画线
auto drawnode1=DrawNode::create();
drawnode1->setTag(10001);
this->addChild(drawnode1);
drawnode1->setVisible(false);//不显示
auto drawnode2=DrawNode::create();
drawnode2->setTag(10002);
this->addChild(drawnode2);
drawnode2->setVisible(false);//不显示
//初始化一些猪
initpig();
//初始化小鸟的值为0
birdindex=0;
return true;
}
voID HelloWorld::menuCloseCallback(Ref* pSender)
{
}
bool HelloWorld::ontouchBegan(touch *touch, Event *unused_event)
{
//添加一个刚体
//addNewPigAtPoint(touch->getLocation().x,touch->getLocation().y);
//添加小鸟
auto spbird=Sprite::create("bird1.png");
spbird->setTag(200+birdindex);
spbird->setScale(2);
this->addChild(spbird);
spbird->setposition(touch->getLocation());
birdindex++;
//显示两条线
this->getChildByTag(10001)->setVisible(true);
this->getChildByTag(10002)->setVisible(true);
DrawNode*n1=(DrawNode*)this->getChildByTag(10001);
DrawNode*n2=(DrawNode*)this->getChildByTag(10002);
n1->clear();
n2->clear();
n1->drawSegment(Vec2(200-20,230),spbird->getposition(),4,color4F(255,255,1));
n2->drawSegment(Vec2(200+20,1));
n1->drawSegment(Vec2(200-20,1));
return true;
}
voID HelloWorld::ontouchmoved(touch *touch, Event *unused_event)
{
//移动小鸟
auto bird=this->getChildByTag(200+birdindex-1);
bird->setposition(touch->getLocation());
DrawNode*n1=(DrawNode*)this->getChildByTag(10001);
DrawNode*n2=(DrawNode*)this->getChildByTag(10002);
n1->clear();
n2->clear();
n1->drawSegment(Vec2(200-20,bird->getposition(),1));
}
voID HelloWorld::ontouchended(touch *touch, Event *unused_event)
{
//发射
auto bird=this->getChildByTag(200+birdindex-1);
//将小鸟加刚体
b2BodyDef bodydef;
bodydef.type=b2_dynamicBody;
bodydef.position.Set(touch->getLocation().x/32,touch->getLocation().y/32);
b2Body*newbird=world->CreateBody(&bodydef);
//定义夹具
//b2CircleShape c;//圆形
//c.m_radius=50/32;//半径
b2polygonShape birdshap;
birdshap.SetAsBox(50/32,50/32);
//定义盒子的形状
b2FixtureDef fixturebird;
fixturebird.shape=&birdshap;
//设置属性
fixturebird.density=1.0;
fixturebird.friction=0.3;
fixturebird.restitution=0.5;
newbird->CreateFixture(&fixturebird);
//最核心的--
newbird->SetUserData(bird);//物理世界在物理世界模拟的时候就会根据这个进行坐标的更新和角度的更新
//给刚体加力
b2Vec2 force=b2Vec2((200-touch->getLocation().x),
( 230-touch->getLocation().y));//力=d弓的点到你拉动的点---/3经过多次加算出来的
newbird->ApplylinearImpulse(force,newbird->Getposition(),true);//应用一个线速度 true:是否恢复计算
//隐藏两条线
this->getChildByTag(10001)->setVisible(false);
this->getChildByTag(10002)->setVisible(false);
}
voID HelloWorld::addNewPigAtPoint(int x,int y)
{
//产生一个动态的刚体
b2BodyDef pigdef;
pigdef.type=b2_dynamicBody;//动态的刚体
pigdef.position.Set(x/32,y/32);
b2Body*newpig=world->CreateBody(&pigdef);//创建一个刚体
//定义形状夹具--圆形
b2polygonShape pigshape; //--方形
//b2CircleShape c;//圆形
//c.m_radius=50/32;//半径
pigshape.SetAsBox(50/32,50/32);//宽度,高度--这个盒子的大小
b2FixtureDef fixturedef;//定义盒子的形状
fixturedef.shape=&pigshape;
fixturedef.density=1.0f;//阻尼--密度
fixturedef.friction=0.3;//摩擦力
fixturedef.restitution=0.3f;//恢复系数
newpig->CreateFixture(&fixturedef);//动态刚体绑定这个形状
//刚体怎么表现?跟我们的cocos2d进行绑定
auto sprite=Sprite::create("pig_1.png");
sprite->setTag(100+pigIndex);//记录下猪的编号,我们创建了几个猪
pigIndex++;
this->addChild(sprite);
newpig->SetUserData(sprite);//刚体中包含了只指向当前精灵的指针
sprite->setposition(x,y);
}
voID HelloWorld::update(float t){
//下一个时间这些刚体都运动到哪
world->Step(t,8,3);//t-时间 8--模拟8次 3---迭代了多少次
//重新获取坐标刚体,并更新cocos的精灵显示
for (b2Body*b=world->Getbody@R_403_6818@(); b!=NulL; b=b->GetNext()){
Sprite*sp=(Sprite*)b->GetUserData();
if (sp!=NulL) {
sp->setposition(b->Getposition().x*32,
b->Getposition().y*32);
sp->setRotation(-CC_radians_TO_degrees(b->GetAngle()));//弧度转化成角度
}
}
}
voID HelloWorld::initpig()
{
//添加一个刚体
this->addNewPigAtPoint(700,128);
this->addNewPigAtPoint(700,160);
this->addNewPigAtPoint(700,192);
this->addNewPigAtPoint(700,224);
this->addNewPigAtPoint(700,256);
this->addNewPigAtPoint(700,288);
//
this->addNewPigAtPoint(750,128);
this->addNewPigAtPoint(750,160);
this->addNewPigAtPoint(750,192);
this->addNewPigAtPoint(750,224);
this->addNewPigAtPoint(750,256);
this->addNewPigAtPoint(750,288);
this->addNewPigAtPoint(800,128);
this->addNewPigAtPoint(800,160);
this->addNewPigAtPoint(800,192);
this->addNewPigAtPoint(800,224);
this->addNewPigAtPoint(800,256);
this->addNewPigAtPoint(800,288);
this->addNewPigAtPoint(850,128);
this->addNewPigAtPoint(850,160);
this->addNewPigAtPoint(850,192);
this->addNewPigAtPoint(850,224);
this->addNewPigAtPoint(850,256);
this->addNewPigAtPoint(850,288);
this->addNewPigAtPoint(900,128);
this->addNewPigAtPoint(900,160);
this->addNewPigAtPoint(900,192);
this->addNewPigAtPoint(900,224);
this->addNewPigAtPoint(900,256);
this->addNewPigAtPoint(900,288);
}
voID HelloWorld::pigSmile(Sprite*spig,floatang){
Vector<SpriteFrame*> allf;
allf.pushBack(SpriteFrame::create("pig_1.png",Rect(0,100,100)));
allf.pushBack(SpriteFrame::create("pig_2.png",100)));
allf.pushBack(SpriteFrame::create("pig_3.png",100)));
auto animation=Animation::createWithSpriteFrames(allf);
animation->setDelayPerUnit(0.3);
auto animate=Animate::create(animation);
spig->runAction(animate);
}//猪笑了
voID HelloWorld::pigCry(Sprite*spig,floatang){
Vector<SpriteFrame*> allf;
allf.pushBack(SpriteFrame::create("pig_1.png",100)));
allf.pushBack(SpriteFrame::create("pig_4.png",100)));
auto animation=Animation::createWithSpriteFrames(allf);
animation->setDelayPerUnit(0.3);
auto animate=Animate::create(animation);
spig->runAction(Sequence::create( animate,
CallFuncN::create([&](Node * obj){
((Sprite*)obj)->setTexture("pig_1.png");
}),
NulL));
}//猪哭了
voID HelloWorld::birdContact(Sprite*spbird, float x,floatang)
{
for (int i=0; i<3; i++) {
auto sp=Sprite::create("yumao1.png");
//apaw与动作序列一样,但是是同时执行
auto act1=Spawn::create(RotateBy::create(3,i*60),//1.5秒旋转这么大得角度
FadeOut::create(1.5),
ScaleBy::create(3,3),
nullptr);
sp->setposition(x+i*10,y+i*10);
sp->setAnchorPoint(Vec2(0.5,0.5));
this->addChild(sp);
sp->runAction(Sequence::create(act1,CallFuncN::create([](Node*obj)
{
obj->removeFromParentAndCleanup(true);
}),
NulL));
}
}//鸟掉毛
3.实现碰撞检测,加入鸟撞击到猪,就让小鸟掉毛,小猪哭。
我们在BirdContact.h中这个样定义,也就是说我们定义侦听
#include"cocos2d.h"
#include"Box2d/Box2d.h"
USING_NS_CC;
class BirdContact:public b2Contact@R_403_6818@ener{
public:
virtual voID BeginContact(b2Contact* contact) ;
virtual voID EndContact(b2Contact* contact);
virtual voID PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
virtual voID postsolve(b2Contact* contact, const b2ContactImpulse* impulse);
};
4.BirdContact.h中这个样实现,我们在BirdContact::postsolve中实现撞击,得到冲量
#include"BirdContact.h"
#include"HelloWorldScene.h"
voID BirdContact::BeginContact(b2Contact* contact)
{
}
voID BirdContact::EndContact(b2Contact* contact)
{
}
voID BirdContact::PreSolve(b2Contact* contact,const b2Manifold* oldManifold)
{
}
voID BirdContact::postsolve(b2Contact* contact,const b2ContactImpulse* impulse)
{
//得到的时横坐标的冲量
float force=impulse->normalimpulses[0];//水平方向
if (force>2) {//两牛
//得到第一个碰撞体
Sprite*sA=(Sprite*)contact->GetFixtureA()->Getbody()->GetUserData();
//得到第二个碰撞体
Sprite*sB=(Sprite*)contact->GetFixtureB()->Getbody()->GetUserData();
if (sA==NulL||sB==NulL)
{return;}
if (sA->getTag()>=100 && sA->getTag()<200)//猪
{
HelloWorld*NowLayer=(HelloWorld*)sA->getParent();
NowLayer->pigCry(sA,sA->getpositionX(),sA->getpositionY(),sA->getRotation());
}
if (sB->getTag()>=100 && sB->getTag()<200)//猪
{
HelloWorld*NowLayer=(HelloWorld*)sA->getParent();
NowLayer->pigCry(sA,sA->getRotation());
}
if (sA->getTag()>=200 && sA->getTag()<300)//鸟
{
HelloWorld*NowLayer=(HelloWorld*)sA->getParent();
NowLayer->birdContact(sA,sA->getRotation());
}
if (sB->getTag()>=200 && sB->getTag()<300)//鸟
{
HelloWorld*NowLayer=(HelloWorld*)sA->getParent();
NowLayer->birdContact(sA,sA->getRotation());
}
}
}
最后我们实现的效果如图:
总结以上是内存溢出为你收集整理的Cocos2d Box2d 物理引擎实现愤怒的小鸟全部内容,希望文章能够帮你解决Cocos2d Box2d 物理引擎实现愤怒的小鸟所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)