Siriss
Siriss

Reputation: 3777

SKPhysicsBody inverted after Xcode 6.2 iOS 8.2 upgrade

EDIT: This is still broken in the 6.3/8.3 update. Anyone have a fix?

Prior to Xcode 6.2/iOS 8.2, this question fixed my problem. Now my SKPhysicsBodies are upside down again after I reload the sprite, or create a new SKPhysicsBody from a previously "used" texture. This is a big problem for my game as it messes up my whole game play.

Here is where I load the textures in init:

@property (strong, nonatomic) SKTextureAtlas *allTextures;
    - (id)initWithSize:(CGSize)size
    {
        self = [super initWithSize:size];
        if (self)
        {
            self.allTextures = [SKTextureAtlas atlasNamed:@"AllTextures"];
            [SKTextureAtlas preloadTextureAtlases:@[ self.allTextures ] withCompletionHandler:^{
                          NSLog(@"ATLASES LOADED");
            }];
        }
        return self;
    }

Here is where I create the Sprite and SKPhysicsBody:

// object is setup in @implementation as SKSpriteNode *object;

- (void)setupObject
{
    object = [SKSpriteNode spriteNodeWithTexture:[self.allTextures textureNamed:@"Object01"]];
    object.position = CGPointMake(self.size.height / 2, object.size.width + 20);
    object.name = kObjectName;
    object.zPosition = 1;
    object.physicsBody = [self setupObjectPhysicsBody];
    [worldNode addChild:object];
}

- (SKPhysicsBody *)setupObjectPhysicsBody
{
    SKPhysicsBody *objectBody = [SKPhysicsBody bodyWithTexture:object.texture size:object.size];
    objectBody.affectedByGravity = YES;
    objectBody.dynamic = YES;
    objectBody.mass = 1.0;
    objectBody.restitution = 0.0;
    objectBody.allowsRotation = NO;
    objectBody.categoryBitMask = objectCategory;
    objectBody.collisionBitMask = groundCategory | obsticalCategory | waterCategory | blockCategory | otherCategory;
    objectBody.contactTestBitMask = groundCategory | obsticalCategory | otherCategory;
    objectBody.usesPreciseCollisionDetection = YES;
    return objectBody;
}

Upvotes: 2

Views: 201

Answers (1)

txaidw
txaidw

Reputation: 479

After all this time with this bug (since iOS 8.2?) I finally manage to get rid of the inverse physics body effect.

How did I fixed? I just replaced the sprite I was using with another for some other test, and it work without inverting the physics body. So I when to photoshop and made another version of the first sprite (the one with the problem), and it finally worked!!

So, I think this have to do with the complexity of the sprite, and maybe because it could not be finding a "closed shape" on the image, I don't know, but know it's working with the new sprite.

I hope you can at least continue your development now, and algo that Apple fixes that in the future!

Upvotes: 2

Related Questions