ios - How can I apply an angular impulse to a Sprite Kit node based on a pan gesture velocity -


What I'm going to do here, spin a SKSpriteNode around its anchor point and its speed and direction match with a pan Account is gesture. So if my pan gesture is clockwise around the rotation, then the curved curved sprite.

My problem with my code is that it works very well from left to right / right to pan under the phantom, but not at all when I try and stacked pan and this Spin the ghost in a wrong way if I pan over the sprite.

Even I have so far -

  go windmill = SKSpriteNode (imageNamed: "Windmill") Override function didMoveToView (see: SKView) {/ * Setup gesture recognitionizers * / go panGestureRecognizer = UIPanGestureRecognizer (target: auto, action: "handlePanGesture:")? Self.view .addGestureRecognizer (panGestureRecognizer) windmill.physicsBody = SKPhysicsBody? (CircleOfRadius: windmill.size.width) windmill.physicsBody .affectedByGravity = false windmill.name = "windmill" windmill.position = CGPoint (X: self.frame.size.width / 2, Y: self.frame.size.height / 2) self.addChild (windmill)} func handlePanGesture (identifier: UIPanGestureRe cognizer) {if (recognizer.state == UIGestureRecognizerState.Changed) {pinwheel.physicsBody? .applyAngularImpulse (recognizer.velocityInView (self.view) .x)}} cause  

I know it is not moving in it with a vertical pan, I'm just getting x value So I understand I need these kind of alliances

I have also tried using this to applyImpulse:. AtPoint:, but it is that the whole result is flowing in Sprite

The following steps indicate a pan A node will rotate on the base.

  1. Store a vector from the center of the node at the initial location of the pan gesture
  2. Create a vector from the center of the node until the end of the pan gesture < Li>
  3. Here is an example of how to do that in Swift ...

      // starting variable in a variable declared location on the startPoint = CGPointZero // Pinwheel.physicsBody to Pinwheel Store to its Origin? .pinned = true // Optionally set dampness property to slow down the wheel with pinwheel.physicsBody .angularDamping = 0.25  

    declared pan handler method

     < Code> function handlePanGesture (identifier: UIPanGestureRecognizer)? {If (recognizer.state == UIGestureRecognizerState.Began) {var location = recognizer.locationInView (self.view) location = self.convertPointFromView (location) go dx = location.x - pinwheel.position.x; Let's d = location Y - Pinwell // touching the starting positionPoint = CGPointMake (dx, sub)} Save vector from the node else if (recognizer.state == UIGestureRecognizerState.Ended) {var location = identizer.locationInView (self.view) location = self.convertPointFromView ( Place) var dx = location.x - pinwheel.position.x; Var dy = location.y - pinwheel.position.y; // Specify the direction to specify the direction of the node = mark (initial page. X * dy - start point.y * dx); Dx = identifier.velocityInView (self.view) .x dy = identifier.velocityInView (self.view) .y // Determine how to rotate the node fast, alternatively, go to speed scale speed = sqrt (dx * Dx + dy * dy) * 0.25 // apply angular impulse pinwheel.physicsBody? .applyAngularImpulse (speed * directions)}}  

Comments