cdasher
cdasher

Reputation: 3073

How to pin one SKPhysicBody to another without both falling

I am trying to join two SKPhysicsBodies together so that the one on top stays in place and the one below falls with gravity but is attached with a joint. Imagine two rectangles, one on top and one below. The one on top stays in place "floating" and the one below is attached with a joint and can move (swing perhaps or bounce etc)

When I attempt to create this, both nodes fall with gravity even if I set the affectedByGravity to NO.

self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

    self.physicsWorld.gravity = CGVectorMake(0, -9.8);


    SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"myFont"];

    myLabel.text = @"Hello, World!";
    myLabel.fontSize = 30;
    myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));

    myLabel.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:myLabel.frame.size];
    myLabel.physicsBody.affectedByGravity = NO;

     [self addChild:myLabel];



    SKLabelNode *myLabel2 = [SKLabelNode labelNodeWithFontNamed:@"myFont"];

    myLabel2.text = @"Hello, World!";
    myLabel2.fontSize = 30;
    myLabel2.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));

    myLabel2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:myLabel2.frame.size];


    [self addChild:myLabel2];


    CGPoint anchor = CGPointMake(CGRectGetMidX(self.frame),
                                 CGRectGetMidY(self.frame));
    SKPhysicsJointFixed* fixedJoint = [SKPhysicsJointFixed jointWithBodyA:myLabel.physicsBody
                                                                    bodyB:myLabel2.physicsBody
                                                                   anchor:anchor];
    [self.scene.physicsWorld addJoint:fixedJoint];

Upvotes: 0

Views: 200

Answers (1)

godel9
godel9

Reputation: 7390

You can set the dynamic property of the static body to NO, and that will cause the body to ignore all forces and impulses, including gravity.

Upvotes: 1

Related Questions