5

I am creating a spritenode, setting its position and changing its anchorpoint to (0, .5) and then creating a phyicsbody.

The physicsbody thinks my anchorpoint is still at (.5, .5), stupidly.

The same problem is referenced here, but unsolved: Physicsbody doesn't adhere to node's anchor point

The order I am doing things in is correct, it's just my physicsbody is stubborn.

Community
  • 1
  • 1
Max Hudson
  • 9,961
  • 14
  • 57
  • 107

1 Answers1

5

The anchorPoint determines where the node's texture is drawn relative to the node's position. It simply does not affect physics bodies because it's a purely visual property (a texture offset).

For physics-driven nodes it is actually counter-productive to change the anchorPoint from its default because that will change the point around which the texture will rotate. And the physics body will usually also change the node's rotation.

So even if you were to move the physics body shape's vertices to match the sprite with a modified anchorPoint, the physics shape will be misaligned with the image as soon as the body starts rotating. And it'll seem to behave weird.

Plus whatever you want to achieve using anchorPoint you can more flexibly achieve by using the node hierarchy to your advantage. Use a SKNode as the physics node, and add a non-physics sprite node as child to that node and offset it the way you wanted the image to be offset by changing the sprite's anchorPoint.

You end up having two nodes, one invisible representing the physics body and one (or more) sprite(s) representing the visuals for the body but not necessarily tied to the body's center position.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • What would you suggest to make it rotate around the left side of the physics body? – Max Hudson Jan 20 '14 at 20:25
  • Do you want it to behave like a physics object, or do you want to animate it manually? It's difficult and error prone to do both at the same time. Currently Sprite Kit does not support changing the weight distribution of a body which might be the physics way of trying to achieve what you want ... maybe. Anyhow it sounds you should probably try joints if you want a body to rotate around another. – CodeSmile Jan 21 '14 at 00:01
  • I want a body to rotate around it's left side, rather than its center. It shouldn't be that complicated. I don't need to do anything with weight distribution, I just want to create a spritenode that has a physics body and that I can change the zRotation and it will rotate around it's left side rather than its center. – Max Hudson Jan 21 '14 at 01:16
  • Again you can use joints for that. Put a body at the rotation center, and your other body at the correct offset. Use a fixed joint to meld them together, then rotate the body at the rotation center position, that will then rotate the other body at one of its corners. – CodeSmile Jan 21 '14 at 08:36
  • I have been troubled by this very problem. I want a line to rotate on its left corner full 360 degrees and ping if any other body comes into contact. I need to reed this again LearnCocos2D to try to get what you mean, but do you know does this same issue affect how CGRectInterSectsRect works? I tried that first and was getting weird results then moved onto physics bodies...just to get different weird results...thanks – user7865437 Feb 19 '14 at 12:57