Reputation: 3568
I have a wheel sprite node like below and it can rotate different speeds, directions, angles. Also I have an indicator it can rotate different features (like I cited above for wheel) After tapping a button firstly I remove UIBezierPath follow action for my indicator, at the same time remove all actions from my wheel.
Here's my question. After all things stopped. How Can I determine/handle that which color pointing by that indicator?
I couldn't create a relation indicator's zRotation and wheel's zRotation. Can I get an rotation angle from first state. But If I continue first state will change.
If someone explain this case, It would be great.
Update for indicator returning:
func moveClockWise() {
let dx = person.position.x - self.size.width / 2
let dy = person.position.y - self.size.height / 2
let rad = atan2(dy, dx)
path = UIBezierPath(arcCenter: CGPoint(x: self.size.width / 2, y: self.size.height / 2), radius: 294, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)
let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: true, speed: 500)
person.run(SKAction.repeatForever(follow).reversed(), withKey: "personFollow")
}
Update indicatorAngle return these values (independent from direction)
let dx = person.position.x - self.size.width / 2
let dy = person.position.y - self.size.height / 2
let rad = atan2(dy, dx)
var indicatorAngle = (rad * 180 / .pi).truncatingRemainder(dividingBy: 360)
wheelAngle returns gradually increase in positive or negative (depends on wheel's rotate direction)
var wheelAngle = (wheel.zRotation * 180 / .pi).truncatingRemainder(dividingBy: 360)
Upvotes: 1
Views: 72
Reputation: 59496
As described by @Whirlwind, you just need to map every angle to a color.
func colorName(byRotationOf wheel: SKSpriteNode) -> String {
let degree = wheel.zRotation * 180 / .pi
switch degree {
case 0..<45: return "Dark Green"
case 45..<90: return "Dark Blue"
case 90..<135: return "Purple"
case 135..<180: return "Bordeaux"
case 180..<225: return "Red"
case 225..<270: return "Orange"
case 270..<315: return "Green"
case 315..<360: return "Blue"
default: fatalError()
}
}
If the indicator moves as well, you'll need to get the delta of the wheel angle and the indicator angle.
This code should do the job
func colorName(by wheel: SKSpriteNode, indicatorRotation: CGFloat) -> String {
let wheelAngle = (wheel.zRotation * 180 / .pi).truncatingRemainder(dividingBy: 360)
let indicatorAngle = (indicatorRotation * 180 / .pi).truncatingRemainder(dividingBy: 360)
let deltaAngle = indicatorAngle - wheelAngle
let slice = Int(deltaAngle / 45)
let colorNames = ["Dark Green", "Dark Blue", "Purple", "Bordeaux", "Red", "Orange", "Green", "Blue"]
return colorNames[slice]
}
Upvotes: 2