Knight0fDragon
Knight0fDragon

Reputation: 16827

Sprite Kit Physics: possible to use a fixed update?

I have a scenario where I have a fixed update happening in my scene.

Every update, I would like to create a shadow, using the sprites previous position.

func update(currentTime: NSTimeInterval)
{
    shadow.position = sprite.position
}

Now when any I apply an impulse to the sprite, I want the sprite to increment the same number of steps regardless of how much time was actually used to get to the next update frame.

However, this is not the case, as when comparing the shadow on my simulator to the shadow on my device, the distance between the two is very different.

E.G. It took my phone 1 second to move the sprite 60 steps It took my simulator 2 seconds to move the sprite 60 steps

In the fixed update world. both took 60 frames to achieve, the simulator just lagged.

The actual results however will put the simulator at 120 steps

I tried disabling asynchronous on the SKView in hopes that would get me fixed updates, but that got me nowhere, so I am asking if anybody knows how to get SKPhysics to work on a fixed update framework.

Upvotes: 2

Views: 445

Answers (1)

Alessandro Ornano
Alessandro Ornano

Reputation: 35382

I'm not sure if it can be useful for you, I'm pretty sure you know this parameter, but maybe you have not noticed:

if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsPhysics = true
            skView.showsNodeCount = true

            skView.frameInterval = 1 // real devices
            #if (arch(i386) || arch(x86_64)) && os(iOS)
                skView.frameInterval = 2  // <- this one
            #endif 

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true
            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .ResizeFill
            skView.presentScene(scene)
        }

The sources:

    /* Number of hardware vsyncs between callbacks, same behaviour as CADisplayLink. Defaults to 1 (render every vsync) */
        public var frameInterval: Int

The docs:

This is the official Apple document that say:

The default value is 1, which results in your game being notified at the refresh rate of the display. If the value is set to a value larger than 1, the display link notifies your game at a fraction of the native refresh rate. For example, setting the interval to 2 causes the scene to be called every other frame, providing half the frame rate.

Setting this value to less than 1 results in undefined behavior and is a programmer error.

Upvotes: 1

Related Questions