5

I need to simulate a fan. In Box2D, I do this through the use of a sensor body. I haven't discovered anything in SK that works similarly. I could be wrong. Any suggestions? thanks a lot!

godel9
  • 7,340
  • 1
  • 33
  • 53
romox
  • 176
  • 1
  • 7

1 Answers1

3

If what you're trying to do is create a body that will result in contact notifications but not collisions, you can use the categoryBitMask, collisionBitMask, and contactTestBitMask properties:

Select a bit to represent the sensor category:

#define kSensorCategoryBit (0)                         // Pick your own bit here
#define kSensorCategory    (1 << (kSensorCategoryBit))

Set the properties for the sensor body:

sensorBody.categoryBitMask    = kSensorCategory; // Set sensor category bit
sensorBody.collisionBitMask   = 0x00000000;      // Prevent all collisions
sensorBody.contactTestBitMask = 0x00000000;      // Prevent contacts between sensors

Set the properties for the other bodies that you want notifications for:

otherBody.contactTestBitMask |= kSensorCategory; // Set sensor category bit

Set the physics world's contact delegate:

scene.physicsWorld.contactDelegate = contactDelegate;

Implement the contact delegate methods for the contactDelegate object:

- (void)didBeginContact:(SKPhysicsContact *)contact
- (void)didEndContact:(SKPhysicsContact *)contact

See Apple's documentation on SKPhysicsContact for more information. Hope that helps...

godel9
  • 7,340
  • 1
  • 33
  • 53
  • good point! I never really understood why Box2D needed a separate flag for "sensor" bodies when you could do the same with contact categories. – CodeSmile Nov 13 '13 at 11:36