If I create an SKPhysicsBody and add it to an SKSpriteNode as below then the collision detection works fine with the floor object below.
let myObjectBody = SKPhysicsBody(circleOfRadius: 100)
However, if instead I create the PhysicsBody using the texture of the SKSpriteNode to which it is applied then the collision doesn't work. Although it does register contact, the object just falls through the floor.
let myObjectBody = SKPhysicsBody(texture: myObject.texture!, alphaThreshold: 0, size: myObject.texture!.size())
Important note: If I set show physics to true:
skView.showsPhysics = true
Then the physics outline matches perfectly the outline of the shape of the sprite, which is generated from a png.
The physicsBody is added to the object and then the scene in the following manner.
let myTexture = SKTexture(imageNamed: "myImage")
let myObject = SKSpriteNode(texture: myTexture)
myObject.xScale = 0.2
myObject.yScale = 0.2 //The image is big to allow zooming
let myObjectBody = SKPhysicsBody(texture: myObject.texture!, alphaThreshold: 0, size: myObject.texture!.size())
myObject.physicsBody = myObjectBody
myWorld.addChild(myObject)
SKPhysicsBody
to your scene? – Alexey BondarchukmyObject
? What kind of class this is and how do you init it? – Alexey Bondarchuk