SKPhysicsBody from texture not working

Hello,


I'm getting very different results with SKPhysicsBody(texture: , size: ).

With iOS 8.4 the physicsBody does conform to the texture most of the time but not always.

Sometimes creating the physicsBody doesn't work, sometimes it does. Same SKSpriteNode, same texture.

With iOS 9.2 SKPhysicsBody(texture: , size: ) doesn't work at all.

Debug area shows: PhysicsBody: Could not create physics body.

Has anyone used SKPhysicsBody(texture: , size: ) successfully with iOS 9.2 or does anyone know

a way to work around this?


Thanks,

André

Replies

Hello mp_andre,


Thanks for posting and reaching out. Would you be able to file a bug report that includes the texture you are using so we can triage the issue you are having? Once we know the texture used we may also be able to provide a workaround or suggestion.


Cheers

Hello greven,


I filed a bug report:

23329854

Thanks for looking into this.

André

Hi mp_andre


Thanks for posting the bug number. We'll let you know via the forum or via your bug when it is addressed. If there is a quick workaround we'll post it back here for you.


Cheers

I've been working around this issue via https://forums.developer.apple.com/welcome

Hi mate,

I am having a similar issue, I also filed a bug report # 23820064. I found the problem initially on ios9.1 and xcode 7.1.1, seems to still be there in 9.2.

This is definately and issue thats need fixing as it is stalling the game development. Interestingly I also tried used SKPhysicsBody(polygonFromPath: path). Same result the error message in the debugger that the Physics body could not be created. The texture is still being rendered in the game correctly and, very occasionaly the physicsbody appears on one of the textures but never all of them, even when they are the same textures. Currently I have just set all the physics bodies to rectangles which appears to work properly, however it means I have no slopes in the game just steps, pity that.

best

Stephen

I have the same problem, could you also help me?

Hi,


Developing on iOS 9.2.1 (in Swift) at the mo and hitting this problem.


Creating physics body from texture is the only time I see the problem and it appears to be random and comes and goes. Sometimes a texture works, sometimes it doesn't.


Clean and rebuild sometimes helps.


Last night I experimented to try and find the root cause. All my textures are loaded from PNG files.


I took the images that were randomly causing problems out of my graphics atlas and put them in a plain group in the project, cleaned and rebuilt.


The problem vanished and hasn't come back. Obviously, I'd rather have them in the atlas, but it was only a certain set of images causing the problem so I can live with them being separate files.


This has allowed me to continue development where I was otherwise stuck - so whilst it's not a perfect solution it may get someone else out of a hole!

I think this solution works pretty well: http://stackoverflow.com/questions/36587720/spritekit-using-sktextureatlas-causes-incorrect-physics-body


Basically, you need to explicitly tell the texture what alphaThreshold to use:

let physicsBody = SKPhysicsBody(texture: self.texture, alphaThreshold: 0.3, size: self.size)


I had the same issue, where using textures individiually (not with SKTextureAtlas) worked fine with auto-generating physics bodies. But once I migrated to use SKTextureAtlas, the physics bodies either came back all wonky or couldn't be generated at all.

Once I changed the physics body init method to include the 'alphaThreshold', the bodies were generated properly.


Hope that helps!

Unfortunately using alphaThreshold does not change anything on my project.

My problem is exactly what was described in the thread you linked to.

I haven't tried the trick with the two invisible pixels as described in the chat though.

To make this work, I would have to add invisible pixels to about 1500 textures. That's just not worth it.

On iOS 9.x I use rectangles as physicsBody and for that project it is good enough.

But thanks for adding this here. Might be useful for future projects.