Page 1 of 1

Removing individual bodies (Objective-Chipmunk)

PostPosted: Wed Mar 17, 2010 4:26 pm
by Rb_
Hello all :),

I've been trying without much success to simply remove an individual chipmunk body on touch for a simple iphone game I'm creating.

For the sake of simplicity the current code I'm using declares the image, shape and body as it should and loads the game then runs through it using the NSTimer. However my ontouch method removes the image itself completely fine (By using something along the lines of " [testBall removeFromSuperview]; " )

However despite using what I think is the correct syntax for chipmunk it doesn't remove the 'testBall's body, meaning its still interacting despite the image itself not actually being there.

The list of things I've tried :
Code: Select all
    void cpSpaceRemoveBody(cpSpace *ballSpace, cpBody *ballBody);
          void cpSpaceRemoveShape(cpSpace *ballSpace, cpShape *ballShape);      
          void cpSpaceFree(cpSpace *ballSpace);
          void cpSpaceFreeChildren(cpSpace *ballSpace);
          void cpShapeDestroy(cpShape *ballShape);
          [testBall removeFromSuperview];

BallShape, BallBody and cpBody are obviously all assigned to testball.

Any help would be fantastic :)

Re: Removing individual bodies (Objective-Chipmunk)

PostPosted: Wed Mar 17, 2010 6:18 pm
by slembcke
You have to remove the body and all shapes attached to it from the space. Not quite sure what the issue is. Are you actually using the Objective-Chipmunk Wrapper? You said you tried the C API functions.

If you are using Objective-Chipmunk, you can remove all of the Chipmunk objects from the space in one call by implementing the ChipmunkObject protocol. Take a look at the iPhoneSnap game that is bundled with Objective-Chipmunk to see a simple example of how this works.

Re: Removing individual bodies (Objective-Chipmunk)

PostPosted: Sat Mar 20, 2010 1:30 pm
by Rb_
Yep sorry, disregard the 'Objective Chipmunk' part of it. I'm using the build prior to where it was implemented.

Using the old calling functions how would it be done? As I gather the objective Chipmunk costs to actually be used now? And I'm only doing it as a little personal project of mine to understand how it all works :)

Re: Removing individual bodies (Objective-Chipmunk)

PostPosted: Sun Mar 21, 2010 12:05 pm
by slembcke
Not really anything more significant than:
Code: Select all
cpSpaceRemoveBody(space, ballBody);
cpSpaceRemoveShape(space, ballShape);     

Re: Removing individual bodies (Objective-Chipmunk)

PostPosted: Sun Mar 21, 2010 3:52 pm
by Rb_
Sorry to be an absolute pain. :(

That's exactly what I initially tried. My issue is (And I'm brand new to iphone development completely)

I have all of the chipmunk functions being declared in a lovely 'SetupChipmunk' method, then I want the ball to be removed using the 'touchesBegan' method.

When trying to use the suggested code it fires up an 'undeclared' error, so after a lot of searching prior to posting this I found that you need to use 'void' prior to it. However upon doing that the code simply doesn't work.

So currently I have :

Code: Select all
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   UITouch *touch = [[event allTouches] anyObject];   
      if ([touch view] ==  ball)
      NSLog(@"touch ball");{

                         cpSpaceRemoveBody(ballSpace, ballBody);
          cpSpaceRemoveShape(ballSpace, ballShape);

         [ball removeFromSuperview];   


I've also tried purely 'space, ballBody' etc etc, but they all have the undeclared errors unless I use void (Then if I use void it doesn't fire)