문제

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;
}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top