objective c - Box2D dynamic body falling off screen -
i have rectangular object (like spear) , have ground body. problem is, when spear hits ground, doesn't bounce back, falls though , off screen. here physics setup: (ignore ball reference, suppose called spear (rectangular))
-(id) init { if( (self=[super init])) { cgsize winsize = [ccdirector shareddirector].winsize; self.isaccelerometerenabled = yes; self.istouchenabled = yes; // create sprite , add layer _ball = [ccsprite spritewithfile:@"spear.png" rect:cgrectmake(0, 0, 100, 10)]; _ball.position = ccp(100, 100); [self addchild:_ball]; // create world b2vec2 gravity = b2vec2(0.0f, -20.0f); bool dosleep = true; _world = new b2world(gravity, dosleep); // create edges around entire screen b2bodydef groundbodydef; groundbodydef.position.set(0,0); b2body *groundbody = _world->createbody(&groundbodydef); b2polygonshape groundbox; b2fixturedef boxshapedef; boxshapedef.shape = &groundbox; groundbox.setasedge(b2vec2(0,0), b2vec2(winsize.width/ptm_ratio, 0)); groundbody->createfixture(&boxshapedef); groundbox.setasedge(b2vec2(0,0), b2vec2(0, winsize.height/ptm_ratio)); groundbody->createfixture(&boxshapedef); groundbox.setasedge(b2vec2(0, winsize.height/ptm_ratio), b2vec2(winsize.width/ptm_ratio, winsize.height/ptm_ratio)); groundbody->createfixture(&boxshapedef); groundbox.setasedge(b2vec2(winsize.width/ptm_ratio, winsize.height/ptm_ratio), b2vec2(winsize.width/ptm_ratio, 0)); groundbody->createfixture(&boxshapedef); // create ball body , shape b2bodydef ballbodydef; ballbodydef.type = b2_dynamicbody; ballbodydef.position.set(100/ptm_ratio, 100/ptm_ratio); ballbodydef.userdata = _ball; b2body *_body = _world->createbody(&ballbodydef); b2polygonshape spearshape; spearshape.setasbox(100/ptm_ratio, 10/ptm_ratio); b2fixturedef ballshapedef; ballshapedef.shape = &spearshape; ballshapedef.density = 1.0f; ballshapedef.friction = 0.9f; ballshapedef.restitution = 0.89f; b2vec2 force; force.set(_body->getlinearvelocity().x+5.0f, _body->getlinearvelocity().y+10.0f); _body->setlinearvelocity(force); [self schedule:@selector(tick:)]; } return self; } - (void)tick:(cctime) dt { _world->step(dt, 10, 10); for(b2body *b = _world->getbodylist(); b; b=b->getnext()) { if (b->getuserdata() != null) { ccsprite *balldata = (ccsprite *)b->getuserdata(); balldata.position = ccp(b->getposition().x * ptm_ratio, b->getposition().y * ptm_ratio); balldata.rotation = -1 * cc_radians_to_degrees(b->getangle()); } } }
i don't see anywhere in code attaching shapes ball , spear. if body not have shape - not collide.
Comments
Post a Comment