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

cocos3 抛物线

时间:2014-12-16 13:23:08      阅读:148      评论:0      收藏:0      [点我收藏+]

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

#pragma once
#include "cocos2d.h"

USING_NS_CC;

class Parabola:public ActionInterval
{
public:
    static Parabola* create(float duration,const Point& startPosition,float velocity,  
        float angle, float g);

     //override
    virtual Parabola* clone() const override;
    virtual Parabola* reverse() const override;
 
    virtual void update(float time) override;

protected:
    bool initWithDuration(float duration, const Point& startPosition, float velocity,
        float angle, float g);

protected:
    float    m_dur ;
    Point    m_startPosition;
    float     m_velocity;
    float    m_angle;
    float    m_g;
};

 

#include "Parabola.h"

#define PIXELS_PER_METER 100


Parabola* Parabola::create(float duration,const Point& startPosition, float velocity,
                           float angle, float g)
{
    Parabola *pRet = new Parabola();
    pRet->initWithDuration(duration,startPosition, velocity, angle,g);
    pRet->autorelease();

    return pRet;
}




bool Parabola::initWithDuration(float duration, const Point& startPosition,  
                                float velocity,float angle, float g)
{
    if (CCActionInterval::initWithDuration(duration))
    {
        m_startPosition = startPosition;
        angle = angle*3.14 / 180;//convert t to radian
        m_angle = angle;
        m_velocity=velocity;
        m_dur = duration;
        m_g = g;
        return true;
    }

    return false;
}


Parabola* Parabola::clone() const
{
    CC_ASSERT(0);
    return nullptr;
}


Parabola* Parabola::reverse() const
{
    CC_ASSERT(0);
    return nullptr;
}


void Parabola::update(float time)
{
    if (_target)
    {
        log("velocity%f",m_velocity);
        float diff_x = m_velocity*cos(m_angle) * _elapsed;
        float diff_y = m_velocity*sin(m_angle)* _elapsed - 0.5 * m_g * _elapsed * _elapsed; 
        log("diff_x%f",diff_x);
        CCPoint newPos = ccpAdd(m_startPosition, ccp(diff_x * PIXELS_PER_METER, diff_y * PIXELS_PER_METER)); //单位由米转换为像素
        _target->setPosition(newPos);


    }
}

 

cocos3 抛物线

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

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

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