Removing individual bodies (Objective-Chipmunk)

Official forum for the Chipmunk2D Physics Library.
Post Reply
Rb_
Posts: 4
Joined: Sat Feb 20, 2010 12:42 pm
Contact:

Removing individual bodies (Objective-Chipmunk)

Post 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 :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Removing individual bodies (Objective-Chipmunk)

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Rb_
Posts: 4
Joined: Sat Feb 20, 2010 12:42 pm
Contact:

Re: Removing individual bodies (Objective-Chipmunk)

Post 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 :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Removing individual bodies (Objective-Chipmunk)

Post by slembcke »

Not really anything more significant than:

Code: Select all

cpSpaceRemoveBody(space, ballBody);
cpSpaceRemoveShape(space, ballShape);      
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Rb_
Posts: 4
Joined: Sat Feb 20, 2010 12:42 pm
Contact:

Re: Removing individual bodies (Objective-Chipmunk)

Post 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)
Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests