SKPhysics体路径生成器

Posted

技术标签:

【中文标题】SKPhysics体路径生成器【英文标题】:SKPhysicsBody Path Generator 【发布时间】:2015-03-30 17:11:46 【问题描述】:

我正在使用SKPhysicsBody Path Generator

为我的精灵生成polygonFromPath。问题是我需要一条有两个孔的路径,我该如何实现?

如下图所示,路径最多只能连接一个孔,但我需要两个孔!有什么帮助吗?

我也尝试了以下方法,但它崩溃了:

let path = CGPathCreateMutable()

        MoveToPoint(path, x: 51 , y:22 , node:sprite)
        AddLineToPoint(path, x: 38 , y:45 , node:sprite)
        AddLineToPoint(path, x: 32 , y:44 , node:sprite)
        AddLineToPoint(path, x: 32 , y:0 , node:sprite)
        AddLineToPoint(path, x: 39 , y:0 , node:sprite)

        let path2 = CGPathCreateMutable()

        MoveToPoint(path2, x: 0 , y:22 , node:sprite)
        AddLineToPoint(path2, x: 13 , y:0 , node:sprite)
        AddLineToPoint(path2, x: 19 , y:0 , node:sprite)
        AddLineToPoint(path2, x: 19 , y:45 , node:sprite)
        AddLineToPoint(path2, x: 13 , y:45 , node:sprite)

sprite.physicsBody = SKPhysicsBody(bodies: [path, path2])

【问题讨论】:

【参考方案1】:

您不能将bodyWithPolygonFromPath 用于您想要的形状。 bodyWithPolygonFromPath 仅适用于凸多边形路径。

您可以使用bodyWithBodies:(NSArray *)bodies 在右侧和底部绘制两个三角形,在左上角绘制正方形。请记住,空白区域不会注册联系人。

这是一个例子:

SKSpriteNode node0 = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:CGSizeMake(200, 200)];
node0.position = CGPointMake(400, 400);

SKPhysicsBody *firstBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 50) center:CGPointMake(-75, 75)];
SKPhysicsBody *secondBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(200, 50) center:CGPointMake(0, -75)];
SKPhysicsBody *thirdBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 150) center:CGPointMake(75, 26)];
node0.physicsBody = [SKPhysicsBody bodyWithBodies:@[firstBody, secondBody, thirdBody]];
node0.physicsBody.dynamic = NO;
[self addChild:node0];

【讨论】:

感谢您的回复 :) 我编辑了我的问题:我尝试了 SKPhysicsBody(bodies: [path, path2]) 但应用程序崩溃了。有什么帮助吗? 路径不是主体。您需要创建 2 个实体,然后使用它们来创建第三个实体。 @Fogmeister 正如文档中所写,它是一个 AnyObject 数组:init(bodies body: [AnyObject]) -> SKPhysicsBody 所以我不能将路径添加为数组元素? @Fogmeister 好的,我读了更多然后我明白了:bodys SKPhysicsBody 对象的数组。谢谢 @sangony 谢谢我解决了请检查我的答案如果您对我的答案有任何反馈,请随时告诉我,我很乐意改进我的代码。【参考方案2】:

好的,我解决了它:)

让 sprite = SKSpriteNode(imageNamed: "cell3")

sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))

let path = CGPathCreateMutable()

MoveToPoint(path, x: 51 , y:22 , node:sprite)
AddLineToPoint(path, x: 38 , y:45 , node:sprite)
AddLineToPoint(path, x: 32 , y:44 , node:sprite)
AddLineToPoint(path, x: 32 , y:0 , node:sprite)
AddLineToPoint(path, x: 39 , y:0 , node:sprite)

CGPathCloseSubpath(path)

let path2 = CGPathCreateMutable()

MoveToPoint(path2, x: 0 , y:22 , node:sprite)
AddLineToPoint(path2, x: 13 , y:0 , node:sprite)
AddLineToPoint(path2, x: 19 , y:0 , node:sprite)
AddLineToPoint(path2, x: 19 , y:45 , node:sprite)
AddLineToPoint(path2, x: 13 , y:45 , node:sprite)

CGPathCloseSubpath(path2)

let b1 = SKPhysicsBody(polygonFromPath: path)

let b2 = SKPhysicsBody(polygonFromPath: path2)

sprite.physicsBody = SKPhysicsBody(bodies: [b1, b2])

【讨论】:

以上是关于SKPhysics体路径生成器的主要内容,如果未能解决你的问题,请参考以下文章

PostgreSQL——查询优化——生成路径1

PostgreSQL——查询优化——生成路径1

Unity之自动生成预制体脚本

Unity之自动生成预制体脚本

kruskal:最小生成树

PostgreSQL——查询优化——生成路径2