首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Box2d物体在相同速度下平滑地抛出

Box2d物体在相同速度下平滑地抛出
EN

Stack Overflow用户
提问于 2013-07-29 07:20:48
回答 2查看 327关注 0票数 0

我有一个从上到下抛出的box2d对象,我设置了它的速度常数,但是当我运行它时,这个对象有时会有不同的速度,如何使这个对象更流畅。

下面是一些演示如何创建box2d world和box2d body对象的方法。

代码语言:javascript
复制
#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());
        }
    }

}

这是我的触摸事件,在那里我得到的角度和速度投。

代码语言:javascript
复制
- (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设置为恒速。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-02 19:17:49

最后我解决了这个问题。我用ApplyForce换了SetLinearVelocity。

这是密码。

代码语言:javascript
复制
 float spd = 10;
 b2Vec2 velocity = spd*b2Vec2(cos(rotAngle), sin(rotAngle));
 freeBody->SetLinearVelocity(velocity);
票数 0
EN

Stack Overflow用户

发布于 2013-07-29 10:42:38

正如上面的world->Step()注释所指示的那样,您应该使用固定的dt。验证dt是固定的,world->Step()是在规则的间隔内调用的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17918136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档