b2RopeJoint连接的物体摆动幅度夸张的解决办法——设置线性阻尼

/**

 * added by Bruce Yang on 2012.03.09.15.41~

 * 创建乌鸦~

 */

-(b2Body*) createCrow:(BYObject*)object {

    b2Vec2 po = object.position;

    b2BodyDef bodyDef;

    bodyDef.type = b2_dynamicBody;

    bodyDef.position.Set(po.x, po.y);

    b2Body *body = _single.gameSceneWorld->CreateBody(&bodyDef);

    body->SetFixedRotation(true);

    /**

     * https://love2d.org/wiki/Body:getLinearDamping

     * Body:getLinearDamping

     * Gets the linear damping(线性阻尼) of the Body.

     * The linear damping is the rate of decrease of the linear velocity over time. 

     * A moving body with no damping and no external forces will continue moving indefinitely, 

     * as is the case in space. A moving body with damping will gradually stop moving.

     * 

     * Damping is not the same as friction - they can be modelled together. 

     * However, only damping is provided by Box2D (and LOVE).

     */

    /**

     * 2012.03.15.11.20, added by Bruce Yang, adviced by Eric Zhu~

     * 相当的有效啊,乌鸦不再烦人的摆来荡去了~

     */

    body->SetLinearDamping(0.8f);

    body->m_isCuttable = false;

    

    b2PolygonShape shape;

    shape.SetAsBox(0.4f0.1f);

    

    b2FixtureDef fixtDef;

    fixtDef.shape = &shape;

    fixtDef.density = CROW_DENSITY;

    fixtDef.friction = CROW_FRICTION;

    fixtDef.restitution = CROW_RESTITUTION;

    body->CreateFixture(&fixtDef);

    

    BYSpriteCrow *sprite = [BYSpriteCrow spriteWithSpriteFrameName:@"animCrow0.png"];

    [sprite setupSchedule];

    sprite.position = ccp(po.x * PTM_RATIO, po.y * PTM_RATIO);

    sprite.tag = TAG_SPRITE_CROW;

    sprite.scale = 1.5f;

    [[BYSingle getInstancespriteRunRepeatAction:sprite animName:ANIMATION_CACHE_CROW_FLY];

    [_single.gameLayer addChild:sprite z:-1];

    body->SetUserData(sprite);

    return body;

}


posted on 2012-03-20 18:43  yang3wei  阅读(254)  评论(0编辑  收藏  举报