Search code examples
swiftsprite-kitskphysicsbody

How to define your own physics body for node added in gamescene.sks


I'm making a game using spritekit and swift. I added a bunch of rocks to use as barriers in the gamescene.sks, and want the rocks to disappear on contact. The body type definitions in the gamescene sks are too big, and I was wondering how I can define my own physics body for these nodes. Thanks in advance for any help.


Solution

  • There are different way to achieve that. If we make it by code, here are the 2 more simple ways of achieving it: Circular body:

    circle.physicsBody = SKPhysicsBody(circleOfRadius: circle.size.width/2) // Here you put any numeric value for your radius
    

    With a Rectangular body:

    square.physicsBody = SKPhysicsBody(rectangleOf: square.frame.size) 
    

    In your case, you can use a circular body with a smaller radius than your rocks.