Spinning a prize wheel with touchesMoved in SpriteKit
Asked Answered
B

1

4

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

Brahmi answered 22/10, 2014 at 5:19 Comment(0)
U
2

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);
Unscramble answered 22/10, 2014 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.