Element 84 Logo

Update: Comparing Sprite Kit Physics and Direct Box2D Simulation Times

12.04.2013

In a previous post I compared Sprite Kit physics to using Box2D directly. In that comparison I used frames per second as measured by Instruments, but it is useful to look at straight simulation time (ignoring rendering time), which I present here.

The code used to time the physics simulation in Sprite Kit is given here:

-(void)didEvaluateActions {
    physicsStartTime = [NSDate timeIntervalSinceReferenceDate];
    if (sampleStartTime == 0) {
        sampleStartTime = physicsStartTime;
    }
}

-(void)didSimulatePhysics {
    physicsEndTime = [NSDate timeIntervalSinceReferenceDate];
    frameCount++;

    simulationTime += (physicsEndTime - physicsStartTime);
    if (physicsEndTime - sampleStartTime > 1.0 - EPSILON) {
        NSLog(@"Average simulation time = %f", (simualtionTime / (double)frameCount));
        simulationTime = 0;
        sampleStartTime = 0;
        frameCount = 0;
    }

}

These are methods of SKScene and part of the Sprite Kit event loop. didEvaluateActions is called just before

and didSimulatePhysics is called just after the physics simulation. physicsStartTime, etc., are member variables on my SKScene implementation, and are intialized to zero at startup.

Again, the project used for these tests is available as a GitHub project. The tests were conducted on an iPhone 5, iOS 7.0.4. The project was built in Xcode 5 with the -Os optimization level (Fastest, Smallest). The Box2D version used for the simulations is version 2.2.1.

The only difference in these results from the previous post is that these provide physics simulation times (in milliseconds) directly instead of frames per second, which incorporates rendering time as well. The average simulation time is computed and output approximately once per second. This breaks down when the time for simulating one frame takes more than one second, but at that point the simulations are too slow to be useful in a game anyway.

The two tests shown here are the same as those shown in the previous post, namely, elastic and inelastic collisions involving small boxes. These tests were conducted using 50, 100, 150, 200, 250, 300, 350, and 400 boxes. The results are summarized in the following sections.

Test 1 – Elastic Collisions

This test involved boxes bouncing off the sides of the screen and each other. The collisions were elastic, meaning the colliding boxes don’t lose energy and thus never stop. See the previous post for screenshots and a better description.

The tow tables summarize the average simulation times for the first 16 seconds of each test run. Each row represents the samples at a given time in the test for all the test runs (50 boxes, 100 boxes, etc.).

The first table shows the results for the direct Box2D simulations.

Box2D (restitution = 1.0, friction = 0, angular damping = 0, linear damping = 0, gravity = 0)
 50 Boxes100 Boxes150 Boxes200 Boxes250 Boxes300 Boxes350 Boxes400 Boxes
SampleAvg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)
01.513.595.839.6320.4644.97289.07447.43
11.112.674.225.027.1111.0250.47590.1
21.112.444.14.857.629.9487.19452.48
31.082.514.114.666.59.0614.83410.12
41.032.563.994.626.328.8515.11397.3
51.042.463.944.656.479.0916.52367.85
61.032.464.054.676.599.3415.06349.34
71.042.473.964.616.538.7520.53348.67
81.042.453.944.546.298.7927.71339.41
91.062.443.954.596.398.8824.03332.53
101.022.453.974.466.338.7232.07341.02
111.012.453.964.486.248.8821.02352.85
121.022.623.894.446.288.9720.38320.34
131.062.433.894.496.318.7424.34328.89
141.052.423.994.496.358.820.94318.27
151.02.463.924.586.438.8418.54336.07

The simulation times look very good until at least 200 boxes, and for the most part look good up until 300 boxes.

The second table represents the test runs done using Sprite Kit physics.

Sprite Kit (restitution = 1.0, friction = 0, angular damping = 0, linear damping = 0, gravity = 0)
 50 Boxes100 Boxes150 Boxes200 Boxes250 Boxes300 Boxes350 Boxes400 Boxes
