Try Colision, sometimes crash aplication

Official forum for the Chipmunk2D Physics Library.
gr_student
Posts: 9
Joined: Mon Oct 05, 2009 1:35 pm
Contact:

Try Colision, sometimes crash aplication

Post by gr_student »

I try use colision. sometimes i have bad excess. in cpArbiter.m

this is part of my code

my heroy (pickachu) digging ground.
and eating red mushrooms
I use colision to check which tile of gound need to be delete

Code: Select all

int groundCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)

{

//speed /2

HelloWorld* helscene=(HelloWorld*)data;

CGPoint checkedTiled=[helscene coordinatesAtPosition:b->body->p];

TMXLayer *orthoLayer=[helscene->ortho layerNamed:@"Layer 0"];
[orthoLayer removeTileAt:ccp(checkedTiled.x,checkedTiled.y)]
cpSpaceRemoveBody(helscene->space,b->body);

if (b->body->m == INFINITY)
cpSpaceRemoveStaticShape(helscene->space,b);
else
cpSpaceRemoveShape(helscene->space ,b);

cpBodyFree(b->body);
cpShapeFree(b);

return 1;
}

-(void)setupChipmunk{

cpInitChipmunk();

space =cpSpaceNew();

//space->gravity = cpv(0,-200);

space->elasticIterations = 1;

[self schedule: @selector(tick:) interval: 1.0f/60.0f];

pickachuBody=cpBodyNew(100.0, INFINITY);

pickachuBody->p= cpv(112,144);

cpSpaceAddBody(space, pickachuBody);

cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) }; 

cpShape* pickachuShape=cpPolyShapeNew(pickachuBody, 4, squared, cpvzero);

pickachuShape->e=0.8;

pickachuShape->u =0.8;

pickachuShape->data=sprite_h;

pickachuShape->collision_type =7; 

cpSpaceAddShape(space, pickachuShape);

//*-*-*-*-*-*-*-*-*

TMXLayer *orthoLayer=[ortho layerNamed:@"Layer 0"];

CGSize s=[orthoLayer layerSize];

cpBody *staticBody;

staticBody = cpBodyNew(INFINITY, INFINITY);

for (int y=0; y<s.height; y++) {

for (int x=0; x<s.width; x++) {

unsigned int tilegid=[orthoLayer tileGIDAt:ccp(x,y)];

//NSLog(@"(%d,%d)= %d",x,y,tilegid);

if(tilegid>0)

{

/* if(tilegid<=8) {

int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};

cpFloat xpos=x*32;

cpFloat ypos=reversDighit[y]*32;


cpShape* floorShape = cpSegmentShapeNew(staticBody, cpv(xpos,ypos+32), cpv(xpos+32,ypos+32), 0);

floorShape->e = 0.5;

floorShape->u = 0.1;

floorShape->collision_type = 0;

cpSpaceAddShape(space, floorShape);

}*/

//50 гриб можно сьесть

if(tilegid==50) {

int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};

cpFloat xpos=x*32;

cpFloat ypos=reversDighit[y]*32;

cpBody* mushroom2Body = cpBodyNew(INFINITY, INFINITY);

mushroom2Body->p = cpv(xpos+16, ypos+16);

cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) }; 

//cpSpaceAddBody(space, mushroomBody);

cpShape* mushroom2Shape = cpPolyShapeNew(mushroom2Body, 4, squared, cpvzero);

mushroom2Shape->e = 0.5;

mushroom2Shape->u = 0.1;

//mushroomShape->data=[orthoLayer tileAt:ccp(x,y)];

mushroom2Shape->collision_type = 5;

cpSpaceAddStaticShape(space, mushroom2Shape);

}

if(tilegid==40) {

int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};

cpFloat xpos=x*32;

cpFloat ypos=reversDighit[y]*32;

cpBody* groundBody = cpBodyNew(INFINITY, INFINITY);

groundBody->p = cpv(xpos+16, ypos+16);

cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) }; 

//cpSpaceAddBody(space, mushroomBody);

cpShape* groundShape = cpPolyShapeNew(groundBody, 4, squared, cpvzero);

groundShape->e = 0;

groundShape->u = 1;

//mushroomShape->data=[orthoLayer tileAt:ccp(x,y)];

groundShape->collision_type = 3;

cpSpaceAddStaticShape(space, groundShape);

}

//51 гриб можно двигать

/*if(tilegid==51) {


int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};

cpFloat xpos=x*32;

cpFloat ypos=reversDighit[y]*32;

//AtlasSprite* tmpSprite=[orthoLayer tileAt:ccp(x,y)];

//-Texture2D* textureForMushroom=[orthoLayer   spriteManagerWithTexture:<#(Texture2D *)tex#>][tmpSprite.textureRect]

//Sprite* tmpSp=[orthoLayer createSpriteWithRect:tmpSprite.textureRect];

//--Sprite* tmpSp=[Sprite spriteWithTexture:[tmpSprite->textureAtlas_]];

Sprite* moosh=[Sprite spriteWithFile:@"pickachu.png"];

[moosh setPosition:CGPointMake(xpos+16, ypos+16)];

tagindex++;

[self addChild:moosh z:1 tag:tagindex];

cpBody* mushroomBody = cpBodyNew(100, INFINITY);

mushroomBody->p = cpv(xpos+16, ypos+16);

cpSpaceAddBody(space, mushroomBody);

cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) }; 

cpShape* mushroomShape = cpPolyShapeNew(mushroomBody, 4, squared, cpvzero);

mushroomShape->e = 0.5;

mushroomShape->u = 0.1;

mushroomShape->data=moosh;

mushroomShape->collision_type = 6;

cpSpaceAddShape(space, mushroomShape);

[orthoLayer removeTileAt:ccp(x,y)];

}*/

}

}

}

