Page 1 of 2

Single object that when shaken falls apart

Posted: Mon Aug 23, 2010 4:02 pm
by Pedagogic368
Hello - I have set up a project in Cocos2d that I want to use Chipmunk to help complete. I have a sprite object that I want to "break apart" when the user shakes the device. It would be like the pyramid topple demo, (the sprite representing the pyramid object) but with fewer components. - Can anyone guide me as to where I should start? I have reviewed the demo and online tutorials, but have yet to find a good example that shows an actual object being broken into pieces that fall onto the ground. Thanks in advance for any suggestions.

Re: Single object that when shaken falls apart

Posted: Tue Aug 24, 2010 8:11 am
by slembcke
Just make a bunch of rigid bodies and collision shapes for different parts of the object you want to fall apart and let the fall to the ground.

Re: Single object that when shaken falls apart

Posted: Tue Aug 24, 2010 8:39 am
by Pedagogic368
Can you point me to a demo. I went over the pyramid tumble but it looks like it is creating an object mathematically - I can understand the concept of creating different rigid bodies, each with collision points, do not have a good grasp on how this is done in chipmunk. Thanks so much for taking the time to respond.

Re: Single object that when shaken falls apart

Posted: Tue Aug 24, 2010 11:37 am
by slembcke
It's not really doing anything complicated. It just boils down to creating a bunch of objects like this:

Code: Select all

body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)));
body->p = cpv(someX, someY);
cpBodySetAngle(body, someAngle);

shape = cpSpaceAddShape(space, cpPolyShapeNew(body, num, verts, cpvzero));
shape->e = 0.0f; shape->u = u;

Re: Single object that when shaken falls apart

Posted: Tue Aug 24, 2010 12:25 pm
by Pedagogic368
Thanks - I will start from here. I really appreciate your response!!

Re: Single object that when shaken falls apart

Posted: Tue Aug 24, 2010 6:10 pm
by Pedagogic368
Hello - thanks for the help and code - I was able to get my object to drop to the ground - I have a few questions..

I understand that I have to bring each section of the object into the space via being a CCSprite - I then define its body in the .m file -

1.) Do I need to add a separate floor for each body that I create? In the code below the second body or second image.png drops and then keeps going - it does not hit the floor.

2.) The action (falling) is immediate. I know enough to adjust the mass of my object so that it does not fall as soon as the scene pops up, but is there a way the have it fall when I click a button or shake the device (preferably) - I imagine that it would be a "force" associated with an action - can you recommend any examples of this? Thanks so much -

Code: Select all

-(void)setupChipmunk{
	cpInitChipmunk();
	space = cpSpaceNew();
	space->gravity = cpv(0,-2000);
	space->elasticIterations = 1;
	[self schedule: @selector(tick:) interval: 1.0f/60.0f];
	
	cpBody* ballBody = cpBodyNew(200.0, INFINITY);
	ballBody->p = cpv(150, 400);
	cpSpaceAddBody(space, ballBody);
	cpShape* ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero);
	ballShape->e = 0.8;
	ballShape->u = 0.8;
	ballShape->data = ballSprite;
	ballShape->collision_type = 1;
	cpSpaceAddShape(space, ballShape);
	
	
	cpBody* ballBody2 = cpBodyNew(200.0, INFINITY);
	ballBody2->p = cpv(500, 400);
	cpSpaceAddBody(space, ballBody2);
	cpShape* ballShape2 = cpCircleShapeNew(ballBody2, 20.0, cpvzero);
	ballShape2->e = 0.8;
	ballShape2->u = 0.8;
	ballShape2->data = ballSprite1;
	ballShape2->collision_type = 1;
	cpSpaceAddShape(space, ballShape2);
	
	cpBody* floorBody = cpBodyNew(INFINITY, INFINITY);
	floorBody->p = cpv(0, 0);
	cpShape* floorShape = cpSegmentShapeNew(floorBody, cpv(0,0), cpv(320,0), 0);
	floorShape->e = 0.5;
	floorShape->u = 0.1;
	floorShape->collision_type = 0;
	cpSpaceAddStaticShape(space, floorShape);
}

