bwiz
bwiz

Reputation: 407

Add collision to only the borders of a node, allowing a sprite to move freely inside of the node

I am building a game in which I "dig" underground to allow a ball to fall through. Is it possible to allow this ball collide with the borders of the node whilst inside of it. Based on the image below, my goal is to draw a tunnel with touchMoved and let the ball fall in while colliding with the surface of the tunnel.

Example of goal

Upvotes: 2

Views: 357

Answers (2)

Steve Ives
Steve Ives

Reputation: 8134

I think you should should use the 2 sides of the tunnel to define SKShapeNodes so that when the ball is in the tunnel, it isn't inside a node.

In your example picture, you would have 2 nodes - one for each of the black areas.

Then set up your collision parameters such that the ball cannot pass through the shapes

Upvotes: 2

Khalid Afridi
Khalid Afridi

Reputation: 923

You have many ways to achieve the same thing in spriteKit.

// add your rectangle size for example 
SKPhysicsBody(edgeLoopFrom: scene.frame)

but in your case as it's in the image. You have to draw the tunnel you can draw your physics body using the points and give it a physicsBody and then set the isDynamic to false which means the body will not be moved by other physic bodies. You can still move it using actions which is called kinematic.

// After you draw your physicsBody set the isDynamic physicsBody property to false
tunnel.physicsBody.isDynamic = false

Now the body will collide with other physic bodies but will not be affected by any kind of collision or gravity

Upvotes: 0

Related Questions