Make SKPhysicsBody react to contact but not to collide

April 1, 2014
Sprite Kit iOS Objective-C Programming

As you know, Sprite Kit offers you a great and simple physics engine. You can easily setup collisions and contact detection between nodes.

Sometimes you might just want to react to contact but you don’t want to show collision, you don’t want two bodies to “bounce” from each other.

MyPass

You can see below an example where you get callbacks when two SKSpriteNodes contact but they will not “bounce” from each other.

This is how you setup this kind of behaviour:

first be sure that your SKScene(or any object that has the node) responds to SKPhysicsContactDelegate:

@interface YourScene : SKScene <SKPhysicsContactDelegate>
@end

and set “self” as the delegate:

self.physicsWorld.contactDelegate = self;

node categories for contact detection:

static const uint32_t firstNodeTypeCategory = 0x1 << 1;
static const uint32_t secondNodeTypeCategory = 0x1 << 2;

setup physics:

//somewhere in your code you have two SKSpriteNodes created as you like it :)
//SKSpriteNode *firstNode, *secondNode
//
//setup physicsBody:
firstNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:firstNode.size];
secondNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:secondNode.size];
//setup category masks so you know which is which node type:
firstNode.physicsBody.categoryBitMask = firstNodeTypeCategory;
secondNode.physicsBody.categoryBitMask = secondNodeTypeCategory;
//
//contact part
//A mask that defines which categories of bodies cause intersection notifications with this physics body.
firstNode.physicsBody.contactTestBitMask = secondNodeTypeCategory;
secondNode.physicsBody.contactTestBitMask = firstNodeTypeCategory;
//
//collision part - set the mask to be category of the node to have the effect that we need
firstNode.physicsBody.collisionBitMask = firstNodeTypeCategory;
secondNode.physicsBody.collisionBitMask = secondNodeTypeCategory;

And the last part is to react to contact callback:

-(void)didBeginContact:(SKPhysicsContact*)contact
{
    SKPhysicsBody *firstBody, *secondBody;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else{
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }
    NSLog(@"first body %@",firstBody);
    NSLog(@"second %@",secondBody);

    if ((firstBody.categoryBitMask & firstNodeTypeCategory) != 0 &&
        (secondBody.categoryBitMask & secondNodeTypeCategory) != 0) {
        NSLog(@"CORRECT CONTACT!");
    }
}

Hope this helps you in some of you games :)

If you want to learn more about Sprite Kit, you can check out the documentation on Apple website.

HODL - Crypto Portfolio Value

April 12, 2018
HODL iOS App Store App

BackWay te ayuda a encontrar el camino de vuelta

January 20, 2018
BackWay iOS App Store App

BackWay助您轻松找到返回的路

January 19, 2018
BackWay iOS App Store App
comments powered by Disqus