Assertion failed error on remove of shape / body

Official forum for the Chipmunk2D Physics Library.
Post Reply
Jinno
Posts: 6
Joined: Thu Mar 25, 2010 7:53 pm
Contact:

Assertion failed error on remove of shape / body

Post by Jinno »

Hello there! :)

Firstly apologies for my first post being a question, I'm sure it becomes very annoying.

Anywho if you could help me that would be fantastc.

I'm using chipmunk 5.1.0 for my iphone and I've tried to remove the body / shape of an object when its reached a certain point on the screen (When its fallen off the bottom) I'm using this code to catch it :

Code: Select all

 
 if(ball.center.y > self.view.bounds.size.height) {
		
		 if(ball.center.y > self.view.bounds.size.height + 10) {			 
			 score_int = score_int + 1;
			 score.text = [NSString stringWithFormat:@"%d",score_int];	 
			 
			 cpSpaceRemoveBody(space, ballBody);
			 cpSpaceRemoveShape(space, ballShape);
			 
			 [ball removeFromSuperview];	
			 
		 }
	 }

When it reaches this point in the actual application though, it crashes out giving :

Code: Select all

Initializing Chipmunk v5.1.0 (Debug Enabled)
Compile with NDEBUG defined to disable debug mode and assert() checks
Assertion failed: (cpArrayContains(space->bodies, body)), function cpSpaceRemoveBody, file /Users/rob/Documents/Iphone Development/Basic Assign Ap/balls Up 0.25/chipmunk/cpSpace.c, line 321.
I will say I've tried removing the shape first and the body first! So I'm very sure it isn't a removal in the right order issue.


I also have a cpDataPointer warning about it being an 'invalid receiver type' but I don't think its related to the issue above. I thought it was worth mentioning though! :D

That occurs on this line of code :

Code: Select all


	if([shape->data isKindOfClass:[UIView class]]) { 
		
		[(UIView *)shape->data setCenter:CGPointMake(shape->body->p.x, 480-shape->body->p.y)];
		
	}


If you need any more information please don't hesitate to ask, but any help in this issue would be great :)
Jinno
Posts: 6
Joined: Thu Mar 25, 2010 7:53 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by Jinno »

Just to add,

If I put the RemoveShape first I get :

Code: Select all


Initializing Chipmunk v5.1.0 (Debug Enabled)
Compile with NDEBUG defined to disable debug mode and assert() checks
Assertion failed: (cpHashSetFind(space->activeShapes->handleSet, shape->hashid, shape)), function cpSpaceRemoveShape, file /Users/rob/Documents/Iphone Development/Basic Assign Ap/balls Up 0.25/chipmunk/cpSpace.c, line 305.


User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by slembcke »

Both of those assertions mean that you never actually added the body or shape in the first place or you are trying to remove it more than once. I think I changed those to warnings in 5.2.x, but the root problem is the same.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Jinno
Posts: 6
Joined: Thu Mar 25, 2010 7:53 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by Jinno »

Interesting,

It is interacting completely fine prior to reaching that point though, I also have it declared in a setup method which is called as soon as the application is launched :


This is in the header :

Code: Select all

	cpSpace *space; // Holds the space object
	cpBody *ballBody;
	cpShape *ballShape;

This is in the main :

Code: Select all

 
 // Create our ball's body with 100 mass
 // and infinite moment
	//							  mass    moment 
	ballBody = cpBodyNew(100.0, INFINITY);
	
 // Set the initial Position
				//Width, Height
	ballBody->p = cpv(60, 250);
	
 // Add the body to the space
	cpSpaceAddBody(space, ballBody);
	
	//Create our shape associated with the ball's body
	     
	ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero);
	
	ballShape->e = 1.0; //Elasticty
	
	ballShape->u = 0.8; // Friction
	
	ballShape->data = ball; //Associate without ball's UIImageView
		
	// Add the shape to out space
			
	cpSpaceAddShape(space, ballShape);
Is it simply a case of me missing something? :)
Jinno
Posts: 6
Joined: Thu Mar 25, 2010 7:53 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by Jinno »

