cocos2d-x学习之box2d物理引擎打砖块

cocos2d-x学习之box2d物理引擎打砖块,第1张

概述cocos2d-x引擎版本是2.2.0 这篇博客其实不是给给人看的,只是给我自己看的,以便自己熟悉里面的代码 总共6个文件: #ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "../../../../external/Box2D/Box2D.h"#includ


cocos2d-x引擎版本是2.2.0

这篇博客其实不是给给人看的,只是给我自己看的,以便自己熟悉里面的代码

总共6个文件:

@H_404_15@#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "../../../../external/Box2D/Box2D.h"#include "MyContactListener.h"USING_NS_CC;class HelloWorld : public cocos2d::cclayer{public: ~ HelloWorld(); // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'ID' in cocos2d-iphone virtual bool init(); // there's no 'ID' in cpp,so we recommend returning the class instance pointer static cocos2d::CCScene* scene(); // a selector callback voID menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(HelloWorld); //每一帧的回调(象征时间的推移) voID update(float delta); //触摸响应 bool cctouchBegan(cocos2d::CCtouch* ptouch,cocos2d::CCEvent*pEvent); //触摸中 voID cctouchmoved(CCtouch *ptouch,CCEvent *pEvent); //触摸结束 voID cctouchended(CCtouch *ptouch,CCEvent *pEvent); //获取标签 int GetTagForBody(b2Body *body); //主要描述重力加速度的信息 b2World* _world; //物体球 b2Body * _ball; b2Body * _paddle; //精灵 CCSprite *_ballSprite; CCSprite *_paddleSprite; //鼠标关节 b2MouseJoint *_mouseJoint; //碰撞监听器 MyContactListener *_contackListener; //地面 b2Body *_groudBody; //得分 int _score;};#endif // __HELLOWORLD_SCENE_H__
@H_404_15@#include "HelloWorldScene.h"#include "GameOverScene.h"USING_NS_CC;#define PTM_RAITO 26.0f //屏幕上的26个像素代表物理世界的1米CCScene* HelloWorld::scene(){ // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}voID HelloWorld::menuCloseCallback(CCObject* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CcmessageBox("You pressed the close button. windows Store Apps do not implement a close button.","Alert");#else CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif#endif}// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !cclayer::init() ) { return false; } //获得屏幕大小 CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); //创建小球精灵 _ballSprite = CCSprite::create("ball.png"); _ballSprite->setposition(ccp(320,200)); this->addChild(_ballSprite); //创建托盘精灵 _paddleSprite = CCSprite::create("button2.PNG"); _paddleSprite->setposition(ccp(200,50)); this->addChild(_paddleSprite); //创建一个重力加速度是0的世界 _world = new b2World(b2Vec2(0,0)); //定义一个物体 b2BodyDef ballBodyDef; ballBodyDef.type = b2_dynamicBody; //是会运动的 ballBodyDef.position.Set(_ballSprite->getpositionX()/PTM_RAITO,_ballSprite->getpositionY()/PTM_RAITO); //设置位置 ballBodyDef.userData = _ballSprite; //和cocos2d中的精灵进行关联 _ball = _world->CreateBody(&ballBodyDef); //创建它 //设置物理属性 b2CircleShape circle; circle.m_radius = (_ballSprite->getContentSize().wIDth/2)/PTM_RAITO; b2FixtureDef ballFixDef; ballFixDef.shape = &circle;//形状 ballFixDef.density = 10;//密度 ballFixDef.friction = 0;//摩察系数 ballFixDef.restitution = 1;//反d系数 _ball->CreateFixture(&ballFixDef); //创建地面 b2BodyDef groudBodyDef; groudBodyDef.type = b2_staticBody; groudBodyDef.position.Set(0,0); _groudBody = _world->CreateBody(&groudBodyDef); b2EdgeShape groudShape; groudShape.Set(b2Vec2(0,0),b2Vec2(screenSize.wIDth/PTM_RAITO,0)); b2FixtureDef groudFixDef; groudFixDef.shape = &groudShape; _groudBody->CreateFixture(&groudFixDef); //建立左边墙壁 b2BodyDef leftgroudBodyDef; leftgroudBodyDef.type = b2_staticBody; leftgroudBodyDef.position.Set(0,0); b2Body *leftgroudBody = _world->CreateBody(&leftgroudBodyDef); b2EdgeShape leftgroudShape; leftgroudShape.Set(b2Vec2(0,b2Vec2(0,screenSize.height/PTM_RAITO)); b2FixtureDef leftgroudFixDef; leftgroudFixDef.shape = &leftgroudShape; leftgroudBody->CreateFixture(&leftgroudFixDef); //建立右边墙壁 b2BodyDef rightgroudBodyDef; rightgroudBodyDef.type = b2_staticBody; rightgroudBodyDef.position.Set(0,0); b2Body *rightgroudBody = _world->CreateBody(&rightgroudBodyDef); b2EdgeShape rightgroudShape; rightgroudShape.Set(b2Vec2(screenSize.wIDth/PTM_RAITO,screenSize.height/PTM_RAITO)); b2FixtureDef rightgroudFixDef; rightgroudFixDef.shape = &rightgroudShape; rightgroudBody->CreateFixture(&rightgroudFixDef); //建立上边墙壁 b2BodyDef topgroudBodyDef; topgroudBodyDef.type = b2_staticBody; topgroudBodyDef.position.Set(0,0); b2Body *topgroudBody = _world->CreateBody(&topgroudBodyDef); b2EdgeShape topgroudShape; topgroudShape.Set(b2Vec2(0,screenSize.height/PTM_RAITO),screenSize.height/PTM_RAITO)); b2FixtureDef topgroudFixDef; topgroudFixDef.shape = &topgroudShape; topgroudBody->CreateFixture(&topgroudFixDef); //定义一托盘个物体 b2BodyDef paddleBodyDef; paddleBodyDef.type = b2_dynamicBody; //是会运动的 paddleBodyDef.position.Set(_paddleSprite->getpositionX()/PTM_RAITO,_paddleSprite->getpositionY()/PTM_RAITO); //设置位置 paddleBodyDef.userData = _paddleSprite; //和cocos2d中的精灵进行关联 _paddle = _world->CreateBody(&paddleBodyDef); //创建它 //设置物理属性 b2polygonShape BoxShape; BoxShape.SetAsBox(_paddleSprite->getContentSize().wIDth/2/PTM_RAITO,_paddleSprite->getContentSize().height/2/PTM_RAITO); b2FixtureDef paddleFixDef; paddleFixDef.shape = &BoxShape;//形状 paddleFixDef.density = 20;//密度 paddleFixDef.friction = 0;//摩察系数 paddleFixDef.restitution = 0;//反d系数 _paddle->CreateFixture(&paddleFixDef); //开启贞回调动画 this->scheduleUpdate(); //开启触屏 this->settouchEnabled(true); CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,true); //小球初始冲量 _ball->ApplylinearImpulse(_ball->GetMass() * b2Vec2(-15,-5),_ball->GetWorldCenter()); //初始化鼠标关节 _mouseJoint = NulL; //创建水平移动关节 b2prismaticJointDef primaticJointDef; primaticJointDef.Initialize(_world->CreateBody(new b2BodyDef),_paddle,_paddle->GetWorldCenter(),b2Vec2(3.0,0)); _world->CreateJoint(&primaticJointDef); //添加目标 const int padding = 30; int offsetX = 150; for (int i=0; i<4; i++) { //添加经精灵 CCSprite* blockSprite = CCSprite::create("Meta_tiles.png"); blockSprite->setposition(ccp(offsetX,400)); blockSprite->setTag(3); this->addChild(blockSprite); //创建对应的砖块物理实体 b2BodyDef blockBodyDef; blockBodyDef.type = b2_dynamicBody; //是会运动的 blockBodyDef.position.Set(blockSprite->getpositionX()/PTM_RAITO,blockSprite->getpositionY()/PTM_RAITO); //设置位置 blockBodyDef.userData = blockSprite; //和cocos2d中的精灵进行关联 b2Body* block = _world->CreateBody(&blockBodyDef); //创建它 //设置物理属性 b2polygonShape blockShape; blockShape.SetAsBox(blockSprite->getContentSize().wIDth/2/PTM_RAITO,blockSprite->getContentSize().height/2/PTM_RAITO); b2FixtureDef blockFixDef; blockFixDef.shape = &blockShape;//形状 blockFixDef.density = 20;//密度 blockFixDef.friction = 0;//摩察系数 blockFixDef.restitution = 0;//反d系数 block->CreateFixture(&blockFixDef); //左右间隔 offsetX += (padding + blockSprite->getContentSize().wIDth); } //添加碰撞监听器 _contackListener = new MyContactListener; _world->SetContactListener(_contackListener); //得分清零 _score = 0; return true;}//每一帧的回调(象征时间的推移)voID HelloWorld::update(float delta){ //物理世界时间推移 _world->Step(delta,6,6); //物理世界的移动反映到cocos2d界面上 //遍历物理世界的物体 for (b2Body *b = _world->GetbodyList(); b; b= b->GetNext()) { if (b->GetUserData()) { //获取该精灵 CCSprite *sp = (CCSprite *)b->GetUserData(); //给小球限速 if (sp == _ballSprite) { b2Vec2 speed = b->GetlinearVeLocity(); if (speed.LengthSquared() > 200) { b->SetlineardamPing(1-200/speed.LengthSquared()); } } //先得到物理世界的位置 b2Vec2 physicPos = b->Getposition(); //设置位置 sp->setposition(ccp(physicPos.x*PTM_RAITO,physicPos.y*PTM_RAITO)); } } //处理碰撞事件 b2Body *bodyNeedDestroy = NulL; for (vector<contactPeerFix>::iterator it=_contackListener->_contacts.begin(); it!=_contackListener->_contacts.end(); it++) { contactPeerFix curContact = *it; //获取两个碰撞的物体 b2Body* bodyA = curContact.fixA->Getbody(); b2Body* bodyB = curContact.fixB->Getbody(); //获取标签 int tagA = GetTagForBody(bodyA); int tagB = GetTagForBody(bodyB); //小球和地面碰撞 if (bodyA==_ball&&bodyB==_groudBody ||bodyB==_ball&&bodyA==_groudBody) { //游戏失败 cclog("fail"); CCDirector::sharedDirector()->replaceScene(GameOverScene::sceneWithWin(false)); } else if (bodyA==_ball&&3==tagB ||bodyB==_ball&&3==tagA)//小球碰撞砖块 { cclog("pengzhuandaozhuankuai"); //需要删除的砖块 bodyNeedDestroy = (bodyA==_ball ? bodyB : bodyA); _score ++; if (_score==4) { CCDirector::sharedDirector()->replaceScene(GameOverScene::sceneWithWin(true)); } } } //需要删除的砖块 if (bodyNeedDestroy != NulL) { //消除对应的方砖精灵 ((CCSprite*)bodyNeedDestroy->GetUserData())->removeFromParentAndCleanup(true); //从物理世界消除方砖 _world->DestroyBody(bodyNeedDestroy); }}//析构函数HelloWorld::~HelloWorld(){}//触摸响应bool HelloWorld::cctouchBegan(CCtouch* ptouch,CCEvent*pEvent){ //触摸从而加速小球// CCPoint touchPos = ptouch->getLocation();// b2Vec2 touchPhysicsPos(touchPos.x/PTM_RAITO,touchPos.y/PTM_RAITO);// b2Vec2 ballPhysics = _ball->Getposition();// b2Vec2 impulse = touchPhysicsPos - ballPhysics;// // //冲量等于 = 质量 * 矢量// impulse *= _ball->GetMass();// //第二个参数表示作用在质心上// _ball->ApplylinearImpulse(impulse,_ball->GetWorldCenter()); //cclog("cctouchBegan"); //获取触摸点 CCPoint touchPos = ptouch->getLocation(); b2Vec2 touchPhysicsPos(touchPos.x/PTM_RAITO,touchPos.y/PTM_RAITO); //得到底座的形状信息 b2Fixture* paddleFix = _paddle->GetFixtureList(); if (paddleFix->TestPoint(touchPhysicsPos)) { //创建鼠标关节,引领托盘移动 b2MouseJointDef mouseJointDef; mouseJointDef.bodyA = _world->CreateBody(new b2BodyDef); mouseJointDef.bodyB = _paddle; mouseJointDef.maxForce = 1000.0 * _paddle->GetMass(); mouseJointDef.target = touchPhysicsPos;//设置物理目标位置 _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&mouseJointDef); return true; } return false;}//触摸中voID HelloWorld::cctouchmoved(CCtouch *ptouch,CCEvent *pEvent){ //cclog("cctouchmoved"); if (_mouseJoint) { //获取触摸点 CCPoint touchPos = ptouch->getLocation(); b2Vec2 touchPhysicsPos(touchPos.x/PTM_RAITO,touchPos.y/PTM_RAITO); //设置目标位置 _mouseJoint->SetTarget(touchPhysicsPos); }}//触摸结束voID HelloWorld::cctouchended(CCtouch *ptouch,CCEvent *pEvent){ //cclog("cctouchended"); if (_mouseJoint) { //销毁关节 _world->DestroyJoint(_mouseJoint); _mouseJoint = NulL; }}//获取标签int HelloWorld::GetTagForBody(b2Body *body){ if (body->GetUserData()) { CCSprite *sp = (CCSprite *)body->GetUserData(); return sp->getTag(); } return -1;} @H_404_15@#pragma once#include "../../../../external/Box2D/Box2D.h"#include <vector>using namespace std;struct contactPeerFix { b2Fixture *fixA; b2Fixture *fixB;};class MyContactListener : public b2ContactListener{public: MyContactListener(voID); ~MyContactListener(voID); //储存所有碰撞 vector<contactPeerFix> _contacts; voID BeginContact(b2Contact* contact); voID EndContact(b2Contact* contact); //voID PreSolve(b2Contact* contact,const b2Manifold* oldManifold); //voID postsolve(b2Contact* contact,const b2ContactImpulse* impulse);};
@H_404_15@#include "MyContactListener.h"MyContactListener::MyContactListener(voID) : _contacts(){}MyContactListener::~MyContactListener(voID){}//开始撞击voID MyContactListener::BeginContact( b2Contact* contact ){ contactPeerFix contactFix = {contact->GetFixtureA(),contact->GetFixtureB()}; _contacts.push_back(contactFix);}//结束撞击voID MyContactListener::EndContact( b2Contact* contact ){ contactPeerFix peer = {contact->GetFixtureA(),contact->GetFixtureB()}; vector<contactPeerFix>::iterator pos,posFound; for (pos=_contacts.begin(); pos!=_contacts.end(); pos++) { contactPeerFix onePeer = *pos; if (onePeer.fixA==peer.fixA && onePeer.fixB==peer.fixB) { posFound = pos; _contacts.erase(posFound); return ; } }}
@H_404_15@#pragma once#include "cocos2d.h"USING_NS_CC;class GameOverScene : public cclayer{public: //初始化 bool initWithWin(bool isWin); //创建场景 static CCScene* sceneWithWin(bool isWin);};
@H_404_15@#include "GameOverScene.h"//初始化bool GameOverScene::initWithWin( bool isWin ){ //父类初始化 if (!cclayer::init()) { return false; } //显示字符串 char words[64]; if (!isWin) { sprintf(words,"you xi shi bai!"); } else { sprintf(words,"you xi cheng gong!"); } cclabelTTF *lable = cclabelTTF::create(words,"Arial",30); lable->setposition(ccp(320,300)); this->addChild(lable); return false;}//创建场景CCScene* GameOverScene::sceneWithWin( bool isWin ){ CCScene* sc = CCScene::create(); GameOverScene *layer = new GameOverScene; layer->initWithWin(isWin); sc->addChild(layer); return sc;} 总结

以上是内存溢出为你收集整理的cocos2d-x学习之box2d物理引擎打砖块全部内容,希望文章能够帮你解决cocos2d-x学习之box2d物理引擎打砖块所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067377.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存