cocos2d 走动椭圆
生活随笔
收集整理的這篇文章主要介紹了
cocos2d 走动椭圆
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.效果圖
藝術(shù)與規(guī)劃說(shuō)他想與我合作在全國(guó)率先主角光環(huán)加,橢圓形走動(dòng)。
cocos2d自帶沒(méi)有,參考網(wǎng)上的寫(xiě)了一個(gè)。
2.橢圓數(shù)學(xué)知識(shí)
有關(guān)橢圓的數(shù)學(xué)知識(shí)我已經(jīng)忘光了。網(wǎng)上找了點(diǎn)資料:
a是橢圓的長(zhǎng)半軸,b是橢圓的短半軸。
o是角度,范圍是[0, 2π]。
我們須要知道橢圓上的位置,能夠用以下的公式:3.直接代碼了..
OvalActionInterval.h
#ifndef __JumpGame__OvalInterval__ #define __JumpGame__OvalInterval__#include "CCActionInterval.h"//包括系統(tǒng)延時(shí)類(lèi)動(dòng)作頭文件using namespace cocos2d;// 定義一個(gè)結(jié)構(gòu)來(lái)包括確定橢圓的參數(shù) typedef struct OvalConfig {//中心點(diǎn)坐標(biāo)Vec2 centerPosition;//橢圓a長(zhǎng)半軸float a;//橢圓b短半軸float b;//是否逆時(shí)針運(yùn)動(dòng)bool moveInAnticlockwise;//two zOrderstd::pair<int, int> zOrder; } lOvalConfig;/**以橢圓方式移動(dòng) */class CC_DLL MoveOvalBy : public ActionInterval{ public:MoveOvalBy();//用“動(dòng)作持續(xù)時(shí)間”和“橢圓控制參數(shù)”初始化動(dòng)作bool initWithDuration(float t, const OvalConfig& c);virtual MoveOvalBy* clone() const override;virtual MoveOvalBy* reverse() const override;virtual void update(float t);//利用update函數(shù)來(lái)不斷的設(shè)定坐標(biāo)virtual void startWithTarget(Node *target) override; public://用“動(dòng)作持續(xù)時(shí)間”和“橢圓控制參數(shù)”創(chuàng)建動(dòng)作static MoveOvalBy *create(float t, const OvalConfig& c);protected:OvalConfig _config;//x = a * cos(t) t = [0, 2Pi]inline float getPositionXAtOval(float t ){//返回X坐標(biāo)//參數(shù)方程if(_config.moveInAnticlockwise == false){return _config.a * cos(6.2831852 * (1 - t));}else{return _config.a * cos(6.2831852 * t);}}//y = b * sin(t) t = [0, 2Pi]inline float getPositionYAtOval(float t ){//返回Y坐標(biāo)//參數(shù)方程if(_config.moveInAnticlockwise == false){return _config.b * sin(6.2831852 * (1 - t));}else{return _config.b * sin(6.2831852 * t);}} private:CC_DISALLOW_COPY_AND_ASSIGN(MoveOvalBy); };#endif
OvalActionInterval.cpp
#include "OvalActionInterval.h"MoveOvalBy::MoveOvalBy(){}// //MoveOvalBy // MoveOvalBy* MoveOvalBy::create(float t, const OvalConfig& c){//利用之前定義的橢圓的參數(shù)初始化橢圓MoveOvalBy *action = new MoveOvalBy();action->initWithDuration(t, c);action->autorelease();return action; }bool MoveOvalBy::initWithDuration(float t, const OvalConfig& c){if (ActionInterval::initWithDuration(t)){_config = c;return true;}return false; } void MoveOvalBy::update(float t){//t [0, 1]//log("t:%f", t);if (_target){float x = getPositionXAtOval(t);//調(diào)用之前的坐標(biāo)計(jì)算函數(shù)來(lái)計(jì)算出坐標(biāo)值float y = getPositionYAtOval(t);_target->setPosition(_config.centerPosition + Vec2(x, y));//因?yàn)槲覀儺?huà)計(jì)算出的橢圓你做值是以原點(diǎn)為中心的。所以須要加上我們?cè)O(shè)定的中心點(diǎn)坐標(biāo)if(t <= 0.5){_target->setLocalZOrder(_config.zOrder.first);}else{_target->setLocalZOrder(_config.zOrder.second);}} }MoveOvalBy* MoveOvalBy::clone() const{auto action = new MoveOvalBy();action->initWithDuration(_duration, _config);action->autorelease();return action; }MoveOvalBy* MoveOvalBy::reverse() const{OvalConfig newConfig;newConfig.centerPosition = _config.centerPosition;newConfig.a = _config.a;newConfig.b = _config.b;newConfig.moveInAnticlockwise = !_config.moveInAnticlockwise;newConfig.zOrder = _config.zOrder;return MoveOvalBy::create(_duration, newConfig); }void MoveOvalBy::startWithTarget(Node *target){ActionInterval::startWithTarget(target); }
參考:http://blog.csdn.net/ufolr/article/details/7447773
我這里還加上了zOrder。這樣有透視效果。
a等于b的時(shí)候就是圓形了。
有時(shí)候在游戲中略微用上點(diǎn)數(shù)學(xué)知識(shí)感覺(jué)非常爽。
調(diào)用例如以下:
auto size = this->getContentSize();auto ball = Sprite::createWithSpriteFrameName("defenceBall.png");this->addChild(ball);ball->setPosition(Vec2(size.width * 0.5, size.height * 0.5) + Vec2(0, 10));OvalConfig config;config.a = 100;config.b = 20;config.centerPosition = ball->getPosition();config.moveInAnticlockwise = true;config.zOrder = make_pair(-1, 0);auto moveAction = MoveOvalBy::create(1.0, config);ball->runAction(RepeatForever::create(moveAction));
http://www.waitingfy.com/archives/1343
總結(jié)
以上是生活随笔為你收集整理的cocos2d 走动椭圆的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到自己母亲出车祸预示着什么
- 下一篇: JAVA解析纯真IP地址库