None dynamic SKPhysicsBodys drifting down in simulator?

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)

Replies

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 🙂

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.

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

Its certainly not getting hit, nor does it have anything either passive or active resting on it. I will try and narrow down whats happening and get back with what I find.

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

For the record this bug is still occurring (at least on OS X 10.11.2 running Xcode 7.2). This is a massive bug. We will need to redesign our entire game's platform system because everything slowly shifts down over time. Still looking for a workaround. Very disappointed.

This bug seems to be still active on OS X(version 10.11.5) and Xcode 7.0. Exactly the same thing as the original poster described, if the SKSpriteNode is attached to SKNode and it has SKPhysicsBody, it keeps falling very slowly. I am surprised that not more people are reporting this.

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.

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.

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.