3

I'm trying to create a SKPhysicsBody from a texture containing alpha values dynamically. Unfortunately the SKPhysicsBody is created from just one piece in the texture. How can I solve that?

Here's how the SKPhysicsBody is being created:

physicsBody = SKPhysicsBody(texture: texture!, size: size)

Edit:

Before

After

DrDigg0R
  • 51
  • 5
  • 1
    Which part of the texture are you looking for? The gray, the brown, or the green? Would just layering work for you? Like gray in the background, then the brown as a foreground with alpha (easier to generate a physicsBody) and then the green (player?) interacting with the brown? – Gliderman Nov 13 '15 at 22:34
  • I'm looking for the collider of the brown part - it shall be a destructible terrain and the collider changes dynamically. These holes in the brown part are caused by the green disk/circle. The problem is SKPhysicsBody(texture: texture!, size: size) only returns one physics body and discards the others. – DrDigg0R Nov 13 '15 at 22:45

1 Answers1

2

For the sake of completeness, I found a way to solve that problem now.

Sadly I can't just create the SKPhysicsBody with this convenient function from the texture. Once the texture would contain more than one shape it'll no longer work.

Basically what I do is to create the initial path manually, do all calculation on the paths itself and store them all, then re-create the SKPhysicsBodies from this paths after each change.

I use an open source clipping library (Clipper) which I pass the paths to clip and which gives me back a two dimensional CGPoint array containing all paths/polygons from all shapes. I use this paths to create the new SKPhysicsBodies and merge them with SKPhysicsBody(bodies: [SKPhysicsBody]).

enter image description here

DrDigg0R
  • 51
  • 5
  • Nice job. I faced similar problem with making `SKPhysicsBody` from not contiguous texture. Maybe you can provide more detailed code examples of your solution? – Yury Apr 16 '21 at 12:40