Made a category for this.
BOOL putInto = [bodyA containsBody:bodyB];
Get the category SKPhysicsBody+Containment from GitHub
, it works only for path-path containment for now, gonna finish it up soon for circles as well.
The solution is two fold.
The first part is CGPath
works. You get CGPathRef
representation of bodyA
transformed state, then iterate over each point of the bodyB
, and test for containment. This works fine for path-path type tests, but not for circle-circle, and even less for circle-path.
The second part is to wrap this up into an SKPhysicsBody
category, to encapsulate all the implementation details. This is surprisingly pauinful.
When you ask for an SKPhysicsBody
instance from SKPhysicsBody
class, you'd expect an SKPhysicsBody
object in return. According to some design decision at Apple, it will spit you up a PKPhysicsBody
instance (part of internal PhysicsKit), which of course won't have any instance method from the category you made for SKPhysicsBody
. Extending that class can be carried out only via method / property swizzlings, so this issue put a weight on this containment category.