Search code examples
swiftsprite-kitskphysicsbody

SpriteKit Sprites slightly moving when isDynamic is false


I am using Swift 3 and Sprite Kit to develop a game. I have a grid of dots, that shouldn't move, but are important to detect collisions. My problem is, that the dots are slightly moving. You cannot see it moving, but if I print the positions, I see they do. This is how I create the dots:

for dot in dots {
        //dot subclasses SKSpriteNode
        dot.name = "dot\(index)"
        dot.size = CGSize(width: DotWidth, height: DotHeight)
        dot.position = pointFor(column: dot.column, row: dot.row)
        dotsLayer.addChild(dot)
        //dot.sprite = sprite
        dot.physicsBody = SKPhysicsBody(circleOfRadius: 2)
        dot.physicsBody?.affectedByGravity = false
        dot.physicsBody?.allowsRotation = false
        dot.physicsBody?.isDynamic = false

        dot.physicsBody?.categoryBitMask = PhysicsCategory.Dot
        dot.physicsBody?.contactTestBitMask = PhysicsCategory.Stick
        dot.physicsBody?.collisionBitMask = PhysicsCategory.None
        dot.physicsBody?.usesPreciseCollisionDetection = true
        index += 1

    }

I tried a few things, like affectedByGravity (even though there is no gravity), and allowsRotation. If I print the position of one dot (subclass of SKSpriteNode), I get Optional((47.9458236694336, 47.9458236694336)), but if I print the position of the same dot a bit later, I get Optional((47.86669921875, 79.892448425293)). And the longer I wait, the bigger the difference. If I turn on showsPhysics, I notice the dots go down and to the left. Is there a way to not move the dots at all?


Solution

  • It seems that my problem only exists when running the app in a simulator. Even with the latest Xcode simulators (Xcode 8.1). It doesn't matter which simulator I use (iPhone 5, iPhone 6, etc.). Running the same app on a real device, doesn't show the shifting problem. The 'dots' stay on the exact same positions as when created. So it seems to be a simulator bug.