Search code examples
swiftsprite-kitskspritenodeskphysicsbody

Swift Sprite-kit SKPhyshicsBody


I have a few images that can detect when they get touched right now I am using

plane.physicsBody = SKPhysicsBody(circleOfRadius: plane.frame.height / 2)

but I need it so it is the boarder of the image not a circle or rectangle.


Solution

  • We have a plane and you want a physical body. Your first approach is circleOfRadius (the physical representation is a circle with a given radius). You can use another shape, such a rectangle:

    plane.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(100, height: 100))  
    

    As explained in the comments you can also use the shape of a sprite (texture) to create the shape of the physics body:

    plane.physicsBody = SKPhysicsBody(texure: plane.texture!, size: plane.size)
    

    But a good way to choose it's this: if you want your game to run smootly on many different devices, don't make sure the physics calculations become too complicated. In other words if you have many object around your plane with physical behavior moving around and colliding with each other, you may choose to use simplified shapes to deal with the physics more efficiently, especially if a shape already looks a lot like a rectangle or a circle.