I created an SKSpriteNode called let say Map , which has an edge path that I defined (some simple polygon shape).
I am trying to figure out how to add a few other paths that will act as the inner edges of the Map . As if the “map” as a whole really had holes. Some forms of the inner border that can work with the Map as a whole: one path to the edge (as shown below)
©
I understand that there is a method that allows you to create SKPhysicsBody with bodies (some NSArray), for example,
Map.physicsBody = [SKPhysicsBody bodyWithBodies:bodiesArray];
Does this method really generate what I showed in the image? Assuming bodiesArray contains 3 SKSpriteNode each with a specific path from using this method:
+ (SKPhysicsBody *)bodyWithEdgeChainFromPath:(CGPathRef)path
creating a path like
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];
I understand that an alternative would be to create 3 separate nodes with the location and shape of the alleged “holes”, but I am attached to avoid creating more nodes than I need. If someone can confirm what I'm trying to do, that’s right, or maybe offer an alternative that I don’t know about.
NOTE : IF what I am doing is correct, but I am missing something, I would appreciate if someone could show me the correct way to do what I am trying to do (even a simple example of a square with an inner smaller square was would be great). Thanks!
EDIT 1 : Below is a snippet of code that I use as an attempt to create “inner borders”. The problem here is that when both the outer and inner rectangles are drawn and displayed, when I add the inner rectangle to the Map bodyWithBodies , it fully controls collision detection, removing all contact management from the outer straight shell. When I delete bodyWithBodies , it returns to its normal state, showing both rectangles, the outer one has collision detection (does not allow me to go through), while the inner one has nothing ... so close
// 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]; }
EDIT 2 This approach works. Just adding a new node with the same properties as the outer rectangle:
SKPhysicsBody *innerRectBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:innerRect]; innerRectBody.collisionBitMask = playerCategory; innerRectBody.categoryBitMask = wallCategory; SKNode *innerBoundary = [SKNode node]; innerBoundary.physicsBody = innerRectBody; [myWorld addChild: innerBoundary];
... but I would really like a cleaner solution that does not require additional nodes. thoughts?