1

我在苹果提供的新框架中基本上尝试了一个带孔的矩形,唯一的问题是它的默认方法 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 节点作为辅助身体形状,因此形状可以由两个凸多边形组成,它按预期创建形状,但是当场景开始播放关节时不会不能按预期工作,并将小三角形从开始的地方移开

4

1 回答 1

0

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

我意识到 Bodyjoint 的点只服从 x 坐标,但总是将 y 坐标移动到 0,所以我将 body 移动到 0,0 坐标,然后将 body 再次移动到之前的位置

    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;

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

于 2013-10-28T12:17:41.260 回答