-(id)init{
	self = [super init];
	if(nil != self){
		ballSprite = [CCSprite spriteWithFile:@"SceneThree_House_11.png"];
		[ballSprite setPosition:CGPointMake(150, 400)];
		[self addChild:ballSprite];
		
		ballSprite1 = [CCSprite spriteWithFile:@"SceneThree_House_14.png"];
		[ballSprite1 setPosition:CGPointMake(500, 400)];
		[self addChild:ballSprite1];
		
		[self setupChipmunk];
	}
	return self;
}
@end

Re: Single object that when shaken falls apart

Posted: Tue Aug 24, 2010 9:29 pm
by mobilebros
1. No you do not need to do that, and I cannot see any error in your code that would make it fall through.... strange. [Edit: perhaps its passing right through it because it is moving very fast?]

2. You need to "step" the space to get anything to actually move, why don't you just not step the space until the trigger you're describing happens.

Re: Single object that when shaken falls apart

Posted: Tue Aug 24, 2010 10:06 pm
by Pedagogic368
Thanks - I will look into how to how to "step" the space works - Then set up my trigger - Thanks for the hint - I can 't wait to see how this all works out!!

Re: Single object that when shaken falls apart

Posted: Wed Aug 25, 2010 10:22 am
by mobilebros
No problem, it appears like this line is what sets off your stepping

Code: Select all

[self schedule: @selector(tick:) interval: 1.0f/60.0f];
You probably step it in your "tick" method, so if you just don't schedule tick until your trigger you should be good.

Re: Single object that when shaken falls apart

Posted: Wed Aug 25, 2010 5:16 pm
by Pedagogic368
Quick question - everything is set up nice and works fine - EXCEPT that the object is "breaking apart" when it falls down - My object is made up of several "block objects" - How can I get them to - "break apart" and crumble or fall down - do I need to set up the masses so that they are different? Do I rotate them as they are falling? There is a discussion in this forum about this very issue - it says to "Use one of the cpMomentFor*() functions to help calculate it" - I can not fined anything on cpMomentFor* () Any suggestions would be appreciated. Thanks

Code: Select all

-(void)setupChipmunk{
	cpInitChipmunk();
	space = cpSpaceNew();
	space->gravity = cpv(0,-200);
	space->elasticIterations = 1;
	
	
	cpBody* blockBody = cpBodyNew(200.0, INFINITY);
	blockBody->p = cpv(470, 524);
	cpSpaceAddBody(space, blockBody);
	cpVect verts[]={cpv(-25,50), cpv(25, 50), cpv(25, -50), cpv(-25,-50)};
	cpShape* blockShape = cpPolyShapeNew(blockBody, 4, verts,cpvzero);
	blockShape->e = 0.8;
	blockShape->u = 0.8;
	blockShape->data = blockSprite;
	blockShape->collision_type = 1;
	cpSpaceAddShape(space, blockShape);
	
	cpBody* blockBody1 = cpBodyNew(200.0, INFINITY);
	blockBody1->p = cpv(570, 524);
	cpSpaceAddBody(space, blockBody1);
	cpVect verts1[]={cpv(-25,50), cpv(25, 50), cpv(25, -50), cpv(-25,-50)};
	cpShape* blockShape1 = cpPolyShapeNew(blockBody1, 4, verts1,cpvzero);
	blockShape1->e = 0.8;
	blockShape1->u = 0.8;
	blockShape1->data = blockSprite1;
	blockShape1->collision_type = 1;
	cpSpaceAddShape(space, blockShape1);
	
	
	cpBody* floorBody = cpBodyNew(INFINITY, INFINITY);
	floorBody->p = cpv(0, 0);
	cpShape* floorShape = cpSegmentShapeNew(floorBody, cpv(200,200), cpv(1024,200), 0);
	floorShape->e = 0.5;
	floorShape->u = 0.1;
	floorShape->collision_type = 0;
	cpSpaceAddStaticShape(space, floorShape);
}