#pragmaOnce#include"cocos2d.h"using_ns_cc;classParabola: Publicactioninterval{ Public: Staticparabola* Create (Constccpoint& StartPosition,Constccpoint&Endposition,floatAnglefloatGfloat&return_duration); //Override Virtualparabola* Clone ()Const Override; Virtualparabola* reverse ()Const Override; Virtual voidUpdatefloatTimeOverride;protected: BOOLInitwithduration (floatDurationConstccpoint&StartPosition,Constccpoint& Endposition,floatAnglefloatGfloatVx0,floatvy0);protected: floatm_vx0; floatm_vy0; Point M_startposition; Point M_endposition; floatM_angle; floatM_dur; floatm_tan_a; floatM_g;};
#include"Parabola.h"#definePixels_per_meter 100parabola* Parabola::create (Constccpoint& StartPosition,Constccpoint&Endposition,floatAnglefloatGfloat&return_duration) {parabola*pret =Newparabola (); floatvx0,vy0, x1, y1, duration; X1= Endposition.x-startposition.x; Y1= ENDPOSITION.Y-STARTPOSITION.Y; X1/= Pixels_per_meter;//pixel-to-meter conversion, pixels_per_meter as the conversion factor, set here toY1/=Pixels_per_meter; Angle= angle*3.14/ the;//convert T to radian if(x1<0) {Angle*= -1; } vx0= X1*sqrt (g/2/(X1*tan (angle)-y1));//finding the initial velocityVy0 = vx0 *Tan (angle); Duration= x1/vx0;//to find out the time of the whole movementreturn_duration = Duration;//returns the duration by parameterPret->initwithduration (duration,startposition, Endposition, Angle,g, vx0, vy0); PRet-autorelease (); returnPRet;}BOOLParabola::initwithduration (floatDurationConstpoint& StartPosition,Constpoint& Endposition,floatAnglefloatGfloatVx0,floatvy0) { if(ccactioninterval::initwithduration (duration)) {m_vx0=vx0; M_vy0=vy0; M_startposition=startposition; M_endposition=endposition; Angle= angle*3.14/ the;//convert T to radianM_angle =angle; M_dur=duration; M_tan_a=Tan (angle); M_g=G; return true; } return false;} Parabola* Parabola::clone ()Const{Cc_assert (0); returnnullptr;} Parabola* Parabola::reverse ()Const{Cc_assert (0); returnnullptr;}voidParabola::update (floatTime ) { if(_target) {floatelapsed = _elapsed;//get the current workout time floatdiff_x = m_vx0 *elapsed; floatdiff_y = m_vy0 * Elapsed-0.5* M_g * Elapsed *elapsed; Ccpoint Newpos= Ccpadd (M_startposition, CCP (diff_x * pixels_per_meter, diff_y * pixels_per_meter));//units converted from meters to pixels_target->setposition (Newpos); }}
COCOS3 Parabolic Motion