DroidHeaven
DroidHeaven

Reputation: 2464

CGPathMoveToPoint/CGPathAddLineToPoint method parameters are confusing

I'm using the answer provided here SpriteKit's SKPhysicsBody with polygon helper tool to create polygons of physics bodies for my sprite nodes.

But the offset parameters of CGPathMoveToPoint/CGPathAddLinetoPoint methods are confusing(I am unable to find a reference in Apple's doc too).

In the code generated by the script there, offsetX and offsetY are defined as:

CGFloat offsetX = sprite.frame.size.width * sprite.anchorPoint.x;
CGFloat offsetY = sprite.frame.size.height * sprite.anchorPoint.y;

While in the gif provided there for the demonstration, it is:

CGFloat offsetX = sprite.frame.size.width/2; 
CGFloat offsetY = sprite.frame.size.height/2;

Both of these offsets are used to correct the parameters in CGPathMoveToPoint/CGPathAddLinetoPoint methods like:

CGPathMoveToPoint(path, NULL, 0 - offsetX, 0 - offsetY); (for the bottom left corner of the texture)
CGPathAddLineToPoint(path, NULL, 11 - offsetX, 0 - offsetY); 
  1. Why do we need this offset?

  2. For the bottom left corner of the texture, why can't I provide just (0,0) like:

    CGPathMoveToPoint(path, NULL, 0, 0);

  3. Will this offset(if needed) change when the sprite node is moving across the screen?

  4. If the anchor point of the parent scene is on top left corner, what should be the offset for these methods?

Upvotes: 0

Views: 481

Answers (1)

sangony
sangony

Reputation: 11696

The offset variable you are referring to is just a starting point. You can just as easily replace the variables with fixed starting coordinate like 0,0.

Your question is regarding a physics body so anchor points do not apply in this case. The physics body 0,0 coordinates are always centered in relation to the node's width and height. So while changing the anchor point for a node effects how its coordinates are centered, it has no effect on the physics body.

Upvotes: 0

Related Questions