Reputation: 318
I,m trying basically a rectangle with a hole in the new framework provided by apple, the only problem is that it the default method bodyWithPolygonFromPath does not accept concave polygons, so I tried to approach the problem as follows:
CGPathMoveToPoint (path, NULL, self.size.width/4.0, self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, self.size.width/2.0, -self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, -self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, self.size.height/4.0);
CGPathCloseSubpath (path);
self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
self.physicsBody.affectedByGravity = YES;
self.physicsBody.categoryBitMask = solidCategory;
self.physicsBody.dynamic = YES;
[self addChild:shape1];
self.auxiliaryShapeNode = [[SKSpriteNode alloc] init];
CGPathMoveToPoint (path_aux, NULL, 3*self.size.width/8.0, 0);
CGPathAddLineToPoint(path_aux, NULL, self.size.width/2.0, 1.5*self.size.height/4.0);
CGPathAddLineToPoint(path_aux, NULL, self.size.width/2.0, -self.size.height/4.0);
CGPathCloseSubpath (path_aux);
self.auxiliaryShapeNode.anchorPoint = CGPointMake(-3.0, 1.0/2.5);
self.auxiliaryShapeNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path_aux];
self.auxiliaryShapeNode.physicsBody.dynamic = YES;
self.auxiliaryShapeNode.physicsBody.categoryBitMask = solidCategory;
[self addChild:self.auxiliaryShapeNode];
[self.scene.physicsWorld addJoint:[SKPhysicsJointFixed jointWithBodyA:self.physicsBody bodyB:self.auxiliaryShapeNode.physicsBody anchor:self.position]];
break;
where self is a custom sprite node i created which has a SKSprite node as auxiliary body shape so the shape can be made by two convex polygons, the it creates the shapes as it is supposed to, but when the scene starts playing the joint doesn't work as it is supposed to and moves the little triangle way off where it started
Upvotes: 1
Views: 523
Reputation: 318
I solved my problem, I actually did not find the answer but i guess you could say I worked it around
What I realized is that the point of the Bodyjoint only obeyed to the x coordinate but always moved the y coordinate to 0, so I moved the body to the 0,0 coordinate, and the moved the body again to it´s previous position
CGPoint *prevPosition = self.position;
//positioning of the node so that the joint lands exactly 0,0
self.position = CGPointMake(-3*self.size.width/8.0,0);
CGPathMoveToPoint (path, NULL, self.size.width/4.0, self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, self.size.width/2.0, -self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, -self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, self.size.height/4.0);
CGPathCloseSubpath (path);
self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
self.physicsBody.affectedByGravity = YES;
self.physicsBody.categoryBitMask = solidCategory;
self.physicsBody.dynamic = YES;
[self addChild:shape1];
self.auxiliaryShapeNode = [[SKSpriteNode alloc] init];
//now we asssign the frame, the position does not matter as it is determined by the joint
self.auxiliaryShapeNode.frame = CGRectMake(0,0,sel.size.width/8.0,self.height/2);
self.auxiliaryShapeNode.anchor = CGPointMake(0,0.5);
CGPathMoveToPoint (path_aux, NULL, 0,0);
CGPathAddLineToPoint(path_aux, NULL, self.auxiliaryShapeNode.size.width, self.auxiliaryShapeNode.size.width/2.0);
CGPathAddLineToPoint(path_aux, NULL, self.auxiliaryShapeNode.size.width, -self.auxiliaryShapeNode.size.width/2.0);
CGPathCloseSubpath (path_aux);
self.auxiliaryShapeNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path_aux];
self.auxiliaryShapeNode.physicsBody.dynamic = YES;
self.auxiliaryShapeNode.physicsBody.categoryBitMask = solidCategory;
[self addChild:self.auxiliaryShapeNode];
[self.scene.physicsWorld addJoint:[SKPhysicsJointFixed jointWithBodyA:self.physicsBody bodyB:self.auxiliaryShapeNode.physicsBody anchor:CGPointZero]];
self.position=prevPosition;
so, I managed to "make it work" this way, hope it´s useful for someone
Upvotes: 0