Saturday, November 7, 2026
HomeiOS Developmentios - Sport isn't detecting some contacts between objects

ios – Sport isn’t detecting some contacts between objects

[ad_1]

Right here is my didBegin operate

    func didBegin(_ contact: SKPhysicsContact) {
        let otherBody: SKPhysicsBody
        let bulletMask = PhysicsCategory.bullet.rawValue
        if (contact.bodyA.categoryBitMask & bulletMask) > 0 {
            // physique A is the bullt
            otherBody = contact.bodyB
        }
        else {
            // physique B is the bullet
            otherBody = contact.bodyA
        }
        print(otherBody.categoryBitMask)
        
        swap otherBody.categoryBitMask {
        case PhysicsCategory.boundary.rawValue:
            print("hit boundary")
        case PhysicsCategory.bullet.rawValue:
            print("hit bullet")
        case PhysicsCategory.participant.rawValue:
            print("hit participant")
        default:
            print("contact with no sport logic")
        }
    }

Mainly, I created code the place the participant always shoots bullet within the course they’re going through at 0.5-second intervals. I want to acknowledge contacts between the bullets and the opposite participant, different bullets, or enviornment boundaries. Nonetheless, once I run this code, the swap assertion leads to calling the case: PhysicsCategory.participant.rawValue as a result of I create a node for the bullet beneath the participant. So the very first contact is between the bullet and the participant it originated from. Humorous sufficient, there barely appears to be a collision right here. However when the identical bullet travels to the sector boundary, and contacts it, I don’t obtain an output within the console hit boundary.

The next are the physicsBodies and bit masks I created for the participant, bullets, and enviornment boundaries.

class HorizontalBoundary: SKSpriteNode {
    
    init() 
        PhysicsCategory.bullet.rawValue
        
        self.physicsBody!.affectedByGravity = false
        self.physicsBody!.isDynamic = false
    
    
    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)
    }
    
}

class Bullet: SKSpriteNode, GameSprite {
    var textureAtlas: SKTextureAtlas = SKTextureAtlas(named: "Participant")
    
    var initialSize: CGSize = CGSize(width: 45, peak: 45)
    
    init() 
        PhysicsCategory.boundary.rawValue
        self.physicsBody?.collisionBitMask =
        PhysicsCategory.participant.rawValue
        
        self.physicsBody!.affectedByGravity = false
        self.physicsBody!.isDynamic = false
    
    
    
    func onTap() {}
    
    
    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)
    }
}

class Player1: SKSpriteNode, GameSprite {
    var textureAtlas: SKTextureAtlas = SKTextureAtlas(named: "Participant")
    
    var initialSize: CGSize = CGSize(width: 50, peak: 50)
    
    init() 
        PhysicsCategory.bullet.rawValue
        
    
    
    
    func onTap() {}
    
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)
    }
}

I even have class GameScene: SKScene, SKPhysicsContactDelegate for my GameScene file and I’ve self.physicsWorld.contactDelegate = self in my didMove operate.

The next is the operate I exploit to create and hearth a bullet from the place of the participant within the course the participant is going through

    func fireBullet1(angle: CGFloat, participant: Player1, scene: SKScene) {
        let bulletNode = SKSpriteNode(texture: bullet1.textureAtlas.textureNamed("bullet1"), coloration: .clear, measurement: bullet1.initialSize)
        bulletNode.place = participant.place
        bulletNode.place.y += sin(angle)*10
        bulletNode.place.x += cos(angle)*10
        
        bulletNode.physicsBody = SKPhysicsBody(circleOfRadius: self.measurement.width/500)
        bulletNode.physicsBody?.mass = 0.005
        bulletNode.physicsBody?.affectedByGravity = false
        bulletNode.zRotation = angle
        
        scene.addChild(bulletNode)
        
        bulletNode.physicsBody?.applyImpulse(CGVector(dx: cos(angle), dy: sin(angle)))
        
    }

And right here is how the arrange the physics classes to make use of class/contact/collision bit masks exterior my GameScene class:

enum PhysicsCategory: UInt32 {
    case participant = 2
    case bullet = 4
    case boundary = 8
}

So can somebody assist me see what am I doing unsuitable and what I must do so as make my sport register the contact and collision of the bullets with the sector boundaries, different bullets, and the opposite participant?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments