0

I am trying to build a game and my physics body contacts are not being detected. Can you pls help me? I have set the physicsWorldDelegate and categorybitmasks but cannot figure out what is wrong. The playground couldn't detect the collisions between the enemy and my avatar.

import SpriteKit

public class GameScene: SKScene, SKPhysicsContactDelegate {

    let coronaSpeed: CGFloat = 80.0
    let playerspeed: CGFloat = 155.0

    var mask: SKSpriteNode?
    var player: SKSpriteNode?
    var mcorona: [SKSpriteNode] = []

    var lastTouch: CGPoint? = nil


    override public func didMove(to view: SKView) {
        physicsWorld.contactDelegate = self

        // Animations
        player = childNode(withName: "player") as? SKSpriteNode

        mask = childNode(withName: "mask") as? SKSpriteNode
        mask!.run(SKAction.repeatForever(
            SKAction.sequence([
                SKAction.moveBy(x: 0, y: 10, duration: 0.45),
                SKAction.moveBy(x: 0, y: -10, duration: 0.45)
                ]
        )))

        for child in self.children {
            if child.name == "corona" {
                if let child = child as? SKSpriteNode {
                    mcorona.append(child)
                }
            }
        }
        // </> Animations
    }

    override public func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) { handleTouches(touches)
        updatePlayer()
        updateZombies()

    }

    override public func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) { handleTouches(touches)

    }

    override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { handleTouches(touches) }

    fileprivate func handleTouches(_ touches: Set<UITouch>) { lastTouch = touches.first?.location(in: self) }

    override public func didSimulatePhysics() {
        if player != nil {
            updatePlayer()
            updateZombies()
        }
    }


    fileprivate func shouldMove(currentPosition: CGPoint,
                                touchPosition: CGPoint) -> Bool {
        guard let player = player else { return false }
        return abs(currentPosition.x - touchPosition.x) > player.frame.width / 2 ||
            abs(currentPosition.y - touchPosition.y) > player.frame.height / 2
    }

    fileprivate func updatePlayer() {
        guard let player = player,
            let touch = lastTouch
            else { return }
        let currentPosition = player.position
        if shouldMove(currentPosition: currentPosition,
                      touchPosition: touch) {
            updatePosition(for: player, to: touch, speed: playerspeed)

        } else {
            player.physicsBody?.isResting = true
        }
    }


    func updateZombies() {
        guard let player = player else { return }
        let targetPosition = player.position

        for corona in mcorona {
            updatePosition(for: corona, to: targetPosition, speed: coronaSpeed)
        }
    }
    func getDuration(pointA:CGPoint,pointB:CGPoint,speed:CGFloat)->TimeInterval{
        let xDist = (pointB.x - pointA.x)
        let yDist = (pointB.y - pointA.y)
        let distance = sqrt((xDist * xDist) + (yDist * yDist));
        let duration : TimeInterval = TimeInterval(distance/speed)
        return duration
    }
    fileprivate func updatePosition(for sprite: SKSpriteNode, to target: CGPoint, speed: CGFloat) {

        let currentPosition = sprite.position
        let angle = CGFloat.pi + atan2(currentPosition.y - target.y, currentPosition.x - target.x)
        let rotateAction = SKAction.rotate(toAngle: angle + (CGFloat.pi*0.5), duration: 0)
        sprite.run(rotateAction)
        //sprite.physicsBody?.isDynamic = true

        let velocityX = speed * cos(angle)
        let velocityY = speed * sin(angle)

        let newVelocity = CGVector(dx: velocityX, dy: velocityY)
        sprite.physicsBody?.velocity = newVelocity

        sprite.physicsBody?.affectedByGravity = false
        let moveToTouch = SKAction.move(to: CGPoint(x: target.x, y: target.y),duration: getDuration(pointA:currentPosition,pointB:target,speed:speed))
        sprite.run(moveToTouch)


    }

    public func didBegin(_ contact: SKPhysicsContact) {

        var firstBody: SKPhysicsBody
        var secondBody: SKPhysicsBody

        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        } else {
            firstBody = contact.bodyB
            secondBody = contact.bodyA
        }

        // Check contact
        if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask &&
            secondBody.categoryBitMask == mcorona[0].physicsBody?.categoryBitMask {
            // Player & corona
            gameOver(false)
        } else if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask &&
            secondBody.categoryBitMask == mask?.physicsBody?.categoryBitMask {
            // Player & mask
            gameOver(true)
        }


    }
    fileprivate func gameOver(_ didWin: Bool) {
        let resultScene = MenuScene(size: size, didWin: didWin, levelToSend: 2)
        let transition = SKTransition.flipVertical(withDuration: 1.0)
        view?.presentScene(resultScene, transition: transition)
    }

}

Here is a GIF to my problem- https://giphy.com/gifs/U7nmXZgVxNcCuvJc7j

4
  • Did you create the SKPhysicsBody for the two sprites? if not, you need to create them.
    – Arie Pinto
    May 13, 2020 at 7:13
  • can you please tell me how to do it? My xcode is filled with bugs when i create the physicsBody May 13, 2020 at 8:03
  • One way would be by texture, so you first create the texture let bodyTexture = SKTexture(imageNamed: "your_texture_image"), then apply it to the node player.physicsBody = SKPhysicsBody(texture: bodyTexture, size: bodyTexture.size()), finally you give it a categoryBitMask and a contactTestBitMask.
    – Arie Pinto
    May 13, 2020 at 11:17
  • Thanks! I figured it out by going and changing the properties in Attribute inspector in Xcode May 13, 2020 at 15:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.