将线性脉冲应用于Sprite-Cocos2d / Box2D的旋转

时间:2011-09-05 01:37:15

标签: iphone cocos2d-iphone box2d

我有一个箭头和一个球。箭头的anchorPoint位于底部。我需要做的就是让精灵能够通过触摸旋转,然后将线性脉冲施加到球上并按箭头指向的方向射击。

E.G。

假设您将箭头旋转70度然后按下开火按钮。然后球以70度角拍摄。

目前,这是我正在使用的,但是球不会朝着箭头指向的方向射击。

触摸旋转箭头。

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    //acquire the previous touch location
    CGPoint firstLocation = [touch previousLocationInView:[touch view]];
    CGPoint location = [touch locationInView:[touch view]];

    //preform all the same basic rig on both the current touch and previous touch
    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
    CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];

    CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
    CGFloat firstRotateAngle = -ccpToAngle(firstVector);
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

    CGPoint vector = ccpSub(touchingPoint, _arrow.position);
    CGFloat rotateAngle = -ccpToAngle(vector);
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

    //keep adding the difference of the two angles to the dial rotation
    arrowRotation += currentTouch - previousTouch;
}

Fire按钮按箭头方向射击球。

-(void) fireBall: (id)sender
{
    [self unscheduleUpdate];

    direction = arrowRotation;
    b2Vec2 force = b2Vec2(direction, 24.8);
    _ballBody->ApplyLinearImpulse(force, _ballBody->GetWorldCenter());
}

非常感谢任何帮助! 提前谢谢!

1 个答案:

答案 0 :(得分:1)

你申请球的冲动是(direction, 24.8)。不应该是:(direction.x * 24.8, direction.y * 24.8)。这可能不是唯一的问题,但它是一个跳出来的人。