cpSpaceAddCollisionPairFunc(space, 7, 5, &mushroomCollision, self);

cpSpaceAddCollisionPairFunc(space, 7, 3, &groundCollision, self);

//cpSpaceAddCollisionPairFunc(space, 7, 6, &mushroomCollisionP, self);

}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Try Colision, sometimes crash aplication

Post by slembcke »

http://code.google.com/p/chipmunk-physics/wiki/cpSpace

Read the big warning at the end of the collision callback section. In short, you can't remove stuff from within a callback.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
gr_student
Posts: 9
Joined: Mon Oct 05, 2009 1:35 pm
Contact:

Re: Try Colision, sometimes crash aplication

Post by gr_student »

I thought this warning is valid only when appointed by the two functions for each of the colliding objects.

how can I remove the object with which I colliding?
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Try Colision, sometimes crash aplication

Post by Tam Toucan »

Just build a list of the objects you want to remove and do it after cpSpaceStep has returned.
gr_student
Posts: 9
Joined: Mon Oct 05, 2009 1:35 pm
Contact:

Re: Try Colision, sometimes crash aplication

Post by gr_student »

how i can add object of cpShape in NSMutableArray?

maybe SpaceManager be easier to use?

(sorry for my english)
zaerl
Posts: 40
Joined: Fri Aug 28, 2009 8:36 am
Contact:

Re: Try Colision, sometimes crash aplication

Post by zaerl »

gr_student wrote:how i can add object of cpShape in NSMutableArray?
It's simple.

Code: Select all

cpShape *yourShape;

[yourArray addObject:[NSValue valueWithPointer:yourShape]];

cpShape *yourShapeFromArray = [[yourArray lastObject] pointerValue];
mobilebros
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am
Contact:

Re: Try Colision, sometimes crash aplication

Post by mobilebros »

If you do decide to use the spacemanager then you would just need to call (from your collision):

[spaceManager scheduleToRemoveAndFreeShape:b];

it'll handle both static and active shapes correctly.
gr_student
Posts: 9
Joined: Mon Oct 05, 2009 1:35 pm
Contact:

Re: Try Colision, sometimes crash aplication

Post by gr_student »

zaerl wrote:
gr_student wrote:how i can add object of cpShape in NSMutableArray?
It's simple.

Code: Select all

cpShape *yourShape;

[yourArray addObject:[NSValue valueWithPointer:yourShape]];

cpShape *yourShapeFromArray = [[yourArray lastObject] pointerValue];
Oh, thanks. very simple..
gr_student
Posts: 9
Joined: Mon Oct 05, 2009 1:35 pm
Contact:

Re: Try Colision, sometimes crash aplication

Post by gr_student »

I try use SpaceManager and have a question.

How i can use cpAtlisSprite? Because AtlisSprite it's cocos2d class which used from TMXTiledMap.

I need load Map

Code: Select all

	MapOfLevel = [TMXTiledMap tiledMapWithTMXFile:@"level2.tmx"];
		tagindex++;
		[self addChild:MapOfLevel z:0 tag:tagindex];
it's OK. then i need make shape (my ground tile and mushroom tile)

earlier a make this

Code: Select all

cpBody *staticBody;

staticBody = cpBodyNew(INFINITY, INFINITY);

for (int y=0; y<s.height; y++) {

for (int x=0; x<s.width; x++) {

unsigned int tilegid=[orthoLayer tileGIDAt:ccp(x,y)];
if(tilegid==40) {

int reversDighit[10]={9,8,7,6,5,4,3,2,1,0};

cpFloat xpos=x*32;

cpFloat ypos=reversDighit[y]*32;

cpBody* groundBody = cpBodyNew(INFINITY, INFINITY);

groundBody->p = cpv(xpos+16, ypos+16);
cpVect squared[] = { cpv(-16, -16), cpv(-16, 16), cpv(16, 16), cpv(16,-16) }; 
cpShape* groundShape = cpPolyShapeNew(groundBody, 4, squared, cpvzero);
groundShape->e = 0;
groundShape->u = 1;
groundShape->collision_type = 3;
cpSpaceAddStaticShape(space, groundShape);

}
}}
maybe now I can use the simpler code?
zaerl
Posts: 40
Joined: Fri Aug 28, 2009 8:36 am
Contact:

Re: Try Colision, sometimes crash aplication

Post by zaerl »

This is a question regarding the cocos-2d engine and not the chipmunk library. I think that you should write on the official cocos-2d forum.
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests