coding22 - 10 months ago 34

Swift Question

I'm able to rotate my node with one finger but I'm having trouble with my

`if`

`if`

`print("rotate right")`

`print("rotate left")`

EDIT here is a picture of the spot.

`override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {`

for touch in touches {

var previousAngle: CGFloat = 0.0

let location = touch.locationInNode(self)

let node = self.nodeAtPoint(location)

if node.name == "circle" {

if lastNodeSelected != nil {

let touchedNode = lastNodeSelected!

//lets user rotate with one finger.

let dy = touchedNode.position.y - location.y

let dx = touchedNode.position.x - location.x

let angle2 = atan2(dy, dx)

touchedNode.zRotation = angle2

let delta = (angle2 - previousAngle)

if delta > 0 {

print("rotate left")

}

else {

print("rotate right")

}

previousAngle = angle2

}

}

}

}

Answer

`atan2`

gives results from -π to π, so there’s a transition point where the value jumps by 2π (or -2π, depending on the direction you’re going).

To make this work in the general case, you can’t just subtract angles; you need to figure out whether `angle2`

is closer to `previousAngle`

moving clockwise or counterclockwise.

Try:

```
func rotationDelta(fromAngle a0: Double, toAngle a1: Double) -> Double {
let π = M_PI
return ((a1 - a0) % (2*π) + 3*π) % (2*π) - π
}
```

…and then:

```
let delta = rotationDelta(fromAngle: previousAngle, toAngle: angle2)
```