1
votes

Still working on my space Invaders clone and I want to add the destructible bases at the bottom of the screen :

enter image description here

I've worked out how I can modify the appearance of the shield by having the bombs and the shield contact each other and then in didBegincontact, create an SKCropNode from a mask for the bomb's explosion and the current texture of the shield and then take the rendered texture with view's texture(from: node) method and updating the shield's texture & physicsBody from the new texture.

However - when there is a hole blown through the center of the shield, such that the shield's texture is no longer contiguous (e.g. the left-most shield in the picture), the generated physicsBody only covers the left-hand portion of the texture. Thus any bombs hitting the right-hand part of the shield won't be detected.

How can I have my shield break into multiple pieces and keep contact detection working?

Possible solutions:

  • Somehow scan the new texture and create multiple physicsBoies which are either assigned to a single SKSpriteNode using SKPhysicsbody(bodies: [SKPhysicsBody]). If, as it appears, the SKPhysicsBody is generated from the top-left of the texture, then maybe rotate the texture through 90 degrees 4 times to generate 4 physicsBodies?
  • Use some sort of alpha-masking (so that the destroyed parts of the
    shiled are clear) and keep the physicsBody of the shield in the
    original shape and when dBC is called, check the colour of the pixels at the contact point/under the bomb - if they are 'clear', then take no action and allow the bomb to fall through unimpeded until
    didEndContact is called indicating that the bomb is clear of the
    shield.

  • Use a simple intersectsRect between the bomb and the shield and then check the pixel colour under the bomb - if clear, don't do anything.

===================================================

Update : See below for sprite texture damage working when not split. The brown base is the original one showing the first damage (physicsBody shows the original base, the texture shows the dame). The green base has had it's physics body recalculated:

enter image description here

Update: See below for the physics body when the texture is split. The brown is the original shield, the small indent is the 'new' damage. The physicsBody only maps to the left-hand portion of the image.

enter image description here

1
You should just be using SKPhysicsBody(texture), every time your shield texture changes, give it a new body with the new texture, if it is not forming the correct body, then place the new texture into a temp sprite node so you can see what is being generated, I am almost positive I created bodies before with holes in the middle of them.Knight0fDragon
@Knight0fDragon - doesn't work 😟 unless I'm doing something wrong. See my update.Steve Ives
When i get time ill see what i come up withKnight0fDragon
@Knight0fDragon - thanks. I've added a screenshot to show the bomb damage/cropnode trick working. I have an Xcode Playground to test this if you want a copy.Steve Ives
Just played with it, looks like it cant be doneKnight0fDragon

1 Answers

1
votes

So I gave up with deforming the SKTexture of the base and instead implemented the base as about 30 small blocks, each with their own physicsBody and each of which is removed completely once it's hit.

I was concerned about performance but these are static blocks and so far, performance is still good. The overall graphic effect is OK too:

enter image description here

I may change the block textures to give them rough edges and experiment with replacing each block's texture with a smaller, more-dameged one the first time it is hit, for a more crumbling appearance.