在SpriteKit中使用touchesMoved旋转奖品轮

时间:2014-10-22 05:19:40

标签: ios iphone sprite-kit game-physics

是否可以使用applyAngularImpulse但不能以指数方式提高车轮的速度? 理想情况下,我希望车轮跟随我的手指,但设置node.zRotation += atan2f(y2-y1, x2-x1)会使我的车轮失控。这就是我所确定的,但感觉非常不稳定:

- (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;
}

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

1 个答案:

答案 0 :(得分:2)

应用你的冲动后:

[node.physicsBody applyAngularImpulse:theImpulse];

只需将角速度钳制到最大速度,其速度取决于您想要让车轮旋转的速度:

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