COCOS2DX extended Ccaction implements sprite parabolic motion

Source: Internet
Author: User

Simulates the parabolic motion of an object under gravity (ignoring air resistance) without the use of a physical engine. The position of the starting point of the movement, the end position, the gravitational acceleration g and the firing angle of the starting point are given as parameters.

First, the initial velocity of the object on the x-axis and the y-axis is obtained according to the known conditions, and then the motion trajectory is obtained according to the formula F (t) =v0t+0.5*at2.

Class ccparabolamove inherit from Ccaction.

1ccparabolamove* Ccparabolamove::create (Constccpoint& StartPosition,Constccpoint& Endposition,floatAnglefloatGfloat&return_duration)2 {3Ccparabolamove *pret =NewCcparabolamove ();4     floatvx0,vy0, x1, y1, duration;5 6X1 = endposition.x-startposition.x;7Y1 = endposition.y-Startposition.y
8X1/=Pixels_per_meter;//pixels to meters conversion, pixels_per_meter for conversion coefficients, set here to9Y1/=Pixels_per_meter;TenAngle = angle*3.14/ the;//convert T to radian One A if(x1<0) - { -Angle *=-1; the } - -vx0 = X1*sqrt (g/2/(X1*tan (angle)-y1)); //Find the initial speed -Vy0 = vx0 *Tan (angle); + -Duration = x1/vx0; //Find out the time of the whole movement +Return_duration =duration; //Duration is returned by parameter
APret->initwithduration (duration,startposition, Endposition, Angle,g, vx0, vy0); atPret->autorelease (); - - returnPRet; -}

Create () is a static function, so assigning values to non-static member variables is implemented in Initwithduration ():

1 BOOLCcparabolamove::initwithduration (floatDurationConstccpoint& StartPosition,Constccpoint& Endposition,floatAnglefloatGfloatVx0,floatvy0)2 {3     if(ccactioninterval::initwithduration (duration))4     {5m_vx0 =vx0;6M_vy0 =vy0;7M_startposition =startposition;8M_endposition =endposition;9Angle = angle*3.14/ the;//convert T to radianTenM_angle =angle; OneM_dur =duration; AM_tan_a =Tan (angle); -M_g =G; -         return true; the     } -  -     return false; -}

At Runaction, Startwithtarget (Ccnode *ptarget) is called first, through Ptarget, as an object pointer to the execution of the action:

1 void Ccparabolamove::startwithtarget (Ccnode *ptarget)2{3     Ccactioninterval::startwithtarget (ptarget); 4 5 }

The parameter T in update (float t) represents the ratio of the current motion time to the duration of the entire motion, ranging from 0 to 1.

1 voidCcparabolamove::update (floatt)2 {3     if(M_ptarget)4     {5         floatElapsed = T *m_fduration; //Get the current exercise time 6         floatdiff_x = m_vx0 *elapsed;7         floatdiff_y = m_vy0 * Elapsed-0.5* M_g * Elapsed *elapsed;8 9Ccpoint Newpos = Ccpadd (M_startposition, CCP (diff_x * pixels_per_meter, diff_y *pixels_per_meter)); //units converted from meters to pixels TenM_ptarget->setposition (newpos); One  A      -     } -}

COCOS2DX extended Ccaction implements sprite parabolic motion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.