The cocos2d-x2.2 realizes elliptical motion, the parameters are the center point coordinates and the long half axis, the short half axis

Source: Internet
Author: User

Ccaction movement did not realize elliptical motion or circular motion, so in the Internet to find a lot of related posts, there is a cccircleby can achieve circular motion, but when using ccrepeatforever cycle motion, always will be stuck, So on the Internet to find an example of the implementation of elliptical motion, when the long and short half of the axis is equal, is the circular motion, this cycle is not card.

#include ". /actions/ccactioninterval.h " //include System Delay class action header file

#define PI 3.14159

typedef struct _lrtuoyuanconfig{

Ccpoint centerposition; //center point coordinates

float alength; //long half shaft

float clength; //oval C length, i.e. half focal length

}lrtuoyuanconfig;

Class __declspec (dllexport) lrtuoyuanby:public Ccactioninterval

{

Public

BOOL Initwithduration (float F, const lrtuoyuanconfig& C);

virtual void update (float time);

Public

Static lrtuoyuanby* actionwithduration (float t, const lrtuoyuanconfig& C);

Static inline float Tuoyuanxat (float A, float bx, float c, float t)

{

Return-a*cos (2*pi*t) +a;

}

Static inline float Tuoyuanyat (float A, float by, float C, float t)

{

Float B = sqrt (POWF (a,2)-POWF (c,2));

Return B*sin (2*pi*t);

}

Protected

Lrtuoyuanconfig M_sconfig;

Ccpoint m_startposition;

Ccpoint s_startposition;

};

The implementation file is:

lrtuoyuanby* lrtuoyuanby::actionwithduration (float t, const lrtuoyuanconfig& C)

{

lrtuoyuanby* Ptuoyuanby = new Lrtuoyuanby ();

Ptuoyuanby->initwithduration (T,C);

Ptuoyuanby->autorelease ();

return Ptuoyuanby;

}

BOOL Lrtuoyuanby::initwithduration (float T, const lrtuoyuanconfig& C)

{

if (Ccactioninterval::initwithduration (t))

{

M_sconfig = C;

return true;

}

return false;

}

void Lrtuoyuanby::update (float time)

{

if (m_ptarget)

{

Ccpoint s_startposition = m_sconfig.centerposition; //center point coordinates

float a = m_sconfig.alength;

float bx = m_sconfig.centerposition.x;

float by = M_SCONFIG.CENTERPOSITION.Y;

float C = m_sconfig.clength;

float x = Tuoyuanxat (A, BX, C, time); //Call the previous coordinate calculation function to calculate the coordinate value

Float y = Tuoyuanyat (A, by, c, time);

M_ptarget-SetPosition (Ccpadd (S_startposition, CCP (x-a, y))); //Because we draw the computed ellipse centered on the origin, we have to add the original set center point coordinates

}

}

See below how to use

First define a Lrtuoyuanconfig

Lrtuoyuanconfig config;

Config.centerposition = CCP (240,160);

config.alength = 100;

Config.clength = 60;

Sprite--Runaction (Ccrepeatforever::create (lrtuoyuanby::actionwithduration (1.0f, config));

This allows the elf to achieve infinite elliptical motion.

When clength = 0 o'clock, it is a circular motion.

The cocos2d-x2.2 realizes elliptical motion, the parameters are the center point coordinates and the long half axis, the short half axis

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.