bouncing menu ?

Official forum for the Chipmunk2D Physics Library.
Post Reply
macounette
Posts: 1
Joined: Tue Mar 31, 2009 3:08 pm
Contact:

bouncing menu ?

Post by macounette »

Hello,

First, sorry for my english, it's not my native language...

I try to draw a bouncing menu with 20 buttons for an iPhone app.
I thought it will be easier to use MenuItem and to add the items/boxes to a shape. And, with a MenuItem, it was easy to have a selectedImage ;)

My problem is my app is very slow...but I have just 20 boxes ! It's not very usable.
Is it because I use MenuItems ?

Code: Select all

-(id) init {
       cpInitChipmunk();
	
	staticBody = cpBodyNew(INFINITY, INFINITY);  
	space = cpSpaceNew();
	cpSpaceResizeStaticHash(space, 400.0f, 40);
	cpSpaceResizeActiveHash(space, 100, 600);
	
	space->gravity = cpv(0, -500);
	makeStaticBox(0, 110,320, 360);
	space->elasticIterations=50;
	
	MenuItem *img =[MenuItemImage itemFromNormalImage:@"img.png" selectedImage:@"imgON.png" target:self selector:@selector(myaction:)];
	MenuItem *img2 =[MenuItemImage itemFromNormalImage:@"img2.png" selectedImage:@"imgON2.png" target:self selector:@selector(myaction2:)];
        ...
        Menu *menu = [Menu menuWithItems: img, img2, ... nil];

	[self addNewIconX:124 y:409 MenuItem:img1 Label:@"my button 1"];
        [self addNewIconX:200 y:409 MenuItem:img2 Label:@"my button 2"];
        ....

        [self schedule: @selector(step:)];
}

-(void) addNewIconX: (float)x y:(float)y MenuItem:(MenuItem*)appIcon Label:(NSString*)appName 
{
	int num = 4;
	
        cpVect verts[] = {
			cpv(-30,-37),
			cpv(-30, 37),
			cpv( 30, 37),
			cpv( 30,-37),
		};
		
	cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero));
	body->p = cpv(x, y);
	cpSpaceAddBody(space, body);
		
	cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero);
	shape->e = 0.5f; shape->u = 0.5f;
	shape->data = appIcon;
	cpSpaceAddShape(space, shape);	
}


-(void) step: (ccTime) delta {
	int steps = 2;
	cpFloat dt = delta/(cpFloat)steps;
	for(int i=0; i<steps; i++){
		cpSpaceStep(space, dt);
	}
	cpSpaceHashEach(space->activeShapes, &eachShape, nil);
	cpSpaceHashEach(space->staticShapes, &eachShape, nil);
} 

//main cocos2d rendering loop, this has to be streamlined
// e.g dont add logs here :/
static void eachShape(void *ptr, void* unused){
	cpShape *shape = (cpShape*) ptr;
	MenuItem *sprite = shape->data;	
	if( sprite ) {
		cpBody *body = shape->body;
		[sprite setPosition: cpv( body->p.x, body->p.y)];
		[sprite setRotation: CC_RADIANS_TO_DEGREES( -body->a )];
	}
}
Thanks !
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: bouncing menu ?

Post by slembcke »

I would guess it's due to how you are rendering it. Is MenuItem a Cocos2D thing? I am unfamiliar with that.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
coolman
Posts: 45
Joined: Fri Feb 27, 2009 7:59 am
Contact:

Re: bouncing menu ?

Post by coolman »

slembcke wrote:Is MenuItem a Cocos2D thing?
Yep it is.

I would try some changes to this code:

space->elasticIterations=50;

change by

space->elasticIterations=5;

Also only if you don''t have static shapes, comment out the line:

cpSpaceHashEach(space->staticShapes, &eachShape, nil);
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests