Reputation: 32459
I have an SKSpriteNode
of an airplane. Here is image (300px x 83px):
The image is named as [email protected]
in my Xcode project.
Now I'm adding a physics body to the airplane:
CGFloat offsetX = self.size.width * self.anchorPoint.x;
CGFloat offsetY = self.size.height * self.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 24 - offsetX, 124 - offsetY);
CGPathAddLineToPoint(path, NULL, 24 - offsetX, 47 - offsetY);
CGPathAddLineToPoint(path, NULL, 339 - offsetX, 1 - offsetY);
CGPathAddLineToPoint(path, NULL, 401 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 452 - offsetX, 52 - offsetY);
CGPathAddLineToPoint(path, NULL, 411 - offsetX, 78 - offsetY);
CGPathAddLineToPoint(path, NULL, 295 - offsetX, 78 - offsetY);
CGPathAddLineToPoint(path, NULL, 268 - offsetX, 93 - offsetY);
CGPathAddLineToPoint(path, NULL, 95 - offsetX, 78 - offsetY);
CGPathAddLineToPoint(path, NULL, 62 - offsetX, 124 - offsetY);
CGPathCloseSubpath(path);
self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
CGPath
is generated by SKPhysicsBody Path Generator
To debug the airplane's physics body I've added an SKShapeNode
with the same CGPath
as for physics body:
SKShapeNode *shape = [[SKShapeNode alloc] init];
shape.path = path;
shape.strokeColor = [SKColor colorWithRed:1.0 green:0 blue:0 alpha:0.5];
[self addChild:shape];
Here is the result:
Am I doing something wrong?
PS: Notices an interesting thing. If I specify CGPath
as follows:
CGPathMoveToPoint(path, NULL, 24/3.0 - offsetX, 124/3.0 - offsetY);
CGPathAddLineToPoint(path, NULL, 24/3.0 - offsetX, 47/3.0 - offsetY);
the shape fits the airplane well!
Upvotes: 1
Views: 749
Reputation: 863
I'm guessing that you need to calculate the points, not the pixels. If you have an @1x image, then it will have the same size in points and pixels. For @2x you will have the same number of points, but double number of pixels and so on for @3x...
Upvotes: 0
Reputation: 903
You must use the original dimensions (320 x 480) or in case of iPhone5+ (320 x 568). @2x/@4x images are only to provide higher resolution images for retina devices. But for backward compatibility reasons all devices still share the same coordinate system.
So your path should be something like:
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"jPfsF.png"];
CGFloat offsetX = sprite.frame.size.width * sprite.anchorPoint.x;
CGFloat offsetY = sprite.frame.size.height * sprite.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 18 - offsetX, 41 - offsetY);
CGPathAddLineToPoint(path, NULL, 31 - offsetX, 25 - offsetY);
CGPathAddLineToPoint(path, NULL, 91 - offsetX, 31 - offsetY);
CGPathAddLineToPoint(path, NULL, 95 - offsetX, 24 - offsetY);
CGPathAddLineToPoint(path, NULL, 136 - offsetX, 26 - offsetY);
CGPathAddLineToPoint(path, NULL, 148 - offsetX, 18 - offsetY);
CGPathAddLineToPoint(path, NULL, 133 - offsetX, 2 - offsetY);
CGPathAddLineToPoint(path, NULL, 75 - offsetX, 1 - offsetY);
CGPathAddLineToPoint(path, NULL, 9 - offsetX, 16 - offsetY);
CGPathAddLineToPoint(path, NULL, 11 - offsetX, 40 - offsetY);
CGPathCloseSubpath(path);
sprite.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
Upvotes: 0
Reputation: 3147
This may not be relevant, but, your defined shape is concave
. The docs say it must be convex
. If you recreate your path with a convex shape, does that help?
Examples: Convex and Concave polygons
Upvotes: 1