Angular 冲动

Angular Impulses

所以我使用 angular 脉冲来旋转带有 physicsBody 的精灵。 我希望屏幕的一侧在一个方向上施加脉冲,而屏幕的另一侧将在另一个方向上施加脉冲(参见下面的代码)。

 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    // Defines UI Touch //
    let touch = touches.first as UITouch!
    let touchPosition = touch.locationInNode(self)


    // Left or Right movement based on touch //
    if touchPosition.x < CGRectGetMidX(self.frame) {sprite.physicsBody?.applyAngularImpulse(-10)}
    else {sprite.physicsBody?.applyAngularImpulse(10)}

    // Smooths motion
    let rate: CGFloat = 0.5; //Controls rate of motion. 1.0 instantaneous, 0.0 none.
    let relativeVelocity: CGVector = CGVector(dx:angularVelocity-sprite.physicsBody!.velocity.dx, dy:0);
    sprite.physicsBody!.velocity=CGVector(dx:sprite.physicsBody!.velocity.dx+relativeVelocity.dx*rate, dy:0);

}

唯一的问题是,很明显,如果您按屏幕左侧,然后按屏幕右侧...它会抵消脉冲并停止精灵。

我的问题是,如何更改我的上述代码,使其不会取消,而是向相反方向施加全功率脉冲?

我知道这很容易通过将 relativeVelocity 设置为 0 来实现,但我不知道如何在 'int' 和 'CGVector'[=12= 之间没有冲突的情况下将此值设置为 0 ]

非常感谢任何帮助!

您可能希望在应用相反的 angular 脉冲之前将 angularVelocity 设置为 0。这是示例:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    // Defines UI Touch //
    let touch = touches.first as UITouch!
    let touchPosition = touch.locationInNode(self)

    // Left or Right movement based on touch //
    if touchPosition.x < CGRectGetMidX(self.frame) {
        sprite.physicsBody?.angularVelocity = 0
        sprite.physicsBody?.applyAngularImpulse(-10)
    } else {
        sprite.physicsBody?.angularVelocity = 0
        sprite.physicsBody?.applyAngularImpulse(10)
    }        
}