Page 1 of 1

Slow fps problem after adding shapes and bodies to space

Posted: Sat Jan 02, 2010 7:09 am
by guptanikesh
Hello

I am having a slow fps problem as soon as my gamelayer is initialized. While with other layers like Menulayer fps is 55-60 fps but as soon as Gamelayer is loaded fps comes down to 7-8 fps. I am adding 19 objects which are initialized as following. its a subclass of Sprite(Cocos2D).

As soon as the first collision occurs with any object out of 19, or among themselves or with another object the fps comes to 55-60 fps. And it never goes down during the full game play. I am unable to find out what is the problem and why this is happening only in the beginning?

Code: Select all

-(id ) initWithImageFile:(NSString *)imageName inSpace:(cpSpace*)inSpace 
{
	self = [super initWithFile:imageName];	
	[self setPosition:ccp(0, 0)];
	image = [UIImage imageNamed:imageName];

	radius = image.size.width / 2 - OFFSET;
	height = image.size.height - (2 * OFFSET);
	width = image.size.width - (2 * OFFSET);	
	isCollided = NO;
	
	int num_vertices = 4;
	cpVect verts[4]= {
		cpv(radius * -1, radius * -1),
		cpv(radius * -1, radius),
		cpv(radius, radius),
		cpv(radius, radius * -1)
	};
	
	body = cpBodyNew(1.0, cpMomentForPoly(1.0, num_vertices, verts, cpvzero));
	body->p = cpv([self position].x, [self position].y);
	body->v = cpvzero;
	body->f = cpvzero;
	cpSpaceAddBody(inSpace, body);
	shape = cpCircleShapeNew(body, radius+0.5, cpvzero);
	shape -> data = self;
	shape -> u = 0.4f;
	shape -> e = 0.7f;
	cpSpaceAddShape(inSpace, shape);
	return self;	
}

When I am loading these objects without adding body and shapes to space

Code: Select all

//cpSpaceAddBody(inSpace, body);
//cpSpaceAddShape(inSpace, shape);

the fps is 55-60 but as soon as I add the body and shapes to space the fps comes down to 7-8.


My step function is as following

Code: Select all

-(void) step: (ccTime) delta
{		
	int steps = 2;
	cpFloat dt = 1.0/60.0/2.0f;
	
	for(int i=0; i<steps; i++){
		cpSpaceStep(space, dt);
	}
	cpSpaceHashEach(space->activeShapes, &eachShape, self);
	cpSpaceHashEach(space->staticShapes, &eachShape, self);
}	
I am resizing hash like this

Code: Select all

cpSpaceResizeStaticHash(space, 100, 500);
cpSpaceResizeActiveHash(space, 100, 500);
Please help me to solve this problem.

Thanks in advance.
Nikesh

Re: Slow fps problem after adding shapes and bodies to space

Posted: Sat Jan 02, 2010 11:45 am
by dieterweb
make sure (when developing for the iPhone) that you are using floats and not doubles for chipmunk.

Re: Slow fps problem after adding shapes and bodies to space

Posted: Sat Jan 02, 2010 10:24 pm
by slembcke
Have you tried profiling with Shark to see where the problem is? That is always a good place to start when optimizing.

Are you compiling as debug? With thumb disabled? Do you need two substeps? Are things constantly touching or just bouncing off? How many iterations are you using for your space?

Re: Slow fps problem after adding shapes and bodies to space

Posted: Tue Jan 05, 2010 10:45 am
by guptanikesh
slembcke wrote:Have you tried profiling with Shark to see where the problem is? That is always a good place to start when optimizing.

Are you compiling as debug? With thumb disabled? Do you need two substeps? Are things constantly touching or just bouncing off? How many iterations are you using for your space?

Hi

I tried Shark ..and it seems to be fine.

Yes I am compiling with debug and without thumb disabled. Yes I need two substeps. Things are constantly touching.I am using 2 iterations for space.

Re: Slow fps problem after adding shapes and bodies to space

Posted: Tue Jan 05, 2010 1:31 pm
by slembcke
guptanikesh wrote:I tried Shark ..and it seems to be fine.
:?:

What do you mean by fine? Shark doesn't tell you good or bad, just where your CPU cycles are spent. If you want to optimize, Shark tells you where your time (down to the level of functions and even lines/instructions) would be best spent trying to optimize.

Re: Slow fps problem after adding shapes and bodies to space

Posted: Tue Jan 05, 2010 5:04 pm
by dieterweb
I had the same problem and my reason was using doubles for chipmunk. Have you checked that?

Re: Slow fps problem after adding shapes and bodies to space

Posted: Tue Jan 05, 2010 6:36 pm
by slembcke
To add to my last post: while it's clear that Chipmunk is probably using a lot of the CPU time, Shark can still give valuable information as to where in Chipmunk the time is spent and the best way to speed it up. It might be as simple as changing some parameters. For our game Crayon Ball, we had 30 balls on the screen at any given time. Mostly settled and packed together. It used a lot of CPU on the iPhone and we were only using 1 iteration per step.

Poly shapes are going to generate more contact points than circles, meaning more constraints to solve and more iterations needed to make it look solid. If Shark tells you that most of the CPU time is spent in the solver (cpArbiterApplyImpulse) then there isn't going to be a whole lot you can do to speed it up other than to use fewer iterations.

Re: Slow fps problem after adding shapes and bodies to space

Posted: Thu Jul 30, 2015 2:14 am
by p1p1
For those who might run in the same trouble, here's a solution I came up with: viewtopic.php?f=1&t=4918