1

The following code works great. It creates a dangling rope made of square links, and then applies a force to the bottom link to swing it around. It is executed in the SKScene object when the SKScene is created.

However, if you change the force on the last line from 2000 to 4000, the physics go haywire, and the links sometimes break. Sometimes the links seem to be vibrating or they’ll just submit entirely to gravity as if there is no joint. Additionally, if you have other physics bodies, they no longer collide with the haywire links, even if they did before the force was applied.

Although 4000 seems excessive, I’ve encountered this issue with much less force on lighter objects, or smaller forces on ropes with less of a gap between links.

Am I doing something wrong? Is there a bug in Sprite Kit or some other explanation? I couldn’t find any answers online.

It also doesn't matter if the force is applied during SKScene creation or delayed until later.

self.scaleMode = SKSceneScaleModeAspectFit;
self.anchorPoint = CGPointMake(0.5, 0.5);

CGFloat length = 20;
CGFloat gapMult = 1.8;
int numBoxes = 10;

SKSpriteNode *prevBox = nil;
for (int i = 0; i < numBoxes; i++) {
    SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(length, length)];
    box.position = CGPointMake(0, -i * length * gapMult);
    [self addChild:box];

    box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(length, length)];
    if (i == 0) {
        box.physicsBody.dynamic = NO;
    } else {
        CGPoint pt1 = CGPointMake(0, prevBox.position.y - length / 2);
        CGPoint pt2 = CGPointMake(0, box.position.y + length / 2);

        SKPhysicsJointLimit *joint = [SKPhysicsJointLimit jointWithBodyA:prevBox.physicsBody bodyB:box.physicsBody anchorA:pt1 anchorB:pt2];
        [self.physicsWorld addJoint:joint];
    }

    prevBox = box;
}

[prevBox.physicsBody applyForce:CGVectorMake(2000, 0)];

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.