Page 1 of 1

how to "turn off" collision temporarily?

Posted: Mon Jun 29, 2009 9:02 pm
by bonjouryentinglai
Hi, this is my first time using chipmunk for iphone game development.
chipmunk is easy to use and do me a lot of favor.
In my program, I want to put some balls and let make them bounce inside the screen.
I create 4 static shape along the screen:

Code: Select all

cpBody *bottomBody = cpBodyNew(INFINITY, INFINITY);
bottomBody->p = cpv(0, 0);
cpShape *bottomShape = cpSegmentShapeNew(bottomBody, cpv(0, 0), cpv(320, 0), 0);
bottomShape->e = 1.0;
bottomShape->u = 0.0;
bottomShape->collision_type = 0;
bottomShape->group = 1;
cpSpaceAddStaticShape(space, bottomShape);

cpBody *rightBody = cpBodyNew(INFINITY, INFINITY);
rightBody->p = cpv(0, 0);
cpShape *rightShape = cpSegmentShapeNew(rightBody, cpv(320, 0), cpv(320, 480), 0);
rightShape->e = 1.0;
rightShape->u = 0.0;
rightShape->collision_type = 0;
rightShape->group = 1;
cpSpaceAddStaticShape(space, rightShape);

cpBody *topBody = cpBodyNew(INFINITY, INFINITY);
topBody->p = cpv(0, 0);
cpShape *topShape = cpSegmentShapeNew(topBody, cpv(0, 480), cpv(320,480), 0);


topShape->e = 1.0;
topShape->u = 0.0;
topShape->collision_type = 0;
topShape->group = 1;
cpSpaceAddStaticShape(space, topShape);

cpBody *leftBody = cpBodyNew(INFINITY, INFINITY);
leftBody->p = cpv(0, 0);
cpShape *leftShape = cpSegmentShapeNew(leftBody, cpv(0, 0), cpv(0, 480), 0);
leftShape->e = 1.0;
leftShape->u = 0.0;
leftShape->collision_type = 0;
leftShape->group = 1;
cpSpaceAddStaticShape(space,leftShape);
So if I put some balls inside the region and give them velocity, they will move and bounce inside the screen automatically. Quiet simple!

What I want to do now is let the ball move inside the screen from outside of the screen then bounce
inside the screen.

Is there a way to "turn off" the collision temporarily, or the ball outside the screen will first hit the "wall"
then move out of the screen.

I have tried put the ball at the edge of the screen, however when the ball move into the screen it looks like
the ball first hit the wall then into the screen (not move into the screen smoothly).

Can anyone give me some suggestion?
Thank for your help.

Re: how to "turn off" collision temporarily?

Posted: Tue Jun 30, 2009 12:02 am
by slembcke
I was thinking about implementing something similar at one point.

You can probably fake it pretty well using collision layers. Put the screen border line segment shapes on layer 1. When adding new balls, put them only in collision layer 2, but add the bit for collision layer 1 once they are all the way inside of the screen boundary. That way balls will always collide with each other, but will only hit the screen border once they've made it all the way in.

Re: how to "turn off" collision temporarily?

Posted: Tue Jun 30, 2009 7:27 am
by bonjouryentinglai
Thank for your suggestion. I think this will work and I will try later.
And I think I need to put a loop to detect if any ball is inside the screen.

So, if this is the normal method other people use to?
or I should not put four static shape along the screen border and use other method?

Re: how to "turn off" collision temporarily?

Posted: Tue Jun 30, 2009 5:08 pm
by slembcke
There isn't really a "normal" way of doing this, and to my knowledge it's never been used.