Search code examples
iosiphonesprite-kitgame-physics

Spinning a prize wheel with touchesMoved in SpriteKit


is it possible to use applyAngularImpulse but not exponentially increase the speed of the wheel?
Ideally I'd like to have the wheel follow my finger, but setting node.zRotation += atan2f(y2-y1, x2-x1) spins my wheel out of control. here's what i settled on, but it feels pretty wonky:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

  UITouch *touch = [touches anyObject];
  SKNode *node = [self nodeAtPoint:location];

  CGPoint positionInScene = [touch locationInNode:self];
  CGPoint previousPosition = [touch previousLocationInNode:self];

  [node.physicsBody applyAngularImpulse:(previousPosition.x - positionInScene.x) * 0.1];
  node.physicsBody.angularDamping = 1;
}

scene: https://dl.dropboxusercontent.com/s/gexfburjm1coude/Screen%20Shot%202014-10-21%20at%2010.30.31%20PM.png?dl=0


Solution

  • After applying your impulse:

    [node.physicsBody applyAngularImpulse:theImpulse];
    

    Simply clamp the angular velocity to a maximum speed whose value depends on how fast you want to allow the wheel to spin:

    const CGFloat maxAngularVelocity = 2.5;
    node.physicsBody.angularVelocity = MIN(node.physicsBody.angularVelocity, maxAngularVelocity);