SKPhysicsJointPin拉伸

时间:2014-12-28 01:48:39

标签: ios sprite-kit skphysicsjoint

我尝试了here发布的解决方案,但它没有解决问题。没有人回应我的请求,因为这个问题已被标记为已回答。

我让玩家跳过屏幕抓一根绳子。通过在绳索段和玩家之间建立SKPhysicsJointPin来实现抓取。绳索本身由许多用SKPhysicsJointPin s相互连接的段组成。那些表现如预期。然而,玩家似乎加入了大约一秒钟,但随着玩家和绳索一起摆动,他们之间的关节伸展开来,玩家完全脱离屏幕。

这是玩家(猴子)被添加到场景的地方:

- (void)addMonkeyToWorld
{
    SKSpriteNode *monkeySpriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"Monkey"];

    // Basic properties
    monkeySpriteNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:monkeySpriteNode.size];
    monkeySpriteNode.physicsBody.density = physicsParameters.monkeyDensity;
    monkeySpriteNode.physicsBody.restitution = physicsParameters.monkeyRestitution;
    monkeySpriteNode.physicsBody.linearDamping = physicsParameters.monkeyLinearDamping;
    monkeySpriteNode.physicsBody.angularDamping = physicsParameters.monkeyAngularDamping;
    monkeySpriteNode.physicsBody.velocity = physicsParameters.monkeyInitialVelocity;

    // Collision properties
    monkeySpriteNode.physicsBody.categoryBitMask = monkeyCategory;
    monkeySpriteNode.physicsBody.contactTestBitMask = ropeCategory;
    monkeySpriteNode.physicsBody.collisionBitMask = 0x0;
    monkeySpriteNode.physicsBody.usesPreciseCollisionDetection = YES;
} 

以下是联系事件的整理地点:

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    // Sort which bodies are which
    SKPhysicsBody *firstBody, *secondBody;
    if (contact.bodyA.categoryBitMask > contact.bodyB.categoryBitMask) {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }

    // Verify that the two bodies were the monkey and rope, then handle collision
    if ((firstBody.categoryBitMask & ropeCategory) != 0 && (secondBody.categoryBitMask & monkeyCategory) != 0)
    {
        [self monkey:secondBody didCollideWithRope:firstBody atPoint:contact.contactPoint];
    }
}

这里是玩家和绳子之间添加关节的地方:

- (void)monkey:(SKPhysicsBody *)monkeyPhysicsBody didCollideWithRope:(SKPhysicsBody *)ropePhysicsBody atPoint:(CGPoint)contactPoint
{
    if (monkeyPhysicsBody.joints.count == 0) {
        // Create a new joint between the monkey and the rope segment
        CGPoint convertedMonkeyPosition  = CGPointMake(monkeyPhysicsBody.node.position.x + sceneWidth/2., monkeyPhysicsBody.node.position.y + sceneHeight/2.);
        CGPoint convertedRopePosition = CGPointMake(ropePhysicsBody.node.position.x + sceneWidth/2., ropePhysicsBody.node.position.y + sceneHeight/2.);
        CGFloat leftMostX = convertedMonkeyPosition.x < convertedRopePosition.x ? convertedMonkeyPosition.x : convertedRopePosition.x;
        CGFloat bottomMostY = convertedMonkeyPosition.y < convertedRopePosition.y ? convertedMonkeyPosition.y : convertedRopePosition.y;
        CGPoint midPointMonkeyAndRope = CGPointMake(leftMostX + fabsf(ropePhysicsBody.node.position.x - monkeyPhysicsBody.node.position.x) / 2.,
                                                bottomMostY + fabsf(ropePhysicsBody.node.position.y - monkeyPhysicsBody.node.position.y) / 2.);
        SKPhysicsJointPin *jointPin = [SKPhysicsJointPin jointWithBodyA:monkeyPhysicsBody bodyB:ropePhysicsBody anchor:midPointMonkeyAndRope]; // FIXME: Monkey-rope joint going to weird position
        jointPin.upperAngleLimit = M_PI/4;
        jointPin.shouldEnableLimits = YES;
        [self.scene.physicsWorld addJoint:jointPin];
    }
}

任何会导致SKPhysicsJointPin伸展的想法?

1 个答案:

答案 0 :(得分:0)

这个问题与我发布的here另一个问题有关。我会在这里为任何一个人重复这个答案。

我在GameScene.m中添加了以下便捷方法。

-(CGPoint)convertSceneToFrameCoordinates:(CGPoint)scenePoint
{
    CGFloat xDiff = myWorld.position.x - self.position.x;
    CGFloat yDiff = myWorld.position.y - self.position.y;
    return CGPointMake(scenePoint.x + self.frame.size.width/2 + xDiff, scenePoint.y + self.frame.size.height/2 + yDiff);
}

我使用此方法添加关节。它处理需要处理的所有坐标系转换,导致此问题中出现的问题。例如,我添加关节的方式

    CGPoint convertedRopePosition = [self convertSceneToFrameCoordinates:ropePhysicsBody.node.position];
    SKPhysicsJointPin *jointPin = [SKPhysicsJointPin jointWithBodyA:monkeyPhysicsBody bodyB:ropePhysicsBody anchor:convertedRopePosition];
    jointPin.upperAngleLimit = M_PI/4;
    jointPin.shouldEnableLimits = YES;
    [self.scene.physicsWorld addJoint:jointPin];