2013-07-29 23 views
0

我有一个box2d对象,我从上到下抛出,我已经设置了它的速度常量,但是当我运行它时,该对象有时会有不同的速度,我怎样才能使这个对象更平滑。Box2d物体抛光更平滑,速度相同

以下是一些显示box2d世界和box2d body对象的方法。

#pragma -mark Box2D World 
-(void)createWorld 
{ 

    // Define the gravity vector. 
    b2Vec2 b_gravity; 
    b_gravity.Set(0.0f, -9.8f); 

    // Do we want to let bodies sleep? 
    // This will speed up the physics simulation 
    bool doSleep = true; 

    // Construct a world object, which will hold and simulate the rigid bodies. 
    world = new b2World(b_gravity); 
    world->SetAllowSleeping(doSleep); 

    world->SetContinuousPhysics(true); 

} 

-(void) createWeb 
{ 
    freeBodySprite = [CCSprite spriteWithFile:@"web1.png"];//web_ani_6_1 
    //freeBodySprite.position = ccp(100, 300); 
    [self addChild:freeBodySprite z:2 tag:6]; 

    CGPoint startPos = CGPointMake(100, 320/1.25); 

    bodyDef.type = b2_staticBody; 
    bodyDef.position = [self toMeters:startPos]; 
    bodyDef.userData = freeBodySprite; 


    float radiusInMeters = ((freeBodySprite.contentSize.width * freeBodySprite.scale/PTM_RATIO) * 0.5f); 
    shape.m_radius = radiusInMeters; 

    fixtureDef.shape = &shape; 
    fixtureDef.density = 0.07f; 
    fixtureDef.friction = 0.1f; 
    fixtureDef.restitution = 0.1f; 

    circularObstacleBody = world->CreateBody(&bodyDef); 
    stoneFixture = circularObstacleBody->CreateFixture(&fixtureDef); 
    freeBody = circularObstacleBody; 

} 

-(b2Vec2) toMeters:(CGPoint)point 
{ 
    return b2Vec2(point.x/PTM_RATIO, point.y/PTM_RATIO); 
} 

-(b2Body *) getBodyAtLocation:(b2Vec2) aLocation { 
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) 
    { 
     b2Fixture* bodyFixture = b->GetFixtureList(); 
     if (bodyFixture->TestPoint(aLocation)){ 
      return b; 
     } 
    } 
    return NULL; 
} 

-(void) tick: (ccTime) dt 
{ 
    //It is recommended that a fixed time step is used with Box2D for stability 
    //of the simulation, however, we are using a variable time step here. 
    //You need to make an informed choice, the following URL is useful 
    //http://gafferongames.com/game-physics/fix-your-timestep/ 

    int32 velocityIterations = 8; 
    int32 positionIterations = 3; 

    // Instruct the world to perform a single step of simulation. It is 
    // generally best to keep the time step and iterations fixed. 
    world->Step(dt, velocityIterations, positionIterations); 


    //Iterate over the bodies in the physics world 
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) 
    { 
     if (b->GetUserData() != NULL) { 
      //Synchronize the AtlasSprites position and rotation with the corresponding body 
      CCSprite *myActor = (CCSprite*)b->GetUserData(); 
      myActor.position = CGPointMake(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO); 
      myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()); 
     } 
    } 

} 

这是我的触摸事件,我得到的角度和速度扔。

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

    //get the location of the end point of the swipe 
    UITouch *myTouch = [touches anyObject]; 
    CGPoint location = [myTouch locationInView:[myTouch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    //CCLOG(@"Start -> %0.f || End -> %0.f",startPoint.x,location.x); 


     if (freeBody) { 
      //[self calcAngleAndRotateObjectStartingAtPoint:startPoint endingAtPoint:location]; 

      self.isTouchEnabled = NO; 
      freeBody->SetType(b2_dynamicBody); 

      //this is the maximum force that can be applied 
      const CGFloat maxForce = 20; 

      //get the rotation b/w the start point and the end point 
      CGFloat rotAngle = atan2f(location.y - startPoint.y,location.x - startPoint.x); 

      //the distance of the swipe if the force 
      CGFloat distance = ccpDistance(startPoint, location) * 0.5; 


      //if (distance>maxForce) 
       distance = maxForce; 
      //else 
       // distance = 10; 

      //apply force 
      freeBody->ApplyForce(b2Vec2(cosf(rotAngle) * distance, sinf(rotAngle) * distance), freeBody->GetPosition()); 


      //lose the weak reference to the body for next time usage. 
      freeBody = nil; 

     } 
} 

这是代码我使用扔,但有时其速度更快并且在一段时间慢,我已成立maxForce = 20,用于恒定的速度。

+0

你是怎么称呼'嘀'的?在init方法中使用 – cahn

+0

。 – Zohaib

+0

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

回答

0

Finnally我已经解决了这个问题。我用SetLinearVelocity更改了ApplyForce ..

这里是代码。

float spd = 10; 
b2Vec2 velocity = spd*b2Vec2(cos(rotAngle), sin(rotAngle)); 
freeBody->SetLinearVelocity(velocity); 
0

如上述注释world->Step()指示,您应该使用固定的dt。确认dt已修复,并且定期调用world->Step()

+0

我验证了固定时间间隔,但仍未解决。 – Zohaib