ios - How to make sprite move in a sinusoidal in cocos2d? -


i have sprite (paper plane, example). i'd make move in picture below. can use lots of moveto , rotateby actions define path points, seems bad idea me. how can implemented ?

enter image description here

i thought might post answer showed basics of how update work if had explicit control on sprite.

i not sure if using cocos2d or cocos2d-x, technique applies in either case. code in c++ using cocos2d-x.

the idea that, based on time, (manually) update position of sprite. position of sprite @ time determined number of seconds since animation begun. line nominally follows straight path (x0,y0) (x1,y0). can project line onto line drawn @ angle using trigonometry. gives ability have sinusoidal path along direction.

here basic code (the main work done in updateanimation()):

// assumes frame rate relatively constant // @ 60 fps. const double seconds_per_tick = 1.0/60; const double duration = 8.0;     // seconds total animation. const double x_start = 100;      // pixels const double y_start = 200;      // pixels const double x_stop = 800;      // pixels const double x_speed = (x_stop-x_start)/duration; const double y_period = 4.0;     // seconds y cycle. const double y_height = 100; const double launch_angle = m_pi/4; // angle line. const ccpoint anchor(x_start,y_start);  ccpoint rotatepointaboutanchor(const ccpoint& pt,double theta,const ccpoint& anchor) {    double xprime = cos(theta) * (pt.x-anchor.x) - sin(theta) * (pt.y-anchor.y) + anchor.x;    double yprime = sin(theta) * (pt.x-anchor.x) + cos(theta) * (pt.y-anchor.y) + anchor.y;     return ccpoint(xprime,yprime); }   void helloworld::initanimation() {    _ticks = 0;    _tickstotal = duration/seconds_per_tick; }  void helloworld::updateanimation() {    if(_ticks <= _tickstotal)    {       double seconds = _ticks*seconds_per_tick;       double xpos = x_start + seconds*x_speed;       double ypos = y_start + y_height*sin(seconds*2*m_pi/y_period);       ccpoint pos = rotatepointaboutanchor(ccpoint(xpos,ypos), launch_angle, anchor);       // set position of sprite       _sprite->setposition(pos);        cclog("tick: %d, seconds: %5.2f, position: (%f,%f)",_ticks,seconds,pos.x,pos.y);       if(_ticks%10 == 0)       {  // add trail          ccsprite* marker = ccsprite::create("icon-72.png");          marker->setscale(0.1);          marker->setposition(_sprite->getposition());          marker->setzorder(50);          addchild(marker);       }       // increment ticks count next time.       _ticks++;    } }  void helloworld::draw() {    cclayer::draw();    ccpoint start;    ccpoint stop;     start = rotatepointaboutanchor(ccpoint(x_start,y_start), launch_angle, anchor);    stop = rotatepointaboutanchor(ccpoint(x_stop,y_start), launch_angle, anchor);    ccdrawline(start,stop);     start = rotatepointaboutanchor(ccpoint(x_start,y_start+y_height), launch_angle, anchor);    stop = rotatepointaboutanchor(ccpoint(x_stop,y_start+y_height), launch_angle, anchor);    ccdrawline(start,stop);     start = rotatepointaboutanchor(ccpoint(x_start,y_start-y_height), launch_angle, anchor);    stop = rotatepointaboutanchor(ccpoint(x_stop,y_start-y_height), launch_angle, anchor);    ccdrawline(start,stop); }  void helloworld::onentertransitiondidfinish() {    initanimation();    scheduleupdate(); }  void helloworld::onexittransitiondidstart() {    unscheduleupdate(); }  void helloworld::update(float dt) {    updateanimation(); } 

i drew markers show path , drew lines "around" path should followed. here looks like:

enter image description here

you can change launch_angle make move along different angles.

obviously not production code, demonstrate idea can follow sinusoidal path in direction. should encapsulate more in line application.

the entire code base available on git hub.

and there more posts stuff in blog.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -