gpt4 book ai didi

ios - SpriteKit SKPhysicsBody 与内边缘

转载 作者:可可西里 更新时间:2023-11-01 04:50:31 24 4
gpt4 key购买 nike

我创建了一个名为 MapSKSpriteNode,它具有我定义的边缘路径(一些简单的多边形形状)。

我想弄清楚的是如何添加几条 other 边缘路径,这些路径将充当 Map 的内部边缘。好像整个“ map ”确实有漏洞。某种可以与Map整体作用的内部边界形状:一条边路径(如下图)

enter image description here©

我知道有一种方法允许创建一个带有主体(一些 NSArray)的 SKPhysicsBody,就像这样

Map.physicsBody = [SKPhysicsBody bodyWithBodies:bodiesArray];

这种方法实际上会生成我在图像中显示的内容吗?假设 bodiesArray 包含 3 个 SKSpriteNode,每个都有使用这种方法定义的路径:

+ (SKPhysicsBody *)bodyWithEdgeChainFromPath:(CGPathRef)path

,创建这样的路径

        SKSpriteNode *innerNode1 = [SKSpriteNode spriteNodeWithImageNamed:@"map"];

CGMutablePathRef innerNode1Path = CGPathCreateMutable();

CGPathMoveToPoint(mapPath, NULL, 1110, 1110);
CGPathAddLineToPoint(mapPath, NULL, <some x1>, <some y1>);
CGPathAddLineToPoint(mapPath, NULL, <some x2>, <some y2>);
CGPathAddLineToPoint(mapPath, NULL, <some x3>, <some y3>);
.
.
.
CGPathCloseSubpath(mapPath);

innerNode1.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:innerNode1Path];
[bodiesArray addObject:innerNode1];

// Repeat for other 2 nodes

我知道另一种方法是创建 3 个具有预期“孔”的位置和形状的独立节点,但我尽量避免创建超出我需要的节点。如果有人可以确认我正在尝试做的事情是正确的,或者可能提出我不知道的替代方案。

注意:如果我做的是正确的,但我遗漏了一些东西,如果有人能告诉我正确的方法来做我想做的事情(即使是一个简单的例子),我将不胜感激带有内部较小正方形的正方形会很棒)。谢谢!

编辑 1:下面是我用来尝试创建“内部边界”的代码片段。这里的问题是,虽然绘制和显示了外部矩形和内部矩形,但当我将内部矩形添加到 Map bodyWithBodies 时,它完全控制了碰撞检测,从中删除了所有接触控制外矩形外壳。当我删除 bodyWithBodies 时,它恢复正常并显示两个矩形,外部有碰撞检测(不允许我通过),而内部没有任何东西......如此接近

// 1 Create large outer shell Map
CGRect mapWithRect = CGRectMake(map.frame.origin.x + offsetX, map.frame.origin.y + offsetY, map.frame.size.width * shrinkage, map.frame.size.height * shrinkage);

self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);
self.physicsWorld.contactDelegate = self;

// 2 Create smaller inner boundary
CGRect innerRect = CGRectMake(100, 100, 300, 300);
SKPhysicsBody *body = [SKPhysicsBody bodyWithEdgeLoopFromRect:innerRect];
body.categoryBitMask = wallCategory;
NSArray *bodyArray = [NSArray arrayWithObject:body];

// 3 Add bodies to main Map body
myWorld.physicsBody = [SKPhysicsBody bodyWithBodies:bodyArray];
myWorld.physicsBody.categoryBitMask = wallCategory;


if ( [[levelDict objectForKey:@"DebugBorder"] boolValue] == YES) {
// This will draw the boundaries for visual reference during testing
[self debugPath:mapWithRect];
[self debugPath:innerRect];
}

编辑 2这种方法有效..只需添加一个与外部矩形具有相同属性的新节点:

SKPhysicsBody *innerRectBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:innerRect];
innerRectBody.collisionBitMask = playerCategory;
innerRectBody.categoryBitMask = wallCategory;
SKNode *innerBoundary = [SKNode node];
innerBoundary.physicsBody = innerRectBody;
[myWorld addChild: innerBoundary];

...但我非常想要一个不需要额外节点的更简洁的解决方案..想法?

最佳答案

你在这里没有做错我来了一个例子,我用两个物理体创建了两个边缘矩形体

//使用gcd一段时间后添加物体

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self addBodyA];
});

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self addBodyB];
});
-(void)addBodyB
{
SKSpriteNode *node=[SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)];
node.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:node.frame.size];
node.position=CGPointMake(550, 420);
node.physicsBody.restitution=1;
[self addChild:node];

}
-(void)addBodyA
{
SKSpriteNode *node=[SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)];
node.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:node.frame.size];
node.position=CGPointMake(400, 420);
node.physicsBody.restitution=1;
[self addChild:node];

}
-(void)addEdgesBodies
{
SKAction *r=[SKAction rotateByAngle:1.0/60 duration:1.0/60];

SKSpriteNode *rect=[SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(300,300)];
rect.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:rect.frame];
rect.position=CGPointMake(500, 400);
[self addChild:rect];


//
SKSpriteNode *rect1=[SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(100,100)];
rect1.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:rect1.frame];
rect1.position=CGPointMake(550, 450);
[self addChild:rect1];


[rect1 runAction:[SKAction repeatActionForever:r]];

}
[self addEdgesBodies];

请记住边体的 CPU 开销很低,所以在您的多边形没有那么多边之前不要担心性能。

关于ios - SpriteKit SKPhysicsBody 与内边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com