Understanding zRotation of a SKSpriteNode

I have created two SKSpriteNode with . I am rotating one around the other. So I give both physics bodies and create a joint like so:


  [self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:earthNode.physicsBody bodyB:moon.physicsBody anchor:earthNode.position]];


Then for every touch I perform the rotation:

- (void)touchDownAtPoint:(CGPoint)pos { moon.zRotation = moon.zRotation + 3.14159/2.0; }


The issue I am having is that I expect the image to rotate to 90 degrees here, pi halves radians, but it seems to rotate somewhere between 1/4 and 1/6 of the way around the circle. However, when I change the rotation to:


- (void)touchDownAtPoint:(CGPoint)pos { moon.zRotation = moon.zRotation + 3.14159*2.0; }


There is no movement at all. This is just as I expect as 2pi radians should bring me back to the same location. So how does .zRotation actually work?

Replies

Your joint pins the moon to rotate around the earth. If you try to rotate the moon, the joint's rigidity should move the earth around the moon, but it can't do that because the joint is pinned at the earth.


If you're trying to make the moon follow its orbit, then you should apply the rotation to the earth, not to the moon.

Thank you for the speedy response. This is interesting, I actually want the earth node to stay stationary, but I will try this approach.

It depends what you mean by "stationary". The earth is already stationary in space, because that's where the pin is.


If you mean rotationally stationary (so that its sprite texture doesn't "spin" due to the orbiting moon), you're going to have to use 2 nodes. One has the physics body and pins the joint, but otherwise has no visibile representation. The second is a sprite node that represents the earth visually, and you can choose to make it either a child or a parent of this "pin" node. The parent is slightly easier, because you don't have to compensate for rotation of the pin. (Child node properties are relative to the parent, not absolute.)


Perhaps to answer your original question: zRotation affects the orientation of a single node (and all its children). It doesn't represent the rotation of the joint about its pin.


P.S. I forgot you mention: If you need the value 'pi', you can reference it as CGFloat.pi, Double.pi or Float.pi, depending on what type you need.

Excellent I have the behavior I was looking for, thank you for your help

Select Correct Answer on QuinceyMorris's post if that answered your question to close the thread.