Search code examples
iosobjective-cscenekitcollada

SceneKit - How to bounce a node within another node (fish in fish bowl)


I am rendering a scene from a .dae (Collada) file. Within the scene I have 4 nodes.

The large node is an odd shaped rectangular 'box'. I need the other nodes to bounce around inside this node like fish in a fishbowl.

I have the rectangular node set up as follows:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                            SCNPhysicsShapeTypeKey, SCNPhysicsShapeTypeConcavePolyhedron, nil];

SCNPhysicsShape *shape = [SCNPhysicsShape shapeWithNode:self.box1 options:dictionary];

SCNPhysicsBody *body = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeStatic shape:shape];

self.box1 = [self.scene.rootNode childNodeWithName:@"box1" recursively:NO];
self.box1.physicsBody = body;

My smaller nodes are just simple shapes with dynamic bodies.

Everything bounces around and collides with each other fine.. my issue is that the smaller nodes will only bounce on the top of the larger 'box' node. As if the box had a lid on it. I can't get the smaller nodes to enter the empty space within the 'box'. So it's like the smaller 'fish' nodes cannot enter the bowl, as they smash against the invisible lid, and just lay on top.

I hope this makes sense.. my brain is fried trying to figure this out.


Solution

  • The box does have a "lid" on it. It's a convex box, nothing concave about it, which is what you need, a concave box shape as your "bowl".

    To create this you'll need to make a "box" with a "hole" by building five sides of a box and leaving one side (the top) open. Be sure to give each side considerable width/depth so smaller objects don't simply pass through from one frame to the next.