SampleAvg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)
03.055.2410.0318.2666.22227.92363.44602.84
12.223.946.579.3213.78259.071018.94401.5
22.173.876.519.2413.83324.82267.56323.8
32.093.826.399.2613.29482.621600.284591.79
42.213.986.349.1714.48924.93259.211029.01
52.124.06.378.2414.71150.152798.99545.44
62.214.786.528.5416.99952.84555.37755.65
72.094.976.438.2716.482221.62867.88688.72
82.194.956.428.2826.19364.391016.83598.14
92.185.26.438.5454.24424.59546.58981.87
102.145.056.488.63109.21422.96784.93821.71
112.175.16.498.61281.63293.37781.34940.54
122.125.156.478.81527.682449.71893.73633.11
132.125.146.58.9927.89385.01921.82901.76
142.135.146.579.15116.94334.61324.46717.27
152.175.416.59.31539.98412.183444.5743.81

The first thing to notice here is that the simulation times are about twice that of the direct Box2D implementation. The Sprite Kit physics simulations also tend to get unusable around 250 boxes.

Test 2 – Inelastic Collisions

This test involves inelastic collision (collisions where colliding objects lose energy and slow down after collisions). Of particular interest here is how the various APIs handle sleeping, which is when an object has slowed down enough that it is considered at rest, and no longer needs to be simulated (until something else collides with it). This can have a big impact on performance.

Here are the simulation times for the direct Box2D implementation:

Box2D (restitution = 0.1, friction = 0.1, angular damping = 0, linear damping = 0, gravity = -1.5)
 50 Boxes100 Boxes150 Boxes200 Boxes250 Boxes300 Boxes350 Boxes400 Boxes
SampleAvg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)
01.823.75.277.6612.0216.1518.9423.2
11.623.155.57.18.9412.1430.4829.96
21.753.497.48.0610.1912.3343.7623.68
31.743.377.758.0310.0411.2920.1216.64
41.753.527.5717.7713.3311.2520.7818.06
51.532.410.617.7426.2611.7731.3925.27
60.751.069.067.6110.1465.67168.4535.52
70.341.16.357.4410.5911.3415.215.17
80.321.072.862.9111.1612.197.414.81
90.291.072.232.2810.0711.327.194.85
100.31.12.252.299.9811.227.24.85
110.291.082.152.2410.0711.337.124.81
120.311.062.232.2910.043.717.24.85
130.31.092.162.2510.023.517.134.9
140.31.032.222.3210.093.427.144.85
150.31.072.272.2510.193.447.344.79

The performance is similar to that of the previous test (elastic collisions), but we see as the simulation progresses (and the falling boxes stack up), the simulation times decrease as most of the boxes go into sleeping mode.

Here are the simulation times for Sprite Kit physics

Sprite Kit (restitution = 0.1, friction = 0.1, angular damping = 0, linear damping = 0, gravity = -1.5)
 50 Boxes100 Boxes150 Boxes200 Boxes250 Boxes300 Boxes350 Boxes400 Boxes
SampleAvg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)Avg. Sim. Time (ms)
04.237.419.3944.3162.25161.1254.11337.22
13.275.728.2414.9719.76209.48633.941082.25
24.48.1812.3367.62302.71170.972063.56202.92
34.899.2914.68249.221062.41237.36620.792090.48
45.019.6714.75408.64268.711687.56373.34565.74
59.439.65173.02298.771617.6358.74306.87316.2
63.949.7520.44140.14221.32191.772786.223602.25
74.469.7514.83151.3750.64996.79421.87490.03
84.479.7114.84117.96963.63212.24426.74555.7
94.529.7314.8134.6965.39520.58428.43571.2
104.479.7514.85123.63968.13926.11303.45346.69
114.559.6914.97209.9966.78249.332749.4321.02
124.599.7414.76122.99972.01197.03543.962155.56
134.579.7514.99135.221004.77220.23420.95404.32
144.479.714.78187.51136.14952.87246.43319.09
154.449.7515.04110.89329.21837.712273.711696.03

The simulation times are already high by 150 boxes, and certainly unacceptable by 200 boxes. Again, the times are typically at least twice as long as those of the direct Box2D implementation. Another thing to notice is that the simulation time does not seem to benefit from sleeping boxes.

Conclusion

The data presented here makes it clearer that Sprite Kit physics does not perform as well as a direct implementation of Box2D. There may be many reasons for this (setting up collision handlers that aren’t being used, perhaps), but at face value it does not look good. Of particular concern is the seeming lack of response to sleeping objects. This coupled with the repeatabilty problems discussed in the previous post confirm my earlier conclusion that Sprite Kit physics is not quite ready for production use.