Search code examples
iosios8sprite-kit2d-gamesskspritenode

"Collision Map" in SpriteKit


I'm building a 2D game using SpriteKit and this is what I would like to achieve.

Imagine a vertically scrolling SKSpriteNode which represents a tall building. Building is represented using simple image and has a physics body set with + (SKPhysicsBody *)bodyWithTexture:(SKTexture*)texture size:(CGSize)size; (introduced with iOS 8) so it is closely following the building's path.

Some parts of the building are special. Colliding with those parts should be yielding a special collision action. For example, touching the wall of the building would fire an action 1 but touching any of the windows would fire an action 2.

What I haven't been able to do is in some way define those "special blocks" of the building.

I was thinking about making some kind of a "Collision Map" for each of the building's sprite images which would be basically a transparent image with non-transparent blocks determining a collideable parts of the building. Simple example shown bellow (left: building image, right: collision map image):

Building image Collision map image

The problem with this approach is that when setting a SKPhysicsBody on a "Collision Map" image like the one above, the body is not applied to all blocks but it wraps around just one of those separate blocks. In other words: one physics body can be applied to only one, continuous block in image.

To conclude, I would like to know which approach are you using when determining non-continuous collision maps.

P.s.: building's SKSpriteNode is represented with multiple unique texture images which are scrolling vertically, one after another.

Thank you in advance.


Solution

  • Just an idea: Can't you use two Sprites for the building which are positioned at the same place: - one represents the physics body of your building (the left one from your image) - invert your collision map image to get a single physics body block. The special areas should overlap the non special area by one pixel

    Hope you understood what I mean. It's just an idea