SpriteKit: Using SKTextureAtlas causes incorrect Physics Body

核能气质少年 提交于 2019-12-11 13:23:41

问题


I am building my first sprite kit game, and recently read an article that said to use SKTextureAtlas to improve performance.

So I migrated all my sprite images into organized .atlas folders and updated the code accordingly. However, now my sprites have weird physics bodies (mainly over-enlarged).

For instance, my player sprite has three states: Flying (flat), Flying Up and Flying Down. The three images are pretty similar, with slight variations (arms pointing flat, up, down).

Before migrating to texture atlases, the physics body was pretty spot on with the image. Now however, it is much larger than the image and slightly stretched on the y-axis.

Here is my player.atlas file structure:

player-flying-iphone.atlas:

  • player-flying-down@2x.png
  • player-flying-down@3x.png
  • player-flying-up@2x.png
  • player-flying-up@3x.png
  • player-flying@2x.png
  • player-flying@3x.png

Here is an example of my code: (Player is a subclassed SKSpriteNode)

let textureAtlas = SKTextureAtlas(named: "player-iphone")
let playerFlyingTexture = textureAtlas.textureNamed("player-flying")
let player = Player(texture: playerFlyingTexture, color: UIColor.clearColor(), size: CGSizeMake(100.0, 100.0))

let physicsBody = SKPhysicsBody(texture: playerFlyingTexture, size: CGSizeMake(100.0, 100.0))
physicsBody.dynamic = true
physicsBody.affectedByGravity = true
physicsBody.usesPreciseCollisionDetection = false
physicsBody.categoryBitMask = CollisionCategories.Player
physicsBody.contactTestBitMask = CollisionCategories.Enemy
physicsBody.collisionBitMask = CollisionCategories.EdgeBody
physicsBody.allowsRotation = false
player.physicsBody = physicsBody

回答1:


(credit goes to @Whirlwind for the help)

So for me, the fix ended up being that I need to change this:

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

..to this..

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

Apparently, the SKTextureAtlas cropping method used when generating a physics body has issues figuring out exactly what to crop, so setting the alphaThreshold explicitly seemed to fix it.

Obviously, that value will rely on your own image and what kind of transparency you have. But if you have hard edges like I did, you should be able to set that to some arbitrary value and be good to go.




回答2:


You are setting your texture size like this:

CGSize(widht:100, height:100)

which are obviously not the right dimensions of your texture.

Instead use the real texture size:

yourTexture.size()




回答3:


This problem exists on iPad 3 (iOS 9.3.5 & xcode 8.1) and in my situation, alphaThreshold didn't solve the issue with incorrect PhysicsBody.

I had to switch to "New Sprite Atlas" instead of "Texture Atlas", which magically fixed the issue.



来源:https://stackoverflow.com/questions/36587720/spritekit-using-sktextureatlas-causes-incorrect-physics-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!