SpriteKit: SKPhysicsBody slips away

104 views Asked by At

I have a SKSpriteNode (Bird) with a SKPhysicsBody (Circle). The bird node is affected by gravity and falls down, until it reaches a y position of 100px.

override func didSimulatePhysics() {   

        if (bird.position.y < 100) {

               bird.position = CGPointMake(bird.position.x, 100)
         }
}

The problem is, that the physicsBody is moving away from the Node. The physicsBody should stay centered on the Bird. As soon as I tap the screen the Bird gets an impulse and moves up. Immediately the physicsBody is back in place.

enter image description here

This is the physicsBody code:

bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.frame.width * 0.5)
bird.physicsBody!.contactTestBitMask = PhysicsCategory.LeftBoard | PhysicsCategory.RightBoard |  PhysicsCategory.Border
bird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
bird.physicsBody!.collisionBitMask = PhysicsCategory.None
bird.physicsBody!.mass = 1

bird.physicsBody!.affectedByGravity = false
bird.physicsBody!.allowsRotation = false
1

There are 1 answers

3
Epic Byte On BEST ANSWER

You need to stop the velocity. You are forcing the position but the velocity continues after each step.

Also you should typically make such changes in the update method before the physics are simulated.