Reputation: 13665
What is the better way for removing object after certain contact/collision detection:
Remove it in didBeginContact/didEndContact when contact occurs.
Add object which needs to be removed into an array and remove it in didSimulatePhysics after the whole physics simulation is done
Put contact (SKPhysicsContact) object into the array and process it in the update method (everything from didBeginContact/didEndContact is removed to one custom method and called within update:) and after every contact is processed, continue with step 2.
Upvotes: 1
Views: 161
Reputation: 1545
In all of the code I've seen and from everything I've done, Option 1 seems like the best choice.
The reason is that didBeginContact has everything that is needed to handle collisions. Each time it is called, there is a new SKPhysicsContact object, which allows for you to identify everything in the collision. If you need to remove an object, then why not do it there or call another method from inside didBeginContact. Also, timing-wise, you have a lot more control inside of didBeginContact than you would if you involved update or tried to use didSimulatePhysics. It makes it needlessly complicated by calling update; you would have rewrite the array each time there is a collision to make sure you aren't using the old SKPhysicsContact object. But, each situation is different so it's hard to judge right now when the other options would be more appropriate.
edit: Sometimes it would be better to not use didBeginContact. For example, if you wanted to change positions or remove the affected objects in the collision but at the same time wanted to see the elastic collision of the physics bodies take place, then you wouldn't want to change the didBeginContact.
Upvotes: 2