Single object that when shaken falls apart
-
- Posts: 6
- Joined: Mon Aug 23, 2010 3:28 pm
- Contact:
Single object that when shaken falls apart
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.
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Single object that when shaken falls apart
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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 6
- Joined: Mon Aug 23, 2010 3:28 pm
- Contact:
Re: Single object that when shaken falls apart
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.
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Single object that when shaken falls apart
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;
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 6
- Joined: Mon Aug 23, 2010 3:28 pm
- Contact:
Re: Single object that when shaken falls apart
Thanks - I will start from here. I really appreciate your response!!
-
- Posts: 6
- Joined: Mon Aug 23, 2010 3:28 pm
- Contact:
Re: Single object that when shaken falls apart
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 -
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
-
- Posts: 90
- Joined: Tue Aug 04, 2009 9:53 am
- Contact:
Re: Single object that when shaken falls apart
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.
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.
-
- Posts: 6
- Joined: Mon Aug 23, 2010 3:28 pm
- Contact:
Re: Single object that when shaken falls apart
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!!
-
- Posts: 90
- Joined: Tue Aug 04, 2009 9:53 am
- Contact:
Re: Single object that when shaken falls apart
No problem, it appears like this line is what sets off your stepping
You probably step it in your "tick" method, so if you just don't schedule tick until your trigger you should be good.
Code: Select all
[self schedule: @selector(tick:) interval: 1.0f/60.0f];
-
- Posts: 6
- Joined: Mon Aug 23, 2010 3:28 pm
- Contact:
Re: Single object that when shaken falls apart
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);
}
Who is online
Users browsing this forum: No registered users and 23 guests