cpMouse Implementation Problems

Official forum for the Chipmunk2D Physics Library.
Post Reply
G-Ram
Posts: 5
Joined: Thu Aug 13, 2009 9:14 pm
Contact:

cpMouse Implementation Problems

Post by G-Ram »

I implemented the cpMouse code into my project. There are no errors or warnings, but when I run the program it quits and the console reads "Assertion fail: (body != NULL), function cpShapeInit."
Here is my code:
extern void makeStaticBox(float x, float y, float width, float height);
cpSpace *space;
cpBody *staticBody
...
Sprite *Block1 = [[Sprite spriteWithFile:@"1x1.png"] retain];
Block1.position = cpv(160,240);
[self addChild: Block1];

cpVect verts[] = {
cpv(-36,-36),
cpv(-36, 36),
cpv(36, 36),
cpv(36,-36),
};

cpBody *Block1Body = cpBodyNew(50.0f, INFINITY);
Block1Body->p = cpv(160,240);
Block1Body->v = cpv(0, 0);

cpSpaceAddBody(space, Block1Body);

cpShape * Block1Shape = cpPolyShapeNew(Block1Body, 4, verts, cpvzero);
Block1Shape->e = 0.9f;
Block1Shape->u = 0.9f;
Block1Shape->data = Block1;
Block1Shape->collision_type = 0;
cpSpaceAddShape(space, Block1Shape);
CGSize s = [[Director sharedDirector] winSize];
int margin = 4;
int dmargin = margin*2;
makeStaticBox(margin, margin, s.width - dmargin, s.height - dmargin); //This references a static box the size of the window in a C class.
}
return self;
}
Please help!
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: cpMouse Implementation Problems

Post by Tam Toucan »

Can't you get a stack trace? That would help. You are calling

Code: Select all

makeStaticBox(margin, margin, s.width - dmargin, s.height - dmargin); //This references a static box the size of the window in a C class.
Hmmmm, and you have

Code: Select all

cpBody *staticBody
Where does that get set up and referenced?
G-Ram
Posts: 5
Joined: Thu Aug 13, 2009 9:14 pm
Contact:

Re: cpMouse Implementation Problems

Post by G-Ram »

The cpBody *staticBox is referenced in the static box created in another class. Here is the code:
extern cpSpace* space;
extern cpBody *staticBody;

void makeStaticBox(float x, float y, float width, float height){
cpShape * shape;
shape = cpSegmentShapeNew(staticBody, cpv(x,y), cpv(x+width, y), 0.0f); //Specifically Here
shape->e = 1.0; shape->u = 1.0;
cpSpaceAddStaticShape(space, shape);

shape = cpSegmentShapeNew(staticBody, cpv(x+width, y), cpv(x+width, y+height ), 0.0f);
shape->e = 1.0; shape->u = 1.0;
cpSpaceAddStaticShape(space, shape);

shape = cpSegmentShapeNew(staticBody, cpv(x+width, y+height), cpv(x, y+height ), 0.0f);
shape->e = 1.0; shape->u = 1.0;
cpSpaceAddStaticShape(space, shape);

shape = cpSegmentShapeNew(staticBody, cpv(x, y+height ), cpv(x, y), 0.0f);
shape->e = 1.0; shape->u = 1.0;
cpSpaceAddStaticShape(space, shape);
}
Thanks! I am working on a stack trace.
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: cpMouse Implementation Problems

Post by Tam Toucan »

Still don't see the code where you set up staticBody i.e. you call

Code: Select all

shape = cpSegmentShapeNew(staticBody,...
but that will crash unless staticBody has been created i.e. staticBody will be NULL so the assert will trigger.
G-Ram
Posts: 5
Joined: Thu Aug 13, 2009 9:14 pm
Contact:

Re: cpMouse Implementation Problems

Post by G-Ram »

Thanks so much!
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests