首页 > 代码库 > cocos3 抛物线

cocos3 抛物线

#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 100Parabola* 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 抛物线