【问题标题】:creating convex SKPhisycsBodies创建凸 SKPhisycsBodies
【发布时间】:2013-10-13 09:42:03
【问题描述】:

我在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 节点作为辅助身体形状,因此形状可以由两个凸多边形组成,它按预期创建形状,但是当场景开始播放时关节没有按预期工作,并将小三角形从它开始的地方移开

【问题讨论】:

    标签: objective-c sprite-kit skspritenode


    【解决方案1】:

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

    我意识到 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;
    

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

    【讨论】:

    • 您是否使用此代码导致内存泄漏?我正在使用多边形路径创建物理体,仪器向我展示了 PhysicsKit 中的奇怪内存泄漏(负责框架包含有关 PKPoint 对象的信息)。我的实现看起来和你的很相似,但是我没有关节,在创建身体后我打电话给CGPathRelease(path)。我想知道这是 Sprite Kit 中的错误还是我做错了什么。
    • 我忘了补充一点,当我使用 [SKPhysicsBody bodyWithCircleOfRadius:radius][SKPhysicsBody bodyWithRectangleOfSize:size] 方法创建主体时,我根本不会发生内存泄漏。
    • 嗯...也许你应该分享你的代码,我没有检测到任何内存泄漏,它仍然工作得很好......也许它与你的多边形的复杂性有关,因为我用过的最复杂的是梯形,但如果你分享你的代码,我会很乐意提供帮助
    • 没问题,您可以查看我的问题:stackoverflow.com/questions/20134891/… - 它包含我的代码,会在 PhysicsKit 中产生内存泄漏。
    猜你喜欢
    • 2016-05-01
    • 2013-03-24
    • 2016-10-21
    • 2019-01-21
    • 1970-01-01
    • 2014-04-02
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多