Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

physicsBody is misaligned with spriteNode

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.

like image 873
Max Hudson Avatar asked Jan 20 '14 19:01

Max Hudson


1 Answers

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.

like image 200
LearnCocos2D Avatar answered Oct 13 '22 20:10

LearnCocos2D