0

我有一个 box2d 对象,我从上到下扔,我已经设置了它的速度常数,但是当我运行它时,该对象有时具有不同的速度,我怎样才能使这个对象更平滑。

以下是展示我如何创建 box2d 世界和 box2d 身体对象的一些方法。

#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 设置为恒定速度。

4

2 回答 2

0

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

这是代码。

 float spd = 10;
 b2Vec2 velocity = spd*b2Vec2(cos(rotAngle), sin(rotAngle));
 freeBody->SetLinearVelocity(velocity);
于 2013-08-02T19:17:49.390 回答
0

正如上面的评论所world->Step()指示的,您应该使用 fixed dt。验证它dt是固定的并且world->Step()是定期调用的。

于 2013-07-29T10:42:38.780 回答