cocos2d-x_Box2d实例代码

  • c4_323843
    了解作者
  • 675.7KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-18 10:26
    上传日期
学习cocos2dx后自己整的物理引擎(box2d)的类似helloworld的示例代码。cocos2d-x_Box2d实例代码,完整可运行的工程例子。对初学cocos2dx的物理引擎开发的同志们非常有用!
Box2dStudy.rar
  • Box2dStudy
  • proj.win32
  • res
  • Box2dStudy.ico
    46.5KB
  • Debug.win32
  • cl.command.1.tlog
    4.2KB
  • link.5724.read.1.tlog
    2B
  • rc.command.1.tlog
    1.4KB
  • CL.read.1.tlog
    117.2KB
  • Box2dStudy.win32.log
    2.9KB
  • vc100.idb
    1.5MB
  • mt.read.1.tlog
    410B
  • link.14144-cvtres.write.1.tlog
    2B
  • Box2dStudy.win32.exe.embed.manifest.res
    472B
  • link.14144.read.1.tlog
    2B
  • link.5724-cvtres.read.1.tlog
    2B
  • link-cvtres.read.1.tlog
    2B
  • link.read.1.tlog
    4.3KB
  • link.14144-cvtres.read.1.tlog
    2B
  • link.15812.read.1.tlog
    2B
  • rc.read.1.tlog
    2.8KB
  • link.write.1.tlog
    1.4KB
  • link.14144.write.1.tlog
    2B
  • CL.write.1.tlog
    2.3KB
  • Box2dStudy.win32_manifest.rc
    234B
  • vc100.pdb
    740KB
  • link.4488-cvtres.write.1.tlog
    2B
  • link.15812-cvtres.write.1.tlog
    2B
  • main.obj
    70.3KB
  • link.command.1.tlog
    2.5KB
  • Box2dStudy.res
    47.4KB
  • link.4488.read.1.tlog
    2B
  • link.15812.write.1.tlog
    2B
  • mt.write.1.tlog
    410B
  • Box2dStudy.win32.lastbuildstate
    54B
  • link.4488-cvtres.read.1.tlog
    2B
  • Box2dStudy.win32.exe.embed.manifest
    406B
  • link.4488.write.1.tlog
    2B
  • HelloWorldScene.obj
    191.7KB
  • link.5724.write.1.tlog
    2B
  • link.5724-cvtres.write.1.tlog
    2B
  • link.15812-cvtres.read.1.tlog
    2B
  • Box2dStudy.win32.exe.intermediate.manifest
    381B
  • mt.command.1.tlog
    490B
  • rc.write.1.tlog
    674B
  • AppDelegate.obj
    97.3KB
  • link-cvtres.write.1.tlog
    2B
  • resource.h
    500B
  • Box2dStudy.win32.vcxproj.filters
    2.3KB
  • Box2dStudy.win32.vcxproj.user
    763B
  • Box2dStudy.rc
    2.2KB
  • main.cpp
    1.1KB
  • Box2dStudy.win32.vcxproj
    8.1KB
  • main.h
    270B
  • Classes
  • AppDelegate.cpp
    1.4KB
  • HelloWorldScene.h
    1008B
  • HelloWorldScene.cpp
    6.3KB
  • AppDelegate.h
    960B
  • Resources
  • HelloWorld.png
    141.5KB
  • CloseNormal.png
    6.2KB
  • fps_images-ipadhd.png
    22.9KB
  • fps_images-hd.png
    22.9KB
  • fps_images.png
    6.8KB
  • CloseSelected.png
    5.4KB
