首页 > 代码库 > cocos3 图片按照椭圆运动

cocos3 图片按照椭圆运动

#pragma once#include "cocos2d.h"USING_NS_CC;typedef struct _EclipseConfig {      //中心点坐标      CCPoint centerPosition;      //椭圆a长,三角斜边      float aLength;      //椭圆c长,三角底边      float cLength;  } EclipseConfig; class Eclipse:public ActionInterval{public:    bool initWithDuration(float duration, const EclipseConfig& deltaPosition);    virtual void update(float time);    virtual Eclipse* clone() const override;    virtual Eclipse* reverse(void) const  override;public:    static Eclipse *actionWithDuration(float t, const EclipseConfig& c); protected:    EclipseConfig m_sConfig;      Point m_startPosition;      Point s_startPosition;  };
#include "Eclipse.h"static inline float tuoyuanXat( float a, float bx, float c,float t )//返回X坐标{    //参数方程    return -a*cos(2*3.1415926*t)+a;}static inline float tuoyuanYat( float a, float by, float c, float t )//返回Y坐标{    float b = sqrt(powf(a, 2) - powf(c, 2));//因为之前定义的参数是焦距c而不是短半轴b,所以需要计算出b    //参数方程    return b*sin(2*3.1415926*t);}////TuoyuanBy//Eclipse* Eclipse::actionWithDuration(float t, const EclipseConfig& c)//利用之前定义的椭圆的三个参数初始化椭圆{    Eclipse *pTuoyuanBy = new Eclipse();    pTuoyuanBy->initWithDuration(t, c);    pTuoyuanBy->autorelease();    return pTuoyuanBy;}bool Eclipse::initWithDuration(float t, const EclipseConfig& c){    if (CCActionInterval::initWithDuration(t))    {        m_sConfig = c;        return true;    }    return false;}void Eclipse::update(float time){    if (_target)    {        CCPoint s_startPosition =m_sConfig.centerPosition;//中心点坐标        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);//调用之前的坐标计算函数来计算出坐标值        float y = tuoyuanYat(a, by, c, time);        _target->setPosition(ccpAdd(s_startPosition, ccp(x-a, y)));//由于我们画计算出的椭圆你做值是以原点为中心的,所以需要加上我们设定的中心点坐标    }}Eclipse* Eclipse::clone() const{    auto a = new Eclipse();    a->initWithDuration(_duration, m_sConfig);    a->autorelease();    return a;}Eclipse* Eclipse::reverse() const{    auto a = new Eclipse();    a->initWithDuration(_duration, m_sConfig);    a->autorelease();    return a;}

 

cocos3 图片按照椭圆运动