Hi everyone, Im wondering if implementing the Proximity Manager is the best solution, i wrote some code for colission detection and I tested it on my old 3g.
I have 100 animated sprites with 30fps (60 sprites with 55 fps) and one of my goals is having 100 animated sprite battle in screen with acceptable fps.
So Ive read about the proximity manager, and I'm trying to improve my performance, but after I implemented it I ended up with a lot worse performance (100 sprites with 5 fps)
I sub classed my sprites and Im testing the collision detection in my subclass this way:
// Initializing in H
#import "ProximityManager.h"
@interface GameLayer : CCLayer
{
ProximityManager*collisionMngr;
// some more H
@property (nonatomic, retain) ProximityManager *collisionMngr;
// Now in M
@synthesize collisionMngr;
//Then Create grid in gamelayer
collisionMngr = [[ProximityManager create:40]retain];
//Adding sprites to gamelayer and proximity manager
[collisionMngr addObject:unit];
[collisionMngr update];
// update them in units subclass
- (void)update:(ccTime)dt
{
CCNode*popo = [self parent];
int newz = abs((self.position.y)-300);
[popo reorderChild:self z:newz];
NSMutableArray *test = [[popo collisionMngr] getExactRange:self.position.x y:self.position.y range:20];
// adding a log its expensive so is just for simulator
//NSLog(@"%@",test);
And this is my old collision detection code
- (void)physx:(ccTime)dt {
for(CCSprite *zunit in movableSprites){
int newz = abs((zunit.position.y)-300);
[spriteSheet reorderChild:zunit z:newz];
}
int count = [movableSprites count];
for (int i = 0; i < count; i++) {
CCNode* currentunit = [movableSprites objectAtIndex:i];
CGRect currentRect = CGRectMake(currentunit.position.x - (currentunit.contentSize.width/2),
currentunit.position.y - (currentunit.contentSize.height/2),
currentunit.contentSize.width*.2,
currentunit.contentSize.height*.2);
for (int j = i+1; j < count; j++) {
CCNode* colisionunit = [movableSprites objectAtIndex:j];
CGRect colisionrect = CGRectMake(colisionunit.position.x - (colisionunit.contentSize.width/2),
colisionunit.position.y - (colisionunit.contentSize.height/2),
colisionunit.contentSize.width*.2,
colisionunit.contentSize.height*.2);
if (CGRectIntersectsRect(currentRect, colisionrect))
{
colisionunit.position = ccp(colisionunit.position.x*1.000,colisionunit.position.y*1.005);
}
}
}
}
I hope someone has little advice :)