sprite-kitphysicsbit-masksskphysicscontact

Physics contacts in SpriteKit


Is it possible to explain how bitmasking works for just this simplistic situation:

 A big (blue) ball.    Contact BitMask 0b0001

 A medium (red) ball.  Contact BitMask 0b0010

 A small (white) ball. Contact BitMask 0b0100

They have no collisions between them, because somehow this is turned off between them. I assume by setting their collision masks to 00 all the way through the 32 bits... but let's leave that for another question, I haven't yet figured out how to turn these off.

Having set each ball to have a unique contact bit mask, what is done that gives contact information when they contact?

How is it possible to know which two balls have contacted each other?

Is it possible to have the contact information only received by the biggest ball in any given contact?


UPDATE:

I'm not necessarily after code. If you need code to explain what's what, how it works, and why whatever does what, go for it.

But what I'm really after is an understanding of how bitmasking works to provide logic that permits the determination of "who" was involved in any contact.

The reason I've given each ball size a different bitmask is that I thought this might help determine the bigger ball in any given contact between two balls. I could be wrong about this, though.

UPDATE 2:

Understanding of processes progressing:

As I understand it, these are the parts of the process:

  1. Register SKView subclassed SKScene as conforming to the physics world contact notification delegate. Say it is so, too.

  2. Set bitmask as categories for each type of bodily interaction type desire to be known about and/or controlled in the simulation

  3. Use these bitmasks to articulate the nature of each object desired to be a part of contacts and/or collisions by applying them appropriately (some magic decision making in here).

  4. Override the contacts callback with code that does stuff, in the same SKView subclass registered as the delegate

  5. Create some magic code that determines who/what has contacted whom.


I understand some of these, but not the differentials of setting contact bitmasks versus the reasoning for the naming of category bitmasks, and not how to determine who has contacted whom.


Solution

  • First, you need to define what your sprites are, this is what the categoryBitMask is for. Think of this as a binary name for your sprite, with each digit being a unique name. Now it is possible to give your sprite more than 1 name, but for simplicity purposes, let's keep it to one.

    contactBitMask tells the sprites what names it should be looking for.

    So during the physics phase, the engine will take a given sprite, and look for all other physics sprites with the search name provided in the contactBitMask.

    After this, evaluations are performed against the original sprite and the sprites in the filtered list.

    On a contact, didBegin(contact:) is called, and contact contains all the information you need, including the 2 contact bodies. You then check the categoryBitMask to get the name of the sprites in question, and you do your conditioning based on these names.