Question

At certain points in my game I want a ton of balls to fall onto the screen. Once they hit the ground and bounce a bit, I want them to just sit there, and no longer need them to move.

Once I get up to 200 physics bodies, the game gets very slow, so I'd like to destroy the bodies. Here is what I was trying in my code:

-(void)didBeginContact:(SKPhysicsContact *)contact {

    if (contact.contactPoint.y < 150) {
        if (contact.bodyA.categoryBitMask == MYPhysicsCategoryBall) {
            NSLog(@"body a is ball");
            contact.bodyA = nil;
        }

        if (contact.bodyB.categoryBitMask == MYPhysicsCategoryBall) {
            NSLog(@"body b is a weapon");
        }
}

This doesn't work, because contact.bodyA and contact.bodyB are both readonly, so I have to fix that, but apart from that, will just setting the actual physics body to nil destroy it and make the physics simulator run faster? Or is there a better way to fix the performance hit? I want to be able to add more than 200 balls, maybe 500 or 600.

Was it helpful?

Solution

Use contact.bodyA.node.physicsBody:

-(void)didBeginContact:(SKPhysicsContact *)contact {

    if (contact.contactPoint.y < 150) {
        if (contact.bodyA.categoryBitMask == MYPhysicsCategoryBall) {
            NSLog(@"body a is ball");
//            contact.bodyA = nil;
            contact.bodyA.node.physicsBody = nil;
        }

        if (contact.bodyB.categoryBitMask == MYPhysicsCategoryBall) {
            NSLog(@"body b is a weapon");
        }
}

I haven't tested it though

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top