Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Sprite Kit equivalent to a Box2D sensor body?

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!

like image 893
romox Avatar asked Oct 20 '22 20:10

romox


1 Answers

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...

like image 153
godel9 Avatar answered Jan 02 '23 19:01

godel9