2012-04-23 105 views
2

我有Box2d的問題。我在屏幕的中心創建了一個對象(圓圈),旋轉後看看我是否在Box2d上設置好了,並遵循我在互聯網上找到的教程。問題是創建了圓圈,但我無法旋轉,儘管我完全遵循了在互聯網上找到的教程。這是代碼:Box2D調試測試

文件.H:

b2World * _world; 
GLESDebugDraw * _debugDraw; 

文件.mm:

-(void)setupWorld { 

    b2Vec2 gravity = b2Vec2(0.0f, 0.0f); 
    bool doSleep = false; 
    _world = new b2World(gravity, doSleep); 

} 

-(void)setupDebugDraw { 

    _debugDraw = new GLESDebugDraw(PTM_RATIO*[[CCDirector sharedDirector] contentScaleFactor]); 
    _world->SetDebugDraw(_debugDraw); 
    _debugDraw->SetFlags(b2DebugDraw::e_shapeBit |b2DebugDraw::e_jointBit); 

} 

-(void)testBox2D { 

    CGSize winSize = [CCDirector sharedDirector].winSize; 

    b2BodyDef bodyDef; 

    bodyDef.type = b2_dynamicBody; 
    bodyDef.position = b2Vec2(winSize.width/2/PTM_RATIO,winSize.height/2/PTM_RATIO); 

    b2Body *body = _world->CreateBody(&bodyDef); 

    b2CircleShape circleShape; 

    circleShape.m_radius = 25.0/PTM_RATIO; 

    b2FixtureDef fixtureDef; 

    fixtureDef.shape = &circleShape; 
    fixtureDef.density = 1.0; 

    body->CreateFixture(&fixtureDef); 

    body->ApplyAngularImpulse(0.01); 

} 

-(void)updateBox2D:(ccTime)dt { 

_world->Step(dt, 1, 1); 

[self updateBox2D:dt]; 

} 

-(void) draw { 

glDisable(GL_TEXTURE_2D); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); 

_world->DrawDebugData(); 

glEnable(GL_TEXTURE_2D); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

} 
 

在初始化:

[self setupWorld]; 

[self setupDebugDraw]; 

[self testBox2D]; 

回答

0

兩個問題:

你遞歸地呼叫updateBox2D ?這對我來說看起來像一個無限循環,我很驚訝它沒有崩潰你的應用程序。

相反,您應該只在您的CCScenetick方法中調用您的世界步驟一次(或幾次,取決於您如何設置時間步長)。

你的下一個問題是你正在對你的身體施加一個小衝動,但它只是一次衝動......角度衝動仍受阻尼的影響,所以身體不會默認旋轉。爲了保持身體的旋轉,您需要將角度阻尼設置爲零:

bodyDef.angularDamping = 0.0f;