1

With SpriteKit, is it possible to have two nodes that collide with one another, but then when another node is introduced, have how the physics works change?

For example, consider the following 3 nodes:
- Object
- Object_Hole
- Player

So, a scene containing a Player (orange circle) and an Object (blue rect):

enter image description here

In the above scene, the Player (circle) would be affected by gravity and collide with the Object (square). I know how this works by using the physics body, category and collision bitmasks.

Now, introduce the Object_Hole (green square):

enter image description here

In the above scene, the Object_Hole (green square) overlays the Object (blue rect) - this can be on either the same z plane or a higher plane.

Is it possible to make the physicsBody on the Player (circle) not collide with the Object in the area that the Object_Hole is?

If there's a better way to achieve this than adding an overlaying Node, please let me know. Otherwise, is it possible?

Thanks!

Joshua
  • 197
  • 1
  • 10
  • 1
    That'll be difficult. If you want the ball to pass through the green box but also bouncing off on the blue box's sides, then you'd have to split the blue box into two bodies and continuously recreate them to change their shape according to the green box's position. – CodeSmile Nov 17 '13 at 23:34
  • Is there no easier way to create a "hole" effect in a Node (with a physicsBody) in SpriteKit? – Joshua Nov 18 '13 at 13:59
  • 1
    No because there is no such thing as a "hole". Even with plain Box2D you'd have to do basically the same thing. The difficult part isn't letting the player pass through, but to disable collision only in the area of the green circle while allowing the player to bounce off of the sides of the other wall. Because if the blue wall is a whole object, once you enable collisions with player for just a frame, collision resolve will move the player outside the blue box instantly. – CodeSmile Nov 18 '13 at 20:03

2 Answers2

2

You could look into using categoryBitMask and collisionBitMask: https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/Reference/Reference.html#//apple_ref/occ/instp/SKPhysicsBody/collisionBitMask

If you want your ball to collide with the object, but not some hole in the object, you will probably want to break your object into 2 pieces with an actual hole in the middle. Set the hole sprite's categoryBitMask and collisionBitMask to be completely different from those of the object and ball.

// sprite1 and sprite2 will never collide.
SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(20.0f, 10.0f)];
sprite1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
sprite1.physicsBody.collisionBitMask = 0xffff0000;
sprite1.physicsBody.categoryBitMask = 0xffff0000;

SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(20.0f, 10.0f)];
sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
sprite2.physicsBody.collisionBitMask = 0x0000ffff;
sprite2.physicsBody.categoryBitMask = 0x0000ffff;
Kelli
  • 51
  • 3
0

Create the blue bar as 2 separate objects, with a gap between them of 0 points (or even overlapping slightly in case the point where they touch causes odd physics reactions) when the 'hole' isn't around, but with a 'hole-sized' gap between them when the hole is preset.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55