内容介绍
#include "HelloWorldScene.h" #define PTM_RATIO 32 //像素与米的比例,在物理引擎中单位是MKS【米、千克、秒】 //这里是为游戏中像素和物理世界中的米转换做的准备 using namespace cocos2d; CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World". CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); CC_BREAK_IF(! pLabel); // Get window size and place the label upper. CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width / 2, size.height - 50)); // Add the label to HelloWorld layer as a child layer. this->addChild(pLabel, 1); // 3. Add add a splash screen, show the cocos2d splash image. CCSprite* pSprite = CCSprite::create("HelloWorld.png"); CC_BREAK_IF(! pSprite); // Place the sprite on the center of the screen pSprite->setPosition(ccp(size.width/2, size.height/2)); // Add the sprite to HelloWorld layer as a child layer. this->addChild(pSprite, 0); setWorld(); bRet = true; } while (0); return bRet; } void HelloWorld::setWorld() { b2Vec2 gravity; gravity.Set(0.0f,-10.0f); world=new b2World(gravity); CCSize winSize=CCDirector::sharedDirector()->getWinSize(); b2BodyDef groundBodyDef; groundBodyDef.position.Set(0,0);//bottom-left corner //创建身体 b2Body *groundBody=world->CreateBody(&groundBodyDef); //创建地面形状:在屏幕四周创建了刚体防止物理世界中的刚体超屛 b2EdgeShape groundBox; //bottom groundBox.Set(b2Vec2(0,0),b2Vec2(winSize.width/PTM_RATIO,0)); groundBody->CreateFixture(&groundBox,0); //top groundBox.Set(b2Vec2(0,winSize.height/PTM_RATIO),b2Vec2(winSize.width/PTM_RATIO,winSize.height/PTM_RATIO)); groundBody->CreateFixture(&groundBox,0); //left groundBox.Set(b2Vec2(0,winSize.height/PTM_RATIO),b2Vec2(0,0)); groundBody->CreateFixture(&groundBox,0); //right groundBox.Set(b2Vec2(winSize.width/PTM_RATIO,winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO,0)); groundBody->CreateFixture(&groundBox,0); this->setTouchEnabled(true); this->setAccelerometerEnabled(true); this->scheduleUpdate(); } void HelloWorld::menuCloseCallback(CCObject* pSender) { // "close" menu item clicked CCDirector::sharedDirector()->end(); } void HelloWorld::update( float dt )//注意,之前2dx的版本是用ccTime的,在2.0.3版本中,用float类型 { int32 velocityIterations = 8; int32 positionIteratoins = 1; world->Step( dt, velocityIterations, positionIteratoins); for( b2Body *b = world->GetBodyList();b;b = b->GetNext() ) { if(b->GetUserData() != NULL) { CCSprite *myActor = (CCSprite*)b->GetUserData(); //, myActor->setPosition(ccp((b->GetPosition().x )* PTM_RATIO,b->GetPosition().y * PTM_RATIO));//设置精灵位置 myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );//设置精灵旋转方向 } } } void HelloWorld::addNewSpriteWithCoords( CCPoint point, CCSprite *sprite ) { CCLOG("Add sprite %0.2f x %02.f",point.x,point.y); sprite->setPosition(point); b2BodyDef bodyDef;//定义刚体 bodyDef.type = b2_dynamicBody;//使刚体能够在力的作用下运行,刚体有三种:静态的、运动的、动态的 bodyDef.position.Set( point.x/PTM_RATIO, point.y/PTM_RATIO);//设置刚体的初始位置 bodyDef.userData = sprite; b2Body *body =world->CreateBody( &bodyDef ); //b2PolygonShape dynamicBox; //dynamicBox.SetAsBox(.9f,.9f); b2CircleShape dynamicBox; dynamicBox.m_radius = 18.0f/PTM_RATIO;//设置半径 b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox;//绑定形状 fixtureDef.density = 1.0f;//设置密度 fixtureDef.friction = 0.3f;//设置摩擦,当然还可以设置恢复,即反弹,这里我就不一一赘述 body->CreateFixture( &fixtureDef ); //给body施加一个力 b2Vec2 force = body->GetWorldVector(b2Vec2(00.0f,0.0f)); b2Vec2 point_force = body->GetWorldPoint( b2Vec2(0.4f,0.4f) ); body->ApplyForce(force,point_force); } void HelloWorld::ccTouchesEnded( CCSet *pTouches, CCEvent *pEvent) { CCLog("bb"); int count = pTouches->count();//这句貌似木有用呐 for( CCSetIterator iterTouch = pTouches->begin();iterTouch != pTouches->end();iterTouch++) { CCTouch *touch = (CCTouch *)*iterTouch; cocos2d::CCPoint location = touch->locationInView(); CCSprite *sprite = CCSprite::create("CloseNormal.png"); this->addChild(sprite); this->addNewSpriteWithCoords( location, sprite ); } }
评论
    相关推荐
    • Cocos2d Game
      I create a Cocos2d game
    • cocos2d富文本
      cocos2d 富文本实现 格式 [i:图片路径|-] [s:22|c:00ff00ff]***[-] [s:22]***[-] [l:item/1001]...[-] 随意组合嵌套
    • Cocos2d学习文档
      Cocos2d学习文档 适合新手 第1章-介绍cocos2d.pdf 第2章-开始学习.pdf 。。。。 第4章-你的第一个游戏(修正错误).pdf 。。。。
    • Cocos2d学习资料
      Cocos2D目录: 第一章:介绍Cocos2d 第二章: 开始学习 第三章: 基础知识 第四章: 你的第一个游戏(修正错误) 第五章: 游戏构成要素 第六章: 深入了解精灵(Sprite) 第七章: 横向滚屏射击游戏 第八章: 完成滚屏射击游戏...
    • cocos2d demo
      本资源是一个小的经典的cocos2d-x的demo,后续每周更新增加功能的版本
    • cocos2d精灵教程
      (译)cocos2d精灵教程:第一部分.pdf (译)cocos2d精灵教程:第三部分.pdf (译)cocos2d精灵教程:第二部分.pdf (译)cocos2d菜单教程:第一部分.pdf (译)cocos2d菜单教程:第三部分(完).pdf (译)...
    • cocos2d模板
      cocos2d-template-updated
    • Cocos2d手势
      为实现Cocos2d手势支持,改写的CCLayer、CCNode、CCGestureRecognizer三个源码类,可以参考我博客使用!
    • Cocos2D文档
      Cocos2D开发参考文档 Cocos2D帮助文档
    • cocos2d实例
      cocos的一些实例代码,有助于初学者。