i stuck @ position , don't know went wrong in this,
i have enabled arc in project. , made softbody follows
ball.h
b2body *body[num_segment]; ccsprite *ball;
ball.mm
ball = [ccsprite spritewithfile:@"ball1.2.png"]; ball.tag = 1; for(int i=0;i<num_segment;i++){ float theta = deltaangle*i; float x = radius * cosf(theta); float y = radius * sinf(theta); b2vec2 circleposition = b2vec2(x/ptm_ratio,y/ptm_ratio); b2bodydef bodydef; bodydef.type = b2_dynamicbody; bodydef.position = (center + circleposition); bodydef.userdata = &ball; body[i] = world->createbody(&bodydef); outerbodyfixture[i]=body[i]->createfixture(&fixturedef); [bodies addobject:[nsvalue valuewithpointer:body[i]]]; }
and have given physics tiles follows,
tile.h
ccsprite *tile;
tile.mm
tile = [layer1 tileat:ccp(i, j)]; tile.tag = 0; b2bodydef tiledef; tiledef.type = b2_staticbody; tiledef.position.set((tile.position.x+(tile.contentsize.width/2))/(ptm_ratio), (tile.position.y + (tile.contentsize.height/2))/ptm_ratio); tiledef.userdata = &tile; tilebody = world->createbody(&tiledef);
now tried catch collision detection , have made code print tag number of colliding bodies. code follows,
std::vector<mycontact>::iterator pos; (pos=_contactlistener->_contacts.begin(); pos != _contactlistener->_contacts.end(); ++pos) { mycontact contact = *pos; b2body *bodya = contact.fixturea->getbody(); b2body *bodyb = contact.fixtureb->getbody(); if (bodya->getuserdata() != null && bodyb->getuserdata() != null) {
at point getting error: exc_bad_access
ccsprite *spritea = (__bridge ccsprite *) bodya->getuserdata();
at point getting error: exc_bad_access
ccsprite *spriteb = (__bridge ccsprite *) bodyb->getuserdata(); printf("contact :%d \n",spriteb.tag); } }
don't know whats wrong code,,give me solution this
your problem store in userdata
pointer pointer rather pointer itself.
tile
pointing ccsprite
instance , &tile
points pointer ccsprite
instance making casting incorrect.
so change :
tiledef.userdata = tile;
Comments
Post a Comment