cocos2dx扩展CCAction实现精灵抛物线运动

  在不使用物理引擎的情况下,模拟物体在重力作用下的抛物线运动(忽略空气阻力)。给出运动起始点位置,终点位置,重力加速度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 radian 11 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 radian
10         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 }

 

posted @ 2014-11-30 20:46  Reise  阅读(2071)  评论(0编辑  收藏  举报