I use the code listed below as an experiment of SpriteKit. What I do is applying the same amount of force as a gravity effects to it but with anti-parallel vector (mass of body is set to 1.0). The function is executed on each frame via update function. I expect the object not to move at all as applyForce and gravity compensate each other.
-(void)didMoveToView:(SKView *)view {
self.anchorPoint = CGPointMake(0.5, 0.5);
SKShapeNode *node = [SKShapeNode shapeNodeWithRect:CGRectMake(0,0,WIDTH, HEIGHT)];
node.position = CGPointMake(30, 280);
node.strokeColor = [SKColor whiteColor];
node.fillColor = [SKColor purpleColor];
node.name = @"node";
CGSize sz = node.frame.size;
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sz center:[GameScene Center]];
node.physicsBody.restitution = 0.3;
node.physicsBody.mass = 1.0;
node.physicsBody.affectedByGravity = YES;
self.figure = node;
[self addChild:node];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
}
-(void)update:(CFTimeInterval)currentTime {
[self.figure.physicsBody applyForce:CGVectorMake(
- self.physicsWorld.gravity.dx,
- self.physicsWorld.gravity.dy)];
However, the object falls down like it's effected by gravity only. I've checked the mass with a log it's always exactly 1.0
After some experiments, I've found out there is some magic number about it. It is 150. If use next update method the object is rested:
-(void)update:(CFTimeInterval)currentTime {
[self.figure.physicsBody applyForce:CGVectorMake(
- self.physicsWorld.gravity.dx,
-150 * self.physicsWorld.gravity.dy)];
And, another moment here, the mass is automatically calculated to be 1.0 if the body has dimensions 150*150. Anyway that wouldn't help. The behavior is same as we directly set the mass equal one.
If anyone knows what's going on here, please help!