Note that there are some explanatory texts on larger screens.

plurals
  1. POSpriteKit gravity confusion
    primarykey
    data
    text
    <p>I have the following SpriteKit code:</p> <pre><code>scene.physicsWorld.gravity = CGVectorMake(0, -10); ... - (void)update:(CFTimeInterval)currentTime { [node.physicsBody applyForce:CGVectorMake(0, 10 * node.physicsBody.mass)]; } </code></pre> <p>Why doesn't the applyForce balance out the gravity? The node falls downwards quite quickly.</p> <p>Full example:</p> <pre><code>@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 </code></pre> <p>EDIT:</p> <p>Looks like it's out by a factor of 150. This seems to work:</p> <pre><code>self.physicsWorld.gravity = CGVectorMake(0, -10.0/150.0); </code></pre> <p>Why would this be the case?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload