simon91
simon91

Reputation: 41

SpriteKit game physics lagging

I have been working on a SpriteKit game for a while now and I have an annoying problem that I can't get rid of. I don't want to tell too much about the game, but it is very simple. I generate some objects (SKSpriteNodes) each second that are falling down from the top of the screen using SpriteKits physics and the player is interacting with them.

Most of the time the game runs perfectly (constant 60 FPS). The problem is that sometimes (maybe once per minute or something like that), the game starts to lag a little bit for about 3-5 seconds (still at 60 FPS) and then it runs perfectly again (note: i'm running the game on an iPhone 5s). It seems to be because of the physics, because if I add a normal move-action on an object, it runs very smoothly while the nodes affected by the physics are lagging.

I tried to remove some particles and effects that I have and I reuse my objects, but I can't remove the lag. I decided to create a very simple test project to see if the lag would be gone but it is still there. Here is the code:

#import "GameScene.h"

static const uint32_t groundCategory = 1 << 0;
static const uint32_t objectCategory = 1 << 1;

@implementation GameScene

-(void)didMoveToView:(SKView *)view
{
    // Init
    self.backgroundColor = [UIColor blackColor];
    self.physicsWorld.gravity = CGVectorMake(0.0f, -3.2f);
    self.physicsWorld.contactDelegate = self;

    // Ground
    SKPhysicsBody* ground = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(0, -88) toPoint:CGPointMake(self.size.width, -88)];
    ground.categoryBitMask = groundCategory;
    self.physicsBody = ground;

    // Start game loop
    SKAction* waitAction = [SKAction waitForDuration:1];
    SKAction* sequence = [SKAction sequence:@[[SKAction performSelector:@selector(addObject) onTarget:self], waitAction]];
    SKAction* repeatAction = [SKAction repeatActionForever:sequence];
    [self runAction:repeatAction withKey:@"fallingObjectsAction"];
}

- (void)addObject
{
    SKSpriteNode* newObject = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(88, 88)];
    newObject.name = @"Object";
    newObject.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:newObject.size];
    newObject.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height + newObject.size.height);
    newObject.physicsBody.categoryBitMask = objectCategory;
    newObject.physicsBody.contactTestBitMask = groundCategory;
    newObject.physicsBody.collisionBitMask = 0;

    [self addChild:newObject];
}

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    if ([contact.bodyA.node.name isEqualToString:@"Object"])
        [contact.bodyA.node removeFromParent];
    else if ([contact.bodyB.node.name isEqualToString:@"Object"])
        [contact.bodyB.node removeFromParent];
}

@end

I generate one object each second and let it fall down from the top of the screen. When it hits the ground, it is removed.

Am I using the physics wrong or is it SpriteKit's fault that it is lagging? It seems strange, because I'm running a very simple project using an iPhone 5s with iOS 8.

Upvotes: 4

Views: 2337

Answers (1)

Kami Dojin
Kami Dojin

Reputation: 41

Okay I've been having the same issue with physics. The issue was objects are jittery despite low CPU usage and a consistent FPS. I've finally figured out why. The solution I found is do not set physicsWorld.speed to 1.0; set it to a .9999. Now everything runs smoothly.

I hope this helps.

Upvotes: 3

Related Questions