Search code examples
iosswiftsprite-kitskphysicsbodyskscene

Physics not Detecting Collision


I simply have put a square shape node onto a scene, and made the physicsBody for the scene be the edge of the screen. I turned on the physics view so you can see all of the physics stuff. The square has a physics body around it, as does the border of the scene. However, when the square runs into the border, nothing happens. The square just passes straight through. Here is how I initialize the square:

let rect1 = CGRect(x: 100, y: 100, width: 50, height: 50)
        let square = Square(rect: rect1)
        square.fillColor = UIColor.blackColor()
        square.zPosition = 200
        square.physicsBody = SKPhysicsBody(edgeLoopFromRect: rect1)
        square.physicsBody?.allowsRotation = false
        square.physicsBody?.affectedByGravity = false
        square.physicsBody?.restitution = 0.4
        self.addChild(square)

Here is how I initialize the physics body for the scene:

let physicsBody = SKPhysicsBody(edgeLoopFromRect: view.bounds)
self.physicsBody = physicsBody

Very similar code has worked for me on other games, so I am not sure why no collisions happen. Any help is greatly appreciated thanks!


Solution

  • The movement of the balls must be caused by physics rather than just setting the position. The issue was fixed through using impulses to move the balls which then made them collide with the various objects.