首页 > 代码库 > 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 抛物线
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。