【问题标题】:Spritekit SKPhysicsBody pin jointsSpritekit SKPhysics车身销接头
【发布时间】:2015-07-29 08:20:39
【问题描述】:

我正在尝试创建一个摩天轮。它由三个不同的精灵组成:底座(腿)、身体(旋转的圆形部分)和座椅(共 16 个)。

在下面的代码中,我将底座添加到场景中,然后将车身附加到底座上,最后将每个座椅附加到车身上。座椅目前正在(以一种奇怪的方式)随着身体旋转。我根本不希望座位旋转。我做错了什么?

注意:更新了来自@dragoneye 的 cmets 和建议

-(void) initFerrisWheel {
    self.physicsWorld.gravity = CGVectorMake(0, 0);

    //creates the body of the ferris wheel and attaches it to the base
    SKSpriteNode *ferrisWheel = (SKSpriteNode*)[self childNodeWithName:@"ferriswheel_base"];
    SKTexture *bodyTexture = [[SharedAssetsManager sharedData].menuAssets objectForKey:@"ferriswheel_body"];
    SKSpriteNode *body = [[SKSpriteNode alloc] initWithTexture:bodyTexture];
    [body setZPosition:2];
    [body setPosition:CGPointMake(0, 55)];
    body.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.height/2];
    [ferrisWheel addChild:body];

    //rotates the body of the ferris wheel
    SKAction *rotateSequence = [SKAction rotateByAngle:-0.785 duration:2.0];
    SKAction *rotateFinal = [SKAction repeatActionForever:rotateSequence];
    [body runAction:rotateFinal];

    SKTexture *seatTexture = [[SharedAssetsManager sharedData].menuAssets objectForKey:@"ferriswheel_seat"];

    int r = 130; //radius of the body for the ferris wheel
    for (int i = 0; i < 16; i++) {
        //creates a seat and places it on the body
        SKSpriteNode *seat = [[SKSpriteNode alloc] initWithTexture:seatTexture];
        float x = r * cosf(2*M_PI*i/16);
        float y = r * sinf(2*M_PI*i/16);
        [seat setPosition:CGPointMake(x, y)];
        [seat setZPosition:3];
        [body addChild:seat];

        //physics body stuff
        seat.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:seat.size.height/2];
        seat.physicsBody.dynamic = YES;

        CGPoint anchor = CGPointMake(x, y);
        SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:body.physicsBody bodyB:seat.physicsBody anchor:anchor];
        [self.physicsWorld addJoint:pin];
        pin.shouldEnableLimits = YES;
        pin.lowerAngleLimit = -0.25;
        pin.upperAngleLimit = 0.25;

    }
}

【问题讨论】:

  • 您可以将节点设置为不旋转:seat.physicsBody.allowsRotation = false

标签: macos sprite-kit skphysicsbody skphysicsjoint


【解决方案1】:

为例如设置关节上限和下限

pinJoint.shouldEnableLimits=TRUE;

pinJoint.lowerAngleLimit = -0.25;

pinJoint.upperAngleLimit = 0.25;

【讨论】:

  • 它没有改变任何东西。周围的座椅没有围绕它们所连接的身体部位正确旋转。它们像以前一样在旋转,但非常缓慢。
【解决方案2】:

经过一段时间的努力并创建了一个简单的测试项目后,我意识到我的问题是我将摩天轮的主体作为子节点连接到底座上,并且我还将座椅连接到了主体上作为子节点。虽然可能有办法让它工作,但我将所有坐标都基于世界坐标。这可能会在将来帮助某人。这是固定的代码:

-(void) initFerrisWheel {
    //creates the body of the ferris wheel and attaches it to the base
    SKSpriteNode *ferrisWheel = (SKSpriteNode*)[self childNodeWithName:@"ferriswheel_base"];
    SKTexture *bodyTexture = [[SharedAssetsManager sharedData].menuAssets objectForKey:@"ferriswheel_body"];
    SKSpriteNode *body = [[SKSpriteNode alloc] initWithTexture:bodyTexture];
    [body setZPosition:2];
    [body setPosition:CGPointMake(ferrisWheel.position.x, ferrisWheel.position.y + 55)];
    body.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:body.size];
    body.physicsBody.affectedByGravity = NO;
    [self addChild:body];

    //rotates the body of the ferris wheel
    SKAction *rotateSequence = [SKAction rotateByAngle:-0.785 duration:2.0];
    SKAction *rotateFinal = [SKAction repeatActionForever:rotateSequence];
    [body runAction:rotateFinal];

    SKTexture *seatTexture = [[SharedAssetsManager sharedData].menuAssets objectForKey:@"ferriswheel_seat"];

    int r = 130; //radius of the body for the ferris wheel
    for (int i = 0; i < 16; i++) {
        //creates a seat and places it on the body
        SKSpriteNode *seat = [[SKSpriteNode alloc] initWithTexture:seatTexture];
        float x = body.position.x + r * cosf(2*M_PI*i/16);
        float y = body.position.y + r * sinf(2*M_PI*i/16);
        [seat setPosition:CGPointMake(x, y)];
        [seat setZPosition:3];
        [self addChild:seat];

        //physics body stuff
        seat.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:seat.size];
        seat.physicsBody.affectedByGravity = NO;

        SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:body.physicsBody bodyB:seat.physicsBody anchor:seat.position];
        [self.physicsWorld addJoint:pin];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多