I have checked the code through and its the only time is called as well.

Code: Select all

cpSpaceRemoveBody(space, ballBody);
	 cpSpaceRemoveShape(space, ballShape);
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by slembcke »

But are you sure that code isn't being called more than once? The reason I added those assertions is because I kept getting support questions from people trying to add or remove things more than once. I'm quite certain that the assertion is correct.

You should put a logging statement in there to check that you aren't actually trying to remove it twice.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Jinno
Posts: 6
Joined: Thu Mar 25, 2010 7:53 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by Jinno »

Yep that was the problem. My code was repeating itself. A big thank-you for the help there :D
Jinno
Posts: 6
Joined: Thu Mar 25, 2010 7:53 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by Jinno »

Sorry to post again :(

I've tried it with removing a static shape that has some Verts attached to it.

using this code :

Code: Select all


cpSpaceRemoveStaticShape(space, floorShape);
	cpSpaceRemoveBody(space, floorBody);

And this brings another assertion error. Now This is being used in exactly the same way as the previous example, so its not being called twice this time.

I'm guessing the issue is that you need to release the Verts first?

Anyway here's the code I'm using :

Code: Select all

	floorBody = cpBodyNew(INFINITY, INFINITY);
	
	floorBody->p = cpv(160, 480-350);	
	
	floorShape = cpPolyShapeNew(floorBody, 4, verts1, cpv(-320.0f / 2, 81.0f / 2));
	
	floorShape->e = 0.5; floorShape->u = 0.1; floorShape->collision_type = 0;
	
	floorShape->data = floor;
	
	cpSpaceAddStaticShape(space, floorShape);
	
	floorShape = cpPolyShapeNew(floorBody, 4, verts2, cpv(-320.0f / 2, 81.0f / 2));
	
	floorShape->e = 0.5; floorShape->u = 0.1; floorShape->collision_type = 0;
	
	floorShape->data = floor;
	
	cpSpaceAddStaticShape(space, floorShape);
	
	floorShape = cpPolyShapeNew(floorBody, 4, verts3, cpv(-320.0f / 2, 81.0f / 2));
	
	floorShape->e = 0.5; floorShape->u = 0.1; floorShape->collision_type = 0;
	
	floorShape->data = floor;
	
	cpSpaceAddStaticShape(space, floorShape);
	
	floorShape = cpPolyShapeNew(floorBody, 4, verts4, cpv(-320.0f / 2, 81.0f / 2));
	
	floorShape->e = 0.5; floorShape->u = 0.1; floorShape->collision_type = 0;
	
	floorShape->data = floor;
	
	cpSpaceAddStaticShape(space, floorShape);
	
	floorShape = cpPolyShapeNew(floorBody, 4, verts5, cpv(-320.0f / 2, 81.0f / 2));
	
	floorShape->e = 0.5; floorShape->u = 0.1; floorShape->collision_type = 0;
	
	floorShape->data = floor;
	
	cpSpaceAddStaticShape(space, floorShape);
	
With them declared in the header.

Any help would be fantastic!

(The error is :

Code: Select all

Initializing Chipmunk v5.1.0 (Debug Enabled)
Compile with NDEBUG defined to disable debug mode and assert() checks
Assertion failed: (cpArrayContains(space->bodies, body)), function cpSpaceRemoveBody, file /Users/rob/Documents/Iphone Development/Basic Assign Ap/balls Up 0.25/chipmunk/cpSpace.c, line 321.
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by Tam Toucan »

No, you don't need to release the verts. The assertion is the check that the body exists in the space before you remove it. It doesn't exist therefore it was never added OR you re removing it twice.

Also, from the code you posted you can't remove all the shapes you added since you have overwritten your floorSpace. So how are you removing them?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Assertion failed error on remove of shape / body

Post by slembcke »

Static bodies are never added to the space. Because they never move or change, you need the space to update them and don't want it to apply gravity to them. If you added the static body to the space in the first place your simulation wouldn't have worked.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests