2012-10-27 2 views
1

공이 튀어 오르지 않습니다. 내가 뭘 잘못하고있는 걸까요?Cocos2d-x 공이있는 Box2d가 가속도계로 튀어 오지 않습니다

여기

#include "GameLayer.h" 

USING_NS_CC; 

GameLayer::GameLayer(){ 
    mWorld = NULL; 
    mBody=NULL; 
    mBall=NULL; 

} 

GameLayer::~GameLayer(){ 

     delete mWorld; 
     mBody=NULL; 
     delete mBall; 
      mBall=NULL; 
} 

bool GameLayer::init() 
{ 
    if (!CCLayer::init()) 
    { 
     return false; 
    } 

    CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 
    // Create sprite and add it to the layer 
    mBall = CCSprite::spriteWithFile("Ball.jpg",CCRectMake(0,0,52,52)); 
    mBall->setPosition(ccp(100,100)); 
    addChild(mBall); 

    // Create a world 
    b2Vec2 gravity; 
    gravity.Set(0.0f,-10.0f); 

    mWorld = new b2World(gravity); 

    // Do we want to let bodies sleep? 
    // mWorld->SetAllowSleeping(true); 

    // mWorld->SetContinuousPhysics(true); 

    //Definition of the body 
    // body definition to specify initial properties of the body such as position or velocity. 
    b2BodyDef groundBodyDef; 
    groundBodyDef.position.Set(0,0); 

    // use the world object to create a body object by specifying the body definition. 
    b2Body *groundBody = mWorld->CreateBody(&groundBodyDef); 

    //Create the shape as needed (for ground all the sides) 
    b2EdgeShape groundEdge; 
    groundEdge.Set(b2Vec2_zero,b2Vec2(winSize.width/PTM_RATIO,0)); 
    groundBody->CreateFixture(&groundEdge,0); 
    groundEdge.Set(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO)); 
    groundBody->CreateFixture(&groundEdge,0); 
    groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO), 
         b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO)); 
    groundBody->CreateFixture(&groundEdge,0); 
    groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO, 
           winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0)); 
    groundBody->CreateFixture(&groundEdge,0); 

    // Create the another body 
     //1. body defn 
     //2. create body from game world 
     // 3. define shape and create fixtures 
    b2BodyDef ballBodyDef; 
    ballBodyDef.position.Set(100/PTM_RATIO,100/PTM_RATIO); 
    // applying sprite as userdata to the bodydef 
    ballBodyDef.userData = mBall; 
    mBody = mWorld->CreateBody(&ballBodyDef); 

    // created different shapes as you want 
    b2CircleShape circle; 
    circle.m_radius = 26.0/PTM_RATIO; 

    //create the fixturedef 
    b2FixtureDef ballShapeDef; 
    ballShapeDef.shape = &circle; 
    ballShapeDef.density=1.0f; 
    ballShapeDef.friction=0.2f; 
    ballShapeDef.restitution=0.8f; 

    mBody->CreateFixture(&ballShapeDef); 

    setAccelerometerEnabled(true); 

    scheduleUpdate(); 

    return true; 
} 


void GameLayer::update(float dt){ 

    mWorld->Step(dt,10,10); 

    for(b2Body *b = mWorld->GetBodyList(); b; b=b->GetNext()) { 
      if (b->GetUserData() != NULL) { 
       CCSprite *ballData = (CCSprite *)b->GetUserData(); 
       ballData->setPosition(ccp(b->GetPosition().x * PTM_RATIO, 
         b->GetPosition().y * PTM_RATIO)); 
       ballData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle())); 

      } 
     } 

} 

void GameLayer::didAccelerate(CCAcceleration* pAccelerationValue) 
{ 

    b2Vec2 gravity(-pAccelerationValue->y * 15,pAccelerationValue->x *15); 
     mWorld->SetGravity(gravity); 


} 

답변

1

b2BodyDef 기본적으로 b2_staticBody 유형을 가지고있는 클래스이다. 설정하려고 시도하십시오.

ballBodyDef.type = b2_dynamicBody; 
관련 문제