2

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.

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44

1 Answers1

1

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.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • Unfortunately that won't change anything, the only thing you are doing is allowing more time between update calls. I really just want a fixed update, without having to recalculate based on delta time( because it is doubling the efforts at this point, wasting precious cycles). Basically the game should think that delta time is always 16.6666666 ms has happened. The way it is right now, even on a very fast device, delta time can vary by a few ms every update – Knight0fDragon Jul 11 '16 at 03:56
  • The only other thing I'm thinking is the Cocos2D sources (in detail the CCScheduler.m) where you can find details about the cocos2d update method and all the fixes apply to this one here: https://github.com/Maxim-Filimonov/conways_gol_ios/blob/master/ConwaysGOL.spritebuilder/Source/libs/cocos2d-iphone/cocos2d/CCScheduler.m – Alessandro Ornano Jul 11 '16 at 06:58
  • Cocoas2d is not sprite kit, i do not use cocoas 2d, the issue is not about cocoas 2d. The goal is to get SKPhysics working in a fixed update manner. (Technically if I can get Sprite Kit update to work in a true consistent manner, where dTime is 16.6666, then that would be golden) – Knight0fDragon Jul 11 '16 at 11:27
  • Sure I know, this code should be to think about a new idea, not to copy and paste obviusly.. – Alessandro Ornano Jul 11 '16 at 11:30
  • well the issue is it is 2 different frameworks, I am just going to chalk it up to it can't be done, maybe if SpriteKit source gets released, I could make my own SKView with a fixed update – Knight0fDragon Jul 11 '16 at 13:13
  • I hope you solve friend , your question is pretty tough and deserves attention. – Alessandro Ornano Jul 11 '16 at 13:23
  • 1
    yeah, in Unity, they have both style updates, maybe in the future it will be included in Sprite Kit, who knows – Knight0fDragon Jul 11 '16 at 13:24