I am new in chipmunk and cocos2d for iphone. I working on test project & concept is 4-5 moving ball object are collide to each other and i want play sound when collide but I am not able to call collision call back function from cpSpaceAddCollisionPairFunc(space, collGrpEemy1, collGrpRedBall, &collFunc, &some_value); Not able to call collFunc here. This is code is same as chipmunk demo1 to 6 file. Even i was not able to collide with screen border list static body and active body collision in other project. Elasticity also not work at all...
I just pasted my code here plz tell me where is my mistake.. i just bang my head from last 4 days...
Code: Select all
@implementation GameScene
- (id) init {
self = [super init];
if (self != nil)
{
id layer = [ColorLayer layerWithColor: 0x902070ff]; //RGBA
[self add:layer z:1 tag:1];
[self add:[GameLayer node] z:1];
}
return self;
}
@end
@implementation GameLayer
@synthesize balls;
@synthesize redBall;
- (id) init
{
self = [super init];
if (self != nil)
{
isTouchEnabled = YES;
isAccelerometerEnabled = YES;
[self initChipmunk];
//seed the random generator
srand([[NSDate date] timeIntervalSince1970]);
collide = false;
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
}
return self;
}
- (void) initChipmunk
{
cpInitChipmunk();
space = cpSpaceNew();
cpSpaceResizeStaticHash(space, 4, 20);
cpSpaceResizeActiveHash(space, 50.0, 500);
space->gravity = cpv(0, 0);
space->iterations = 20;
staticBody = cpBodyNew(INFINITY, INFINITY);
[self schedule: @selector(step:)];
CGSize window = [[Director sharedDirector] winSize];
float margin = 4;
float dmargin = margin*2;
// add line border at screen
makeBorder(margin, margin, window.width - dmargin, window.height - dmargin);
self.balls = [[NSMutableArray alloc] init];
[self initBalls];
}
- (void) draw
{
glColor4f(1.0, 1.0, 1.0, 0.7);
cpSpaceHashEach(space->activeShapes, &eachShape, NULL);
glColor4f(1.0, 1.0, 1.0, 0.7);
cpSpaceHashEach(space->staticShapes, &drawObject, NULL);
glColor4f(1.0, 1.0, 1.0, 0.7);
cpSpaceHashEach(space->activeShapes, &eachShape, NULL);
}
-(void) updateSpace
{
int substeps = 3;
/* This is the actual time step that we will use. */
cpFloat dt = (1.0f/60.0f) / (cpFloat)substeps;
/* Finally, we step the space */
for(int i=0; i<substeps; i++)
{
cpSpaceStep(space, dt);
}
cpSpaceRehashStatic(space);
}
-(void) step: (ccTime) delta
{
[b] // check each moving object collision.. collGrpEemy1= 1st object & collGrpRedBall= 2nd object. some_value = 40
// collFunc is function which should be invoke it.[/b]
cpSpaceAddCollisionPairFunc(space, collGrpEemy1, collGrpRedBall, &collFunc, &some_value);
....
....
....
Ball position update logic is here
[self updateSpace];
cpSpaceHashEach(space->activeShapes, &eachShape, nil);
cpSpaceHashEach(space->staticShapes, &drawObject, nil);
}
-(void) initBalls
{
for(int i=0; i<5; i++)
{
[self addNewenemy:0 y:0];
}
for( BallSprite *ball in balls )
{
[self add:ball.ballSprite z:i++ tag:kTagSpriteR1 + i];
CGSize spriteSize = ball.ballSprite.texture.contentSize;
float wi = spriteSize.width/2 -2;
[self add:ball.ballSprite z:i++ tag:kTagSpriteR1 + i];
ball.spriteBody = cpBodyNew(1.0, cpMomentForCircle(10.0, wi, wi, cpvzero));
cpBody *body = ball.spriteBody;
body->p = cpv(ball.ballSprite.position.x ,ball.ballSprite.position.y);
cpSpaceAddBody(space, body);
cpShape *shape = cpCircleShapeNew(body, spriteSize.width/2-4, cpvzero);
shape->e = 0.95f; shape->u = 1.5f;
shape->data = ball.ballSprite;
shape->collision_type = collGrpEemy1 + i;
shape->group = collGrpEemy1;
cpSpaceAddShape(space, shape);
// Add a collision callback between objects of the default type and the box.
[b]cpSpaceAddCollisionPairFunc(space, 1, 0, collFunc, NULL);[/b]
}
[self initRedBall];
}
-(void) addNewenemy:(float) x y:(float)y
{
cpVect startPosition = cpv( random()%480, random()%320);
cpVect velocity = cpv( random()%200, random()%150 );
BallSprite *tempBall = [[BallSprite alloc] initWithPosition:startPosition spriteName:@"r1.png"];
tempBall.velocity = velocity;
[self.balls addObject:tempBall];
[tempBall release];
}
-(void) initRedBall
{
redBall = [Sprite spriteWithFile:@"ball.png"];
[redBall setTag:kTagSpriteRedBall];
redBallSize = redBall.texture.contentSize;
bTouchBall = YES;
[self add:redBall];
cpBody *redBody = cpBodyNew(1.0, cpMomentForCircle(1.0, redBallSize.width/2, redBallSize.height/2, cpvzero));
redBody->p = cpv(240,160);
circle = redBody;
cpSpaceAddBody(space, redBody);
cpShape *shape = cpCircleShapeNew(redBody, redBallSize.width/2, cpvzero);
shape->e = 0.9f; shape->u = 1.5f;
shape->data =redBall;
shape->collision_type = collGrpRedBall;
cpSpaceAddShape(space, shape);
}
[b]static int collFunc(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
printf("\n I am in coll Func ");
if(a->collision_type == collGrpEemy1)
{
return 0;
}
else
{
collide = true;
// play sound
return 1;
}
}[/b]
Manu