0

I have a basic rocket game that collects meteorites. Very basic. The game works fine except there is a real annoyance that i've been trying to iron out but can't.

When the rocket collides with a meteorite:

-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair rocket:(CCNode *)nodeA meteorite:(CCNode *)nodeB{
[self meteoriteRemoved:nodeB];
[self spawnMeteorite];
score ++;
_scoreLabel.string = [NSString stringWithFormat:@"%d", score];
}

It is removed and respawned. This works fine, but if the meteorite is close to another meteorite and the rocket slams into one, it knocks the other one out of the way like snooker balls. I want them to remain stationary until the rocket collides with them.

Is there any way to tell sprites in the same collision group to ignore each other?

EDIT**

I'm loading the meteorites in a loop from a class which has:

@implementation Meteorite

- (void)didLoadFromCCB {
self.physicsBody.collisionType = @"meteorite";
}

@end

They are then spawned using:

-(void)spawnMeteorite{
CCNode *meteorite = [CCBReader load:@"Meteorite"];

CGFloat randomX = ((double)arc4random() / ARC4RANDOM_MAX);
CGFloat randomY = ((double)arc4random() / ARC4RANDOM_MAX);
CGFloat rangeX = 320 - 48;
CGFloat rangeY = 2880 - 200;

meteorite.position = ccp((randomX * rangeX)+24, (randomY * rangeY)+ _ground.contentSize.height + _rocket.contentSize.height);
meteorite.physicsBody.velocity = ccp(0,0);
[_physicsNode addChild:meteorite];

}

The following code gives a log of Meteorite Hit extremely often when flying into a group, and most of them fly off into oblivion!

-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair meteorite:(CCNode *)nodeA meteorite:(CCNode *)nodeB{
NSLog(@"Meteorite HIT");
}

Thanks for looking into this, hope it helps

Dave440
  • 57
  • 10

1 Answers1

1

Objects with the same collision group do not collide with each other. The documentation states:

/**
 *  The body's collisionGroup, if two physics bodies share the same group id, they don't collide. Defaults to nil.
 */
@property(nonatomic, assign) id collisionGroup;

Could you share the code that assigns the collision group to your physics objects? You need to be careful here, because the "same" collision group means the exact same pointer to the same object!

Ben-G
  • 4,996
  • 27
  • 33
  • I've added the code above, hope it sheds some light on whats going wrong. – Dave440 May 28 '14 at 21:02
  • Sorted!! Thanks mate, I've given the meteorites a physicsBody.collisionGroup as well and this now works. Thanks for the pointer!! – Dave440 May 28 '14 at 21:14