0

I am trying to come up with a way to accumulate the rotation of a SKNode. For example, if a node is rolling down a hill (circular physics body), I want to know how many rotations it has made, or an accumulated angle. I will need it to increment when rotating one way and decrement the other.

I have thought of keeping track of the node's zRotation and on every update updating a count when it passes over from >0 to <0, but that seems hacky since I have to come up with some sort of range to check and it can miss a rotation from either a fast spinning node or low frame rate.

any advice would be awesome!

edit: this is my solution, it works perfectly for my purposes

-(void)update:(CFTimeInterval)currentTime {
    //get CurrentAngle (-pi to pi)
    float currentAngle = _wheelSprite.zRotation;
    if (currentAngle<0) {
        //change angle format (0 to M_PI*2)
        currentAngle+=M_PI*2;
    }

    //calculate angle change
    float change = currentAngle - _lastAngle;
    if (change>M_PI) {
        change-=2*M_PI;
    }else if (change<-M_PI){
        change+=2*M_PI;
    }

    //update last angle
    _lastAngle = currentAngle;

    //update actualWheelAngle
    _actualWheelAngle += change;
}
Nathan
  • 139
  • 10

1 Answers1

3

What I would do is create a variable that gets incremented/decremented with the zRotation change each frame. Then dividing that value by 360 would give you the amount of rotations.

prototypical
  • 6,731
  • 3
  • 24
  • 34
  • 1
    Since the zRotation value is stored in radians, you could also divide it by M_2_PI – ZeMoon Mar 03 '14 at 11:47
  • That's about what I did, I will post my solution in the question. akashg is right about the radians. – Nathan Mar 04 '14 at 00:12
  • Your counter variable can be either radians or degrees, I was just suggesting the concept of a counter and using division to determine a full rotation. I chose degrees for clarity of the approach I was suggesting. – prototypical Mar 04 '14 at 23:26