Spinning a prize wheel with touchesMoved in SpriteKit

拜拜、爱过 提交于 2019-12-09 18:36:27

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/26500756/spinning-a-prize-wheel-with-touchesmoved-in-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!