c++ - Box2d SetLinearVelocity weird issue -


i trying b2body object follow touch location in constant speed , have problem in "y" axis in body go location on "other half of screen" mirror image...the "x" axis fine though. (i using cocos2d 1.0.0)

here code:

helloworldlayer.h

#import "cocos2d.h" #import "box2d.h"  #define ptm_ratio 32.0  @interface helloworldlayer : cclayer {     b2world *_world;     b2body *_body;     ccsprite *_ball; }  + (id) scene;  @end 

helloworldlayer.m

#import "helloworldlayer.h" #import "cgpointextension.h"  static cgpoint location;  @implementation helloworldlayer  + (id)scene {      ccscene *scene = [ccscene node];     helloworldlayer *layer = [helloworldlayer node];     [scene addchild:layer];     return scene;  }  - (id)init {      if ((self=[super init])) {         cgsize winsize = [ccdirector shareddirector].winsize;          // create sprite , add layer         _ball = [ccsprite spritewithfile:@"ball.png" rect:cgrectmake(0, 0, 52, 52)];         _ball.position = ccp(100, 300);         [self addchild:_ball];          // create world         b2vec2 gravity = b2vec2(0.0f, 0.0f);         _world = new b2world(gravity);          // create edges around entire screen         b2bodydef groundbodydef;         groundbodydef.position.set(0,0);          b2body *groundbody = _world->createbody(&groundbodydef);         b2edgeshape groundedge;         b2fixturedef boxshapedef;         boxshapedef.shape = &groundedge;          //wall definitions         groundedge.set(b2vec2(0,0), b2vec2(winsize.width/ptm_ratio, 0));         groundbody->createfixture(&boxshapedef);          groundedge.set(b2vec2(0,0), b2vec2(0,winsize.height/ptm_ratio));         groundbody->createfixture(&boxshapedef);          groundedge.set(b2vec2(0, winsize.height/ptm_ratio),                        b2vec2(winsize.width/ptm_ratio, winsize.height/ptm_ratio));         groundbody->createfixture(&boxshapedef);          groundedge.set(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, 300/ptm_ratio);         ballbodydef.userdata = _ball;         _body = _world->createbody(&ballbodydef);          b2circleshape circle;         circle.m_radius = 26.0/ptm_ratio;          b2fixturedef ballshapedef;         ballshapedef.shape = &circle;         ballshapedef.density = 1.0f;         ballshapedef.friction = 0.2f;         ballshapedef.restitution = 0.8f;         _body->createfixture(&ballshapedef);          [self schedule:@selector(tick:)];         [self settouchenabled:yes];         [self setaccelerometerenabled:yes];     }     return self; }  - (void)tick:(cctime) dt {      int32 velocityiterations = 8;     int32 positioniterations = 1;     _world->step(dt, velocityiterations, positioniterations);      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());         }     }  }  -(void)cctouchesmoved:(nsset *)touches withevent:(uievent *)event{      uitouch *touch = [[event alltouches] anyobject];      location = [touch locationinview:touch.view];     nslog(@"%@", nsstringfromcgpoint(location));   } -(void) cctouchesbegan:(nsset *)touches withevent:(uievent *)event{      uitouch *touch = [[event alltouches] anyobject];     location = [touch locationinview:touch.view];      [self schedule:@selector(asd:)];   }  -(void) cctouchesended:(nsset *)touches withevent:(uievent *)event{     // [self unschedule:@selector(asd:)];    // _body->setlinearvelocity(b2vec2(0,0));    // _body->setangularvelocity(0);     //_body->setactive(false); }  -(void)asd:(cctime) dt {      nslog(@"%@", nsstringfromcgpoint(location));      //float ca = location.y;          b2vec2 convertedlocation = b2vec2(location.x/ptm_ratio - _body->getposition().x, location.y/ptm_ratio - _body->getposition().y);          //b2vec2 totouchpoint = convertedlocation - _body->getposition();          convertedlocation.normalize();         b2vec2 impulse = 5 * convertedlocation;          _body->setlinearvelocity(impulse);       //  _body->applylinearimpulse(_body->getmass() * impulse, _body->getworldcenter());     }   - (void)dealloc {     delete _world;     _body = null;     _world = null;     [super dealloc]; }  @end 

thanks in advance!

when location touch using locationinview, origin @ top left corner. in cocos2d origin lies @ bottom left corner. can flip y value. or alternatively can use converttogl function of ccdirector. prefer later because handles portrait , landscape modes.

cgpoint loc = [touch locationinview:touch.view] location = [[ccdirector shareddirector] converttogl:loc]; 

Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -