SKPhysicsBody看起来太大了

时间:2013-11-23 10:23:11

标签: sprite-kit skphysicsbody

我有SKSpriteNode架飞机。这是图像(300px x 83px):

An airplane

我的Xcode项目中的图像被命名为p40@2x.png

现在我在飞机上添加一个物理身体:

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];

CGPathSKPhysicsBody Path Generator

生成

为了调试飞机的物理机构,我添加了SKShapeNode与物理机构相同的CGPath

SKShapeNode *shape = [[SKShapeNode alloc] init];
shape.path = path;
shape.strokeColor = [SKColor colorWithRed:1.0 green:0 blue:0 alpha:0.5];
[self addChild:shape];

结果如下:

result

我做错了吗?

PS:注意到一件有趣的事情。如果我指定CGPath如下:

CGPathMoveToPoint(path, NULL, 24/3.0 - offsetX, 124/3.0 - offsetY);
CGPathAddLineToPoint(path, NULL, 24/3.0 - offsetX, 47/3.0 - offsetY);

形状很适合飞机!

3 个答案:

答案 0 :(得分:1)

这可能不相关,但是,您定义的形状为concave。文档说它必须是convex。如果您使用凸形重建路径,这有帮助吗?

enter image description here

示例:Convex and Concave polygons

答案 1 :(得分:0)

您必须使用原始尺寸(320 x 480)或iPhone5 +(320 x 568)。 @ 2x / @ 4x图像仅用于为视网膜设备提供更高分辨率的图像。但出于向后兼容性原因,所有设备仍然共享相同的坐标系。

所以你的道路应该是这样的:

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];

答案 2 :(得分:0)

我猜你需要计算点,而不是像素。如果你有@ 1x图像,那么它的点和像素大小相同。对于@ 2x,您将拥有相同数量的点数,但是对于@ 3x具有双倍像素数等等...