3
votes

I have the following SpriteKit code:

scene.physicsWorld.gravity = CGVectorMake(0, -10);
...
- (void)update:(CFTimeInterval)currentTime
{
    [node.physicsBody applyForce:CGVectorMake(0, 10 * node.physicsBody.mass)];
}

Why doesn't the applyForce balance out the gravity? The node falls downwards quite quickly.

Full example:

@interface HelloScene()

@property SKLabelNode *node;

@end

@implementation HelloScene

- (void)didMoveToView:(SKView *)view
{
    self.backgroundColor = [SKColor blueColor];
    self.scaleMode = SKSceneScaleModeAspectFit;
    self.physicsWorld.gravity = CGVectorMake(0, -10);
    [self addChild:[self newHelloNode]];
}

- (SKNode *)newHelloNode
{
    self.node = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    self.node.text = @"Hello, World!";
    self.node.fontSize = 42;
    self.node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    self.node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10.0];
    self.node.physicsBody.mass = 1.0;
    self.node.physicsBody.angularDamping = 0.0;
    self.node.physicsBody.linearDamping = 0.0;
    self.node.physicsBody.friction = 0.0;
    return self.node;
}

- (void)update:(NSTimeInterval)currentTime
{
    [self.node.physicsBody applyForce:CGVectorMake(0, 10 * self.node.physicsBody.mass)];
}

@end

EDIT:

Looks like it's out by a factor of 150. This seems to work:

self.physicsWorld.gravity = CGVectorMake(0, -10.0/150.0);

Why would this be the case?

1

1 Answers

1
votes

Looks like @mitchellallison has done some experimenting and found it was due to a pixel-to-metre ratio. See applyForce(0, 400) - SpriteKit inconsistency