10 Replies
      Latest reply on May 4, 2020 1:52 PM by rikuvirta
      Fuzzygoat Level 1 Level 1 (20 points)

        Has anyone seen (or aware) of a why I am seeing none dynamic SKPhysicsBody objects in my scene randomly jumping down by a few points. This does not seem to happen on the device just the simulator, its also not a constant movement its little random jumps of a few pixels over quite long periods of time. Last light I left the simulator running after going to the train station to pick up my partner, when I got back the SKPhysicsBody platforms (had moved down by maybe 10 points. I did this agin and watched and the platforms stay staic and then all of a sudden the jump down by a couple of points then stop, this seems to be happening at intervals but I can't work out the trigger.

         

        This is some of the physics settings for the SKPhysicsBodys which are moving. (To my mind this body should not move)

         

        let physics = SKPhysicsBody(rectangleOfSize: self.size)
        physics.dynamic = false
        physics.allowsRotation = false
        physics.affectedByGravity = false
        self.physicsBody = physics
        
        

         

        Scene gravity was origianally set to a very low -0.25, but making it -10 did not speed up the drift so it does not seem to be gravity related.

         

        self.physicsWorld.gravity.dy = -10 //-0.25
        
        

         

        Xcode Version 7.2 beta (7C62b)

        • Re: None dynamic SKPhysicsBodys drifting down in simulator?
          pavelgubarev Level 2 Level 2 (40 points)

          Maybe something pushes that body? You can set collisiton detector and log any interactions with this sprite. At least you can rule out this version

          • Re: None dynamic SKPhysicsBodys drifting down in simulator?
            Floating Wrench Level 3 Level 3 (180 points)

            If there is a small amount of gravity, then anything affected should move down. The physics simulation is not deterministic. Small errors in position will accumulate over time, and I believe there may be small amounts of entropy added to the simulator to keep things active. This may be what you are seeing.

             

            If you truly want the items to stay still then I would suggest you make the bodies static or kinematic.

            • Re: None dynamic SKPhysicsBodys drifting down in simulator?
              Floating Wrench Level 3 Level 3 (180 points)

              Duh. Just noticed you mentioned these were static bodies, so.. interesting question.

              • Re: None dynamic SKPhysicsBodys drifting down in simulator?
                Fuzzygoat Level 1 Level 1 (20 points)

                Tested this again this morning and the drift is happening to non-dynamic SKPhysicsBody(s) that are parented to an SKNode. For example I have an organisational SKNode at the origin called "Platfom_Root", all my "Platforms" are parented to this node so I can quickly access them. The drift I am seeing only happens on the simulator and affects all of the "Platforms", if I parent the platforms to self (i.e. the SKScene) they do not drift.

                 

                    func dynamic(location: CGPoint, color: SKColor, parent: Bool) {
                        let spriteSize = CGSize(width: 70, height: 15)
                        let sprite = SKSpriteNode(color: color, size: spriteSize)
                        sprite.position = location
                      
                        let physics = SKPhysicsBody(rectangleOfSize: sprite.size)
                        physics.dynamic = false // BODY SHOULD NOT MOVE
                        sprite.physicsBody = physics
                        if parent {
                            self.treeRoot.addChild(sprite) // DRIFTS DOWN AND LEFT
                        } else {
                            self.addChild(sprite) // WORKS AS EXPECTED
                        }
                    }
                
                

                 

                I have put together a sample project and submitted it to Apple.

                 

                BUG 23687610

                • Re: None dynamic SKPhysicsBodys drifting down in simulator?
                  mperriard Level 1 Level 1 (0 points)

                  Yes, I'm seeing the exact same thing. All platforms and walls in my level are supposed to be static, but the physics bodies are moving slowly out of place.

                   

                  I can't see any reasonable explanation why it should happen, so I'm thinking it is a bug in spritekit. It is at least not what you expect. I tried setting dynamic=NO and pinned=YES, and other different combinations, but nothing seems to fix it.

                   

                  I read somewhere that you may need to reset all positions in didFinishUpdate, which is an ugly work-around and not great for performance, but might be the only way.

                  • Re: None dynamic SKPhysicsBodys drifting down in simulator?
                    CartoonSmart.com Level 3 Level 3 (280 points)

                    Never Trust the Simulator. N.T.t.S.  Its is not a replacement for testing on a device, especially when you start mixing in physics in a fast running game. 

                    • Re: None dynamic SKPhysicsBodys drifting down in simulator?
                      rikuvirta Level 1 Level 1 (0 points)

                      It's an old post, but still in 2020 this is a problem. Pinned and non-dynamic PhysicsBodies drift over time.

                       

                      In case someone is still searching for a solution, I have a workaround.

                       

                      Previously, I had an SKNode with two child SKShapeNodes with PhysicsBodies. These nodes would drift apart over time.

                       

                      Now I have an SKNode with two child SKShapeNodes with empty child nodes, and the PhysicsBodies are in the empty nodes. In addition to this, I generate the PhysicsBodies only when they are needed. In my case, I have a tile based platformer, and only a 9 by 9 grid of tiles around the player have PhysicsBodies. This way the lifespan of the PhysicsBody is short, and they won't drift.

                       

                      This has solved the problem for me.