1

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

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
heczaco
  • 198
  • 1
  • 13

1 Answers1

0

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

heczaco
  • 198
  • 1
  • 13
  • Did you get memory leaks using this code? I'm creating physics bodies with polygons form path and Instruments shows me strange memory leaks in PhysicsKit (responsible frame contains info about PKPoint objects). My implementation looks very similar to yours, but I don't have joints and after creating body I'm calling `CGPathRelease(path)`. I wonder if this is a bug in Sprite Kit or I'm doing something wrong. – Darrarski Nov 22 '13 at 00:24
  • I forgot to add that when I'm creating bodies using `[SKPhysicsBody bodyWithCircleOfRadius:radius]` or `[SKPhysicsBody bodyWithRectangleOfSize:size]` methods I don't get memory leaks at all. – Darrarski Nov 22 '13 at 00:28
  • hmm... maybe you should share your code, I haven't detected any memory leak, and it still works pretty nice... perhaps it has something to do with the complexity of your polygon, as the most complex that i've used is a trapezium, but if you share your code i'll be glad to help if i can – heczaco Nov 25 '13 at 21:50
  • No problem, you can check my question: http://stackoverflow.com/questions/20134891/skphycisbody-bodywithpolygonfrompath-memory-leaks - it contains my code that produces memory leaks inside PhysicsKit. – Darrarski Nov 25 '13 at 22:44