Sprites not totally removed from screen

Official forum for the Chipmunk2D Physics Library.
Post Reply
j03m4r13
Posts: 2
Joined: Thu Jan 21, 2010 7:44 pm
Contact:

Sprites not totally removed from screen

Post by j03m4r13 »

Hi there,

I have a problem about some sprite images not totally remove when I call the collision handler:

static void
postStepRemove(cpSpace *space, cpShape *shape, void *data)
{
HelloWorld *mb = (HelloWorld*) data;
[mb explode:shape->body->p.x y:shape->body->p.y];
cpSpaceRemoveBody(space, shape->body);
cpBodyFree(shape->body);
cpSpaceRemoveShape(space, shape);
cpShapeFree(shape);

}

static int
begin(cpArbiter *arb, cpSpace *space, void *data)
{
cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, b, data);
return 0;
}

and I bind the collision handler in my init method of my game CCLayer using this,

cpSpaceAddCollisionHandler(space, 2, 2, begin, NULL, NULL, NULL, self);

What I actually want is that when two non-static shapes collide, the other one will be removed from the scene. What happened is that, there are instances that when the 2 objects collide, the sprite of the other shape object remained on screen. Yet there are instances that it's doing exactly as what I expected.

Anyone who can give me some insights about this?

Thanks in advance!
User avatar
slembcke
Site Admin
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Sprites not totally removed from screen

Post by slembcke »

j03m4r13 wrote:What I actually want is that when two non-static shapes collide, the other one will be removed from the scene. What happened is that, there are instances that when the 2 objects collide, the sprite of the other shape object remained on screen. Yet there are instances that it's doing exactly as what I expected.
You are going to need to be a bit more clear when you are saying "other". The collision handler that you are using takes two shapes with collision type 2. I would assume that one of those shapes is a something like a player while the other is an enemy or something? If that's the case, you should be using different collision types for them. Your handler callbacks will always be called so that the shapes' collision types are in the same order as when you defined the handler. By using the same collision type twice, you have no way other than the data pointer to be able to tell which is which.

Now a couple tips:

Unless you really only have two collision types, I would strongly recommend that you use an enum to define them all. That way your compiler can track down problems for you if you need to change things later.

Lastly, you are doing this:

Code: Select all

 cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);
I got tired of typing that so I made a macro that does it. It's mentioned in the docs, but maybe I never changed the demos over to use it?

Code: Select all

#define CP_ARBITER_GET_SHAPES(arb, a, b) cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
j03m4r13
Posts: 2
Joined: Thu Jan 21, 2010 7:44 pm
Contact:

Re: Sprites not totally removed from screen

Post by j03m4r13 »

Hi slembcke,

Sorry for the late response.. I was so busy. I'm sorry for not being clear with my explaination. And it's my mistake there of putting the same collision_type, and after a few fiddling I made with my code plus your suggestion, I made it working now. About the use of enum, actually that's my ideal code design, but the last script I did was just for a quick test of your great engine, was just too lazy to put enums there. lol . Anyway, thanks very much for the response, I really appreciate it.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest