cocos2dx扩展CCAction实现精灵抛物线运动
来源:互联网 发布:ubuntu挂载新硬盘 编辑:程序博客网 时间:2024/05/22 14:13
在不使用物理引擎的情况下,模拟物体在重力作用下的抛物线运动(忽略空气阻力)。给出运动起始点位置,终点位置,重力加速度g,起始点的发射角作为参数。
先根据已知条件求出物体在x轴和y轴上的初速度,再根据公式f(t)=v0t+0.5*at2 求出运动轨迹。
类CCParabolaMove继承自CCAction。
1 CCParabolaMove* CCParabolaMove::create(const CCPoint& startPosition, const CCPoint& endPosition, float angle, float g,float &return_duration) 2 { 3 CCParabolaMove *pRet = new CCParabolaMove(); 4 float vx0,vy0, x1, y1, duration; 5 6 x1 = endPosition.x - startPosition.x; 7 y1 = endPosition.y - startPosition.y
8 x1 /= PIXELS_PER_METER; //像素到米的转换,PIXELS_PER_METER为转换系数,这里设置为100 9 y1 /= PIXELS_PER_METER;10 angle = angle*3.14 / 180;//convert t to radian11 12 if (x1<0)13 {14 angle *= -1;15 }16 17 vx0 = x1*sqrt(g / 2 / (x1*tan(angle) - y1)); //求出初速度18 vy0 = vx0 * tan(angle);19 20 duration = x1 / vx0; //求出整个运动的时间21 return_duration = duration; //将duration通过参数返回
22 pRet->initWithDuration(duration,startPosition, endPosition, angle,g, vx0, vy0);23 pRet->autorelease();24 25 return pRet;26 }
create()为静态函数,所以对非静态成员变量的赋值要在initWithDuration()中实现:
1 bool CCParabolaMove::initWithDuration(float duration, const CCPoint& startPosition, const CCPoint& endPosition, float angle, float g, float vx0, float vy0) 2 { 3 if (CCActionInterval::initWithDuration(duration)) 4 { 5 m_vx0 = vx0; 6 m_vy0 = vy0; 7 m_startPosition = startPosition; 8 m_endPosition = endPosition; 9 angle = angle*3.14 / 180;//convert t to radian10 m_angle = angle;11 m_dur = duration;12 m_tan_a = tan(angle);13 m_g = g;14 return true;15 }16 17 return false;18 }
在runaction时startWithTarget(CCNode *pTarget)会首先被调用,通过pTarget为执行动作的对象指针:
1 void CCParabolaMove::startWithTarget(CCNode *pTarget)2 {3 CCActionInterval::startWithTarget(pTarget);4 5 }
update(float t)中参数t表示当前运动时间与整个运动时长的比值,范围0到1。
1 void CCParabolaMove::update(float t) 2 { 3 if (m_pTarget) 4 { 5 float elapsed = t * m_fDuration; //获得当前的运动时间 6 float diff_x = m_vx0 * elapsed; 7 float diff_y = m_vy0 * elapsed - 0.5 * m_g * elapsed * elapsed; 8 9 CCPoint newPos = ccpAdd(m_startPosition, ccp(diff_x * PIXELS_PER_METER, diff_y * PIXELS_PER_METER)); //单位由米转换为像素10 m_pTarget->setPosition(newPos);11 12 13 }14 }
0 0
- cocos2dx扩展CCAction实现精灵抛物线运动
- JS实现抛物线运动
- JS 实现抛物线运动
- 【Cocos2d-x】使用贝塞尔曲线(Bezier)实现精灵抛物线运动
- IOS cocos2d学习笔记-<五>CCAction精灵运动
- 抛物线运动
- JavaScript实现的抛物线运动效果
- Android动画(实现抛物线运动)
- cocos2dx-3.2之创建精灵和简单运动
- js实现小球抛物线轨迹运动的两种方式
- cocos2dx实现象棋之运动
- cocos2dx教程之CCAction
- cocos2dx中的CCAction
- Unity 半圆抛物线运动
- CSS3动画-抛物线运动
- 抛物线运动1
- 抛物线运动2
- cocos-抛物线运动
- cocos2dx安卓客户端接入移动MM付费SDK
- JAVA操作properties文件【要注意properties文件的路径】
- cocos2dx加Admob不显示的解决方法
- Admob插页广告的添加
- cocos2dX改变锚点位置
- cocos2dx扩展CCAction实现精灵抛物线运动
- cocos2d滑动碰撞检测的实现
- 借助CCAction实现转盘抽奖
- 性能提升总结
- CSS基本功先生——滤镜
- cocos2d实现SlotMachine(老*虎*机)
- 将博客搬至CSDN
- cocos2dx 2.x 资源加载和释放问题
- 关于J2EE部分错误&解决方法对照表