创建凸SKPhisycsBodies

时间:2013-10-06 21:41:58

标签: objective-c sprite-kit skspritenode

我,我基本上尝试了一个由apple提供的新框架中有一个洞的矩形,唯一的问题是它默认方法bodyWithPolygonFromPath不接受凹多边形,所以我试着按如下方式解决问题:

        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;

其中self是我创建的自定义精灵节点,它有一个SKSprite节点作为辅助体形,因此形状可以由两个凸多边形构成,它创建的形状应该如此,但是当场景开始播放时关节不能正常工作,并将小三角形移动到它开始的位置

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,实际上我找不到答案,但我想你可以说我已经解决了这个问题

我意识到Bodyjoint的点只服从x坐标,但总是将y坐标移动到0,所以我将身体移动到0,0坐标,并将身体再次移动到它以前的职位

    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;

所以,我设法以这种方式“让它工作”,希望它对某人有用