Slow fps problem after adding shapes and bodies to space

Official forum for the Chipmunk2D Physics Library.

Slow fps problem after adding shapes and bodies to space

Postby guptanikesh » Sat Jan 02, 2010 7:09 am

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
guptanikesh
 
Posts: 8
Joined: Mon Aug 31, 2009 5:09 am

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

Postby dieterweb » Sat Jan 02, 2010 11:45 am

make sure (when developing for the iPhone) that you are using floats and not doubles for chipmunk.
Visit our game Blog: http://zombiesmash.gamedrs.com or follow us on twitter: http://twitter.com/zombiesmash
dieterweb
 
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

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

Postby slembcke » Sat Jan 02, 2010 10:24 pm

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?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

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

Postby guptanikesh » Tue Jan 05, 2010 10:45 am

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.
guptanikesh
 
Posts: 8
Joined: Mon Aug 31, 2009 5:09 am

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

Postby slembcke » Tue Jan 05, 2010 1:31 pm

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

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

Postby dieterweb » Tue Jan 05, 2010 5:04 pm

I had the same problem and my reason was using doubles for chipmunk. Have you checked that?
Visit our game Blog: http://zombiesmash.gamedrs.com or follow us on twitter: http://twitter.com/zombiesmash
dieterweb
 
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany

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

Postby slembcke » Tue Jan 05, 2010 6:36 pm

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

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

Postby p1p1 » Thu Jul 30, 2015 2:14 am

For those who might run in the same trouble, here's a solution I came up with: viewtopic.php?f=1&t=4918
p1p1
 
Posts: 7
Joined: Sun Jul 19, 2015 7:17 am


Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 1 guest

cron