C++版 小球自由落体的实现

/*
*@
*complier:vc++ 2008
*Date:8/11/2013
*Author:pjgan
*/

/*
*Param Describe: 
*m_ballreboundCount: the rebound count
*m_ballnextpos: next position;
*KBALLDESTINATIONPOS: the bottom of the ground
*KBALLINITSPEED: the speed of the ball
*m_pAcorn: the object( the ball)
*m_ballState: the ball's state
*/

void CSEGameMainPage::balldrop(VINT32 idestionpos) { // the ball drop
    if(m_pAcorn[0]) {
        VINT32 iballcurrentYpos = m_pAcorn[0]->GetYpos();
        if(iballcurrentYpos > idestionpos) {
             m_ballreboundCount++; //the rebount count
            VDOUBLE dreboundCountDistance = Global_pow(0.5, m_ballreboundCount);
            m_ballnextpos = KBALLDESTINATIONPOS - (KBALLDESTINATIONPOS * dreboundCountDistance);//set the rebound position
            m_ballState = BALLSTATE_REBOUND; // 
        } else {
            /*
            *set the drop speed;
            */
            m_InitballSpeed  /= KBALLINITSPEED;
            m_InitballSpeed  += KBALLINITSPEED*2;
            if((m_CurTime - m_LastTime) > 20) {
                m_pAcorn[0]->SetYpos(iballcurrentYpos + m_InitballSpeed);
                m_LastTime = m_CurTime;
            }
        }
    }
}

void CSEGameMainPage::ballrebound(VINT32 idestionpos) { // the ball rebound
    if(m_pAcorn[0]) {
        VINT32 iballcurrentYpos = m_pAcorn[0]->GetYpos();
        if(iballcurrentYpos <= idestionpos) {
            m_ballnextpos = KBALLDESTINATIONPOS;
            m_ballState = BALLSTATE_DROP;
        } else {
            /*
            *set the rebound speed 
            */
            m_InitballSpeed  /= KBALLINITSPEED;
            m_InitballSpeed  += KBALLINITSPEED*2;
            if((m_CurTime - m_LastTime) > 20) {
                m_pAcorn[0]->SetYpos(iballcurrentYpos - m_InitballSpeed);
                m_LastTime = m_CurTime;
            }
        }
    }
}
    switch(m_ballState) {
        case BALLSTATE_INITSTATE:
            SquirrelsRun();
            break;
        case BALLSTATE_DROP:
            balldrop(m_ballnextpos);
            break;
        case BALLSTATE_REBOUND:
            if(m_ballnextpos < KBALLDESTINATIONPOS) {
                ballrebound(m_ballnextpos);
            } else {
                m_ballState = BALLSTATE_STOP;
            }
            break;
        case BALLSTATE_STOP:
            break;
        default:
            GEASSERT(0);
            break;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值