应用线性冲动时,对象开始旋转 - iPhone

时间:2011-10-11 13:10:17

标签: iphone objective-c box2d

在我的游戏中,我使用Box2D将线性脉冲应用于圆形物体。当脉冲施加到物体上时,它开始顺时针旋转,但我不能让它停止旋转。

以下是代码:

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    CGPoint vector = ccpSub(ccp(touchLocation.x,touchLocation.y), _ball.position);

    b2Vec2 forceVector(vector.x / 5, vector.y / 5);

    if ((vector.x <= 100) && (vector.y <= 100))
         _ballBody->ApplyLinearImpulse(forceVector, _ballBody->GetLocalCenter());
}

希望有人能澄清情况。

UPD:

// Create ball body and shape
    b2BodyDef ballBodyDef;
    ballBodyDef.type = b2_dynamicBody;
    ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
    ballBodyDef.userData = _ball;
    _ballBody = _world->CreateBody(&ballBodyDef);

    b2CircleShape circle;
    circle.m_radius = 26.0/PTM_RATIO;

    b2FixtureDef ballShapeDef;
    ballShapeDef.shape = &circle;
    ballShapeDef.density = 0.3f;
    ballShapeDef.friction = 0.2f;
    ballShapeDef.restitution = 0.4f;
    _ballBody->CreateFixture(&ballShapeDef);

1 个答案:

答案 0 :(得分:0)

使用body->applyLinearImpulse(impulse, body->GetPosition())