Search code examples
swiftsprite-kitskphysicsbody

Designing physics bodies other than circles


I'm making my first game with SpriteKit and for the enemy aliens (SKSpriteNode subclass) physics bodies I have been using:

self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/2)

this has worked so far as most of the aliens are circle-shaped. However, if I were to have ellipse or square aliens what would be the best method of designing their physics bodies? I tried this for square shape:

let squarePath = UIBezierPath(rect: CGRect(x: -64, y: -64, width: 128, height: 128))
self.physicsBody = SKPhysicsBody(polygonFromPath: squarePath.CGPath)

but I don't know if this is the most efficient, and also don't know how to exactly make ellipses. Any suggestions would be great!


Solution

  • Using paths is the only way to create complex physics bodys, however, you can create rectangles with SKPhysicsBody(rectangleOf: CGSize(width: 100, height: 100))

    Edit: Use this to help create efficient paths from images: http://insyncapp.net/SKPhysicsBodyPathGenerator.html

    Note: this will only generate the paths for the image, you will need to clean it up your self by deleting paths and connecting the points.