2013-07-27 89 views
1

我有一個是經由重力沿x軸線的Box2D的對象而重力在y軸上

int32 velocityIterations = 6; 
int32 positionIterations = 2; 

self.world->Step(dt, velocityIterations, positionIterations); 
self.world->ClearForces(); 

for(b2Body *b = self.world->GetBodyList(); b; b=b->GetNext()) { 
    if (b->GetUserData() != NULL) { 

     id object = (id)b->GetUserData(); 

     if([object isKindOfClass:[FallingObject class]]) 
     { 
      CCSprite *sprite = (CCSprite *)b->GetUserData(); 
      sprite.position = CGPointMake(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO); 
      sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()); 
     } 
    } 
} 

向下移動所述屏幕的Box2D的對象。當用戶在屏幕上或左移動他們的手指拉動或者我想在對象仍然沿着屏幕向下移動時向左或向右移動box2d對象。

任何人都可以提出最好的方法來做到這一點。我曾嘗試應用線速度,但它似乎只是拍攝屏幕。

任何建議

感謝

回答

2

有一些方法可以做到這一點,你需要嘗試最適合你的情況。

可以施加力,衝動,或者手動更改了車體速度只爲X參數:

// x axis force 
b2Vec2 xAxisForce = b2Vec2(10, 0); 

// Try one of these 
b->ApplyForce(xAxisForce, b->GetWorldCenter()); 
b->ApplyForceToCenter(xAxisForce); 
b->ApplyLinearImpulse(xAxisForce, b->GetWorldCenter()); 

// Or change the body velocity manually 
b->SetLinearVelocity(b2Vec2(10, b->GetLinearVelocity().y)); 
+0

那是夢幻般的,感謝您的幫助 –