Split physicsBody or alternatives

121 views Asked by At

I have a picture set up in spritekit that takes up the whole screen, which has its own physicsBody as follows:

original

I set up the touch methods so that the user "draws" on the screen with a clear color, essentially removing alpha from the picture:

drawing with clear color

As you can see, my physicsBody changes as the alpha values in the image change. Now, as I keep drawing closer and closer to another edge, the physicsBody acts the way it should, keeping everything that isnt alpha 0 part of the body:

another picture

However, when the texture is eventually split by a line with alpha value of 0, the physics body discludes one of the portions:

final alpha picture

This is undesired behavior, as I would like to keep EVERYTHING that has an alpha value over 0 part of the body. I trie splitting the body into multiple parts, essentially meaning that if a line is drawn between them, there will be two bodies. However, this still doesn't change anything if a line is drawn within one of the bodies.

Does anyone know how I can split the bodies if they are separated? Maybe other alternatives to accomplish the same task? Thanks

1

There are 1 answers

21
jonogilmour On

Just have an SKNode to represent the whole thing, then add a child SKNode with a physicsBody attached. Then, when you detect that the user has "split" the body in half, reduce the existing child node's physicsBody where the split happens, and add a new child node with the rest of the split entity as its physicsBody. If you split it multiple times you'll eventually have multiple nodes, each with its own physicsBody, linked together (related) by the parent SKNode.

You can do it this way (one node with heaps of children), or create a tree structure where every split will further split each child's physicsBody into two children, so every node will have at most two children. This will create a binary search tree which you can use for collisions if you wish.