Question

I have the following code which creates an SKShapeNode (self.tiles).

CGMutablePathRef path = CGPathCreateMutable();
.... //Some stuff that creates a path

self.tiles = [SKShapeNode node];
[self addChild:self.tiles];

self.tiles.path = path;
self.tiles.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:path];

But it seems that the physics body that is created does not align with the shape that is created on screen.The SKShapeNode is exactly where I want it on screen (visually).

I have no idea where the physics bodies really are.

How can I align the SKShapeNode and the SKPhysicsBody?

Was it helpful?

Solution

This is for other people who may have trouble understanding:

The anchor point of a sprite, is where the local coordinate system of children of that sprite begins. So if the anchor point is the center of a sprite, then (0,0) of child nodes corresponds to the center of the parent sprite.

In the case of a CGRect, you normally specify the origin of the CGRect (as the lower left corner of the rect), and then specify the dimensions.

Since you may be creating an SKShapeNode or SKPhysicsBody ellipse or rectangle using CGRect, you should offset it, so the center of the rect matches the center of the sprite, so that the shape and the physics body are in the expected position.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top