2

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?

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • are the path points relative or absolute? – CodeSmile Nov 29 '13 at 07:49
  • Assume they are absolute. – Rahul Iyer Nov 29 '13 at 08:09
  • for shape node and body shape they have to be relative (ie in node coordinate space) – CodeSmile Nov 29 '13 at 09:30
  • Did you ever find an answer to this? I'm noticing that the physics seem to be offset from the actual shape on my SKShapeNode subclass, and I'm too much aof a n00b to understand LearnCocos2D's comment… – Garrett Albright Mar 10 '14 at 06:30
  • Your child node uses a coordinate space relative to the parent node, so in your calculations, you have to specify the child nodes position in the coordinate space of the parent. For example, if the parent node is at (0,0), the location of the child node would be exactly where you expect. But if the parent is at (10,0), then the child node coordinates in the world would be offset by (10,0). So even if you specify the child to be at (20,10), if the parent is at (10,0), then the child will be at (30,10). Get it ? – Rahul Iyer Mar 10 '14 at 07:41
  • What if I'm trying to draw the shape before the sprite is added to a scene (namely, in the subclass's `init` method)? Does that not work? Do I have to addChild it to a parent before I set the path if I want it to be in a predictable location? – Garrett Albright Mar 12 '14 at 05:30
  • I think you're missing the point - Each node has its own coordinate space. While you specify a nodes position in its own coordinate space (actually its parents space), when rendered the position is translated to the "world" coordinate space based on the node hierarchy and its ancestors. If the node has no ancestors (not even attached to a scene), then the node just won't be displayed on screen. You can still do what you want with it, it just won't be visible. – Rahul Iyer Mar 13 '14 at 02:28
  • I understand that it won't be visible if it has no ancestors. What I'm stuck on is seemingly the same thing you were stuck on; once I *do* add the sprite to the scene (after `init`ing it, and thus adding the physics body), the physics body and the sprite shape do not align, despite seemingly being the same dimensions. You seemed to have solved it, but I'm still stumped. [Here's my `init` method](http://pastebin.com/n570dSiZ), if it helps clarify my confusion… I also have a SKScene which is adding instances of this sprite to the screen when and where touched, with gravity. – Garrett Albright Mar 13 '14 at 05:39
  • If you link to a working project which demonstrates the problem I can take a look at it. http://pastebin.com/RBtztK41 is the init method of a class that inherits from SKSpriteNode that I used a while back. Offhand I can't tell whats wrong with your code. – Rahul Iyer Mar 13 '14 at 10:51
  • Well, going off of [this question and answer](http://stackoverflow.com/questions/19396279/skphysicsbody-not-as-expected?rq=1), I've found I can get the expected result if I set my path with `CGRectMake(-16, -16, 32, 32)` - so offset the X and Y params of `CGRectMake()` with half of the intended width and height. I don't understand why this works, but it does. I guess the physics are added around a central point that the graphics normally aren't, or something. It's kind of bizarre. – Garrett Albright Mar 16 '14 at 03:19
  • Well that's what I did in the example I linked to for you. :) The reason you do that is because you want to center your physics body at the anchor point of your sprite. But CGRecMake specifies the origin of the rectangle as the bottom left corner - so that you can always specify the rectangles dimensions in positive coordinates. But the position of a sprite depends on its anchor point, which is the center of the sprite. Its mentioned in the overview of Geometry: https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html – Rahul Iyer Mar 16 '14 at 18:47
  • Ah, I see it now. I didn't think that example was applicable because it's for a subclass of SKSpriteNode instead of SKShapeNode as I'm using - got hung up on that and didn't pay attention to the rest. =[ Thanks. If you post an answer for your question, I'll upvote it. :D – Garrett Albright Mar 17 '14 at 04:09
  • They're all subclasses of SKNode.. – Rahul Iyer Mar 17 '14 at 09:28

1 Answers1

3

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.

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • If I just have an SKShapeNode and want to rotate around its centre, how can I do that? – Bijoy Thangaraj Oct 09 '14 at 16:23
  • Don't know. I've stopped using SpriteKit. Perhaps you should open a fresh question with more details, including what you've tried doing. – Rahul Iyer Oct 10 '14 at 05:02
  • But I imagine you just have to change some attribute marked rotation or angle or something. That seems like a really simple thing to figure out. – Rahul Iyer Oct 10 '14 at 05:09
  • I posted a new question here: http://stackoverflow.com/questions/26284346/rotating-an-skshapenode-along-its-center/26285548#26285548 – Bijoy Thangaraj Oct 10 '14 at 14:03