码迷,mamicode.com
首页 > 其他好文 > 详细

cocos3 图片按照椭圆运动

时间:2014-12-15 16:57:14      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   on   div   

#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 图片按照椭圆运动

标签:style   blog   io   ar   color   os   sp   on   div   

原文地址:http://www.cnblogs.com/yufenghou/p/4165153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!