cocos2dx扩展CCAction实现精灵抛物线运动
2014-11-30 20:46
190 查看
在不使用物理引擎的情况下,模拟物体在重力作用下的抛物线运动(忽略空气阻力)。给出运动起始点位置,终点位置,重力加速度g,起始点的发射角作为参数。
先根据已知条件求出物体在x轴和y轴上的初速度,再根据公式f(t)=v0t+0.5*at2 求出运动轨迹。
类CCParabolaMove继承自CCAction。
create()为静态函数,所以对非静态成员变量的赋值要在initWithDuration()中实现:
在runaction时startWithTarget(CCNode *pTarget)会首先被调用,通过pTarget为执行动作的对象指针:
update(float t)中参数t表示当前运动时间与整个运动时长的比值,范围0到1。
先根据已知条件求出物体在x轴和y轴上的初速度,再根据公式f(t)=v0t+0.5*at2 求出运动轨迹。
类CCParabolaMove继承自CCAction。
CCParabolaMove* CCParabolaMove::create(const CCPoint& startPosition, const CCPoint& endPosition, float angle, float g,float &return_duration) { CCParabolaMove *pRet = new CCParabolaMove(); float vx0,vy0, x1, y1, duration; x1 = endPosition.x - startPosition.x; y1 = endPosition.y - startPosition.y x1 /= PIXELS_PER_METER; //像素到米的转换,PIXELS_PER_METER为转换系数,这里设置为100 y1 /= PIXELS_PER_METER; angle = angle*3.14 / 180;//convert t to radian if (x1<0) { angle *= -1; } vx0 = x1*sqrt(g / 2 / (x1*tan(angle) - y1)); //求出初速度 vy0 = vx0 * tan(angle); duration = x1 / vx0; //求出整个运动的时间 return_duration = duration; //将duration通过参数返回 pRet->initWithDuration(duration,startPosition, endPosition, angle,g, vx0, vy0); pRet->autorelease(); return pRet; }
create()为静态函数,所以对非静态成员变量的赋值要在initWithDuration()中实现:
bool CCParabolaMove::initWithDuration(float duration, const CCPoint& startPosition, const CCPoint& endPosition, float angle, float g, float vx0, float vy0) { if (CCActionInterval::initWithDuration(duration)) { m_vx0 = vx0; m_vy0 = vy0; m_startPosition = startPosition; m_endPosition = endPosition; angle = angle*3.14 / 180;//convert t to radian m_angle = angle; m_dur = duration; m_tan_a = tan(angle); m_g = g; return true; } return false; }
在runaction时startWithTarget(CCNode *pTarget)会首先被调用,通过pTarget为执行动作的对象指针:
void CCParabolaMove::startWithTarget(CCNode *pTarget) { CCActionInterval::startWithTarget(pTarget); }
update(float t)中参数t表示当前运动时间与整个运动时长的比值,范围0到1。
void CCParabolaMove::update(float t) { if (m_pTarget) { float elapsed = t * m_fDuration; //获得当前的运动时间 float diff_x = m_vx0 * elapsed; float diff_y = m_vy0 * elapsed - 0.5 * m_g * elapsed * elapsed; CCPoint newPos = ccpAdd(m_startPosition, ccp(diff_x * PIXELS_PER_METER, diff_y * PIXELS_PER_METER)); //单位由米转换为像素 m_pTarget->setPosition(newPos); } }
相关文章推荐
- cocos2dx扩展CCAction实现精灵抛物线运动
- JS实现抛物线运动
- 仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
- cocos2dx 3.2 实现鼠标拖动精灵移动的效果!
- Cocos2d-x虚拟摇杆控制精灵上下左右运动----之游戏开发《赵云要格斗》(1) cocos2dx 3.3移植版
- 实现子弹自动向前运动 定时器 移动精灵 精灵释放
- jQuery抛物线运动实现方法(附完整demo源码下载)
- 探cocos2dx对游戏精灵(sprite_nodes)的实现。(源码版本2.2.3)
- cocos2dx 3.2 学习篇之六(精灵运动,自定义运动轨迹(太极八卦))
- Cocos2dx实现精灵的加速(适用于塔防游戏)
- ios怎样实现快速将显卡中数据读出压缩成视频在cocos2dx扩展开发中
- Cocos2d-x初学指南(3): 扩展CCAction制作自己的运动轨迹
- cocos2dx 实现横版游戏中屏幕随精灵移动
- cocos2dx-3.2之创建精灵和简单运动
- cocos2dx 基础触碰效果封装(通用于具有触碰效果的节点或精灵,类似按钮,方便扩展),很久以前的代码
- cocos2d实现精灵在屏幕上随机的运动
- (转)cocos2d-x学习笔记(五)仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
- 实现精灵沿着圆形轨迹运动
- cocos2d-x学习笔记(五)仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
- jQuery抛物线运动实现方法(附完整demo源码下载)