`
yangping_Account
  • 浏览: 187275 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

使用Box2d物理系统

 
阅读更多

 

上一节讲述了粒子的相关问题,当然啦,不示弱,今天继续将物理系统给大家进行简单的介绍和讲述;

       首先先介绍,如何在cocos2d中加入box2d开发lib包,因为一般使用cocos2d引擎进行开发游戏时,大家创建项目都会选用cocos2d框架,而不是直接采用物理系统的cocos2d框架,但是后期忽然需要在项目中使用物理系统(这种情况很经常发生,至于为什么,童鞋们都懂得~),OK,首先创建一个普通的cocos2d项目;

       OK,加入box2d->lib步骤如下:

       1. 首先将box2d的lib包拷贝到刚创建的项目下,然后右键项目的libs文件夹进行导入项目中;(如果你没有box2d的lib包,那就新建一个cocos2d-box2d的项目就有了)

       2.双击你的项目名默认打开配置信息窗口,点击Build Settings标签,然后在页面中找到”Search Paths“一栏,然后在“User Header Search Paths”中添加“xx/libs”;这里的XX是你的项目名称;紧接着在“User Header Search Paths”一项的上面设置“Always Serch Paths”的选项 为YES,默认为NO;这里务必要设置;

       3.最后commadn+B (我用的xcode For lion)编译项目代码,如果提示编译成功,OK,可以使用啦;


      下面我来给大家简单的介绍以下如何在cocos2d中使用Box2d物理系统,虽然关于Box2d的相关资料和教程很少,但是这里我也不会很详细的介绍和解释,因为我即将上市的Android游戏开发书籍中已经对Box2d进行了很详细的讲解和两个物理小游戏实战,所以这里就大概的介绍下一些重要的方法;

      便于讲解,这里我直接使用Xcode直接创建一个cocos2d-Box2d的项目,然后简单的修改:

 

//  

//  HelloWorldLayer.mm  

//  Box2dProject  

//  

//  Created by 华明 李 on 11-9-14.  

//  Himi  

//  

// Import the interfaces  

#import "HelloWorldLayer.h"  

   

#define PTM_RATIO 32  

  

// enums that will be used as tags  

enum {  

    kTagTileMap = 1,   

    kTagAnimation1 = 1,  

};  

  

  

// HelloWorldLayer implementation  

@implementation HelloWorldLayer  

  

+(CCScene *) scene  

{   

    CCScene *scene = [CCScene node];    

    HelloWorldLayer *layer = [HelloWorldLayer node];   

    [scene addChild: layer];   

    return scene;  

}  

  

// on "init" you need to initialize your instance  

-(id) init  

{  

    //初始化中,在屏幕上创建了物理世界,并且创建了在屏幕四周创建了刚体防止物理世界中的刚体超屏  

    //最后并且调用一个tick方法用于让物理世界不断的去模拟  

    if( (self=[super init])) {   

        self.isTouchEnabled = YES;   

        self.isAccelerometerEnabled = YES;   

        CGSize screenSize = [CCDirector sharedDirector].winSize;  

        CCLOG(@"Screen width %0.2f screen height %0.2f",screenSize.width,screenSize.height);   

        // Define the gravity vector.  

        b2Vec2 gravity;  

        gravity.Set(0.0f, -10.0f);  

        bool doSleep = true;    

        world = new b2World(gravity, doSleep);   

        world->SetContinuousPhysics(true);   

        // Debug Draw functions  

        m_debugDraw = new GLESDebugDraw( PTM_RATIO );  

        world->SetDebugDraw(m_debugDraw);   

        uint32 flags = 0;  

        flags += b2DebugDraw::e_shapeBit;   

        m_debugDraw->SetFlags(flags);       

        b2BodyDef groundBodyDef;  

        groundBodyDef.position.Set(0, 0); // bottom-left corner   

        b2Body* groundBody = world->CreateBody(&groundBodyDef);   

        b2PolygonShape groundBox;         

                // bottom  

        groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));  

        groundBody->CreateFixture(&groundBox,0);  

          

        // top  

        groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));  

        groundBody->CreateFixture(&groundBox,0);  

          

        // left  

        groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));  

        groundBody->CreateFixture(&groundBox,0);  

          

        // right  

        groundBox.SetAsEdge(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));  

        groundBody->CreateFixture(&groundBox,0);  

          

            

        CCLabelTTF *label = [CCLabelTTF labelWithString:@"Himi" fontName:@"Marker Felt" fontSize:32];  

        [self addChild:label z:0];  

        label.position = ccp( screenSize.width/2, screenSize.height-50);   

        [self schedule: @selector(tick:)];  

    }  

    return self;  

}  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics