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



我有一个箭头和一个球。箭头的锚点在底部。我所需要做的就是让精灵能够随着触摸而旋转,然后将线性脉冲应用到球上,并朝着箭头指向的方向射击。

假设你将箭头旋转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());
}

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

你对球施加的冲量是(direction, 24.8)。不应该是:(direction.x * 24.8, direction.y * 24.8)。这可能不是唯一的问题,但这是一个跳到我面前的问题。

最新更新