objective c - Cocos2d rotation on touches moved problem -


i have spear sprite. rotation decided touchesmoved method. whenever user slides finger points towards touch. method:

- (void)cctouchesmoved:(nsset *)touches withevent:(uievent *)event {      uitouch* touch = [touches anyobject];     cgpoint location = [touch locationinview: [touch view]];       float angleradians = atanf((float)location.y / (float)location.x);     float angledegrees = cc_radians_to_degrees(angleradians);      spear.rotation = -1 * angledegrees;  } 

this kinda works, 0 45 degrees. , goes opposite. moving finger bottom top, rotates clockwise (it should follow direction of fnger , rotate counter clockwise). 45 90, works fine (moves counter clickwise) if start touch in upper diagonal of screen.

what doing wrong? thanks

#define ptm_ratio 32  - (void)cctouchesmoved:(nsset *)touches withevent:(uievent *)event{      for( uitouch *touch in touches ) {          cgpoint location = [touch locationinview: [touch view]];          location = [[ccdirector shareddirector] converttogl: location];          b2vec2 locationworld = b2vec2(location.x/ptm_ratio, location.y/ptm_ratio);          spritebody->settransform(locationworld,spritebody->getangle());     } }  -(void) tick: (cctime) dt {      //it recommended fixed time step used box2d stability     //of simulation, however, using variable time step here.     //you need make informed choice, following url useful     //http://gafferongames.com/game-physics/fix-your-timestep/      int32 velocityiterations = 8;     int32 positioniterations = 1;      // instruct world perform single step of simulation.     // best keep time step , iterations fixed.     m_world->step(dt, velocityiterations, positioniterations);      //  (int = 0; < (int)birds.size(); i++)     //      birds[i]->render();      //iterate on bodies in physics world     (b2body* b = m_world->getbodylist(); b; b = b->getnext())     {         if (b->getuserdata() != null) {             //synchronize atlassprites position , rotation corresponding body             ccsprite *myactor = (ccsprite*)b->getuserdata();             myactor.position = cgpointmake( b->getposition().x * ptm_ratio, b->getposition().y * ptm_ratio);             myactor.rotation = -1 * cc_radians_to_degrees(b->getangle());         }        } } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -