cpSpaceAddCollisionPairFunc is not collision callback functi

Official forum for the Chipmunk2D Physics Library.
Post Reply
waghman
Posts: 1
Joined: Mon Apr 13, 2009 6:06 am
Contact:

cpSpaceAddCollisionPairFunc is not collision callback functi

Post by waghman »

Hi All,

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]
Thanks in Advance...
Manu
chip007
Posts: 2
Joined: Thu Apr 16, 2009 2:12 am
Contact:

Re: cpSpaceAddCollisionPairFunc is not collision callback functi

Post by chip007 »

No one seems answering. I am also new in chipmunk and cocos2d for iphone. Waghman, I'm not aware about the answer, but could you please tell me how you came this much far in chipmunk and cocos2d to pile up this much mixture of both. I could not find how to use the chipmunk with cocos2d. Could you please point to some useful links?
chip007
Posts: 2
Joined: Thu Apr 16, 2009 2:12 am
Contact:

Re: cpSpaceAddCollisionPairFunc is not collision callback functi

Post by chip007 »

May be the following
Advanced topics
Advanced collision processing

Using collision pair functions, it’s possible to get information about a collision such as the locations of the contact points and the normals, but you can’t get the impulse applied to each contact point at the time the callback is called. Because Chipmunk caches the impulses, it is possible to access them after the call to cpSpaceStep() returns.

The impulse information is stored in the jnAcc and jtAcc fields of the cpContact structures passed to the collision pair function. So you must store a reference to the contacts array in the collision pair function so that you can access it later once the impulses have been calculated.


cpVect cpContactsSumImpulses(cpContact *contacts, int numContacts);
cpVect cpContactsSumImpulsesWithFriction(cpContact *contacts, int numContacts);

Sums the impulses applied to the the given contact points. cpContactsSumImpulses() sums only the normal components, while cpContactsSumImpulsesWithFriction() sums the normal and tangential componets.

Notes:

* The contact array will either be destroyed or out of date after the next call to cpSpaceStep(). If you need to store the collision information permanently, you’ll have to copy it.

Collision pair functions, groups, and layers

There are three ways that you can specify which shapes are allowed to collide in Chipmunk: collision pair functions, groups, and layers. What are their intended purposes?

Collision pair functions: More than just a callback, collision pair functions can conditionally allow a collision between two shapes. This is the only choice if you need to perform some logic based on the shapes or contact information before deciding to allow the collision.

Groups: Groups filter out collisions between objects in the same non-zero groups. A good example of when groups would be useful is to create a multi-body, multi-shape object such as a ragdoll. You don’t want the parts of the ragdoll to collide with itself, but you do want it to collide with other ragdolls.

Layers: Layers are another way of grouping shapes. Collision between shapes that don’t occupy one or more of the same layers are filtered out. Layers are implemented using a bitmask on an unsigned long, so there are up to 32 layers available.

To be clear, for a collision to occur, all of the tests have to pass. Additionally, collisions between static shapes are not considered nor are collisions between shapes connected to the same rigid body.
from http://files.slembcke.net/chipmunk/chipmunk-docs.html could be of some help in your scenario.
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests