Cocos-2dx台球游戏中的路径预测

Cocos-2dx台球游戏中的路径预测

本文由 @lonelyrains出品,转载请注明出处。
文章链接: http://blog.csdn.net/lonelyrains/article/details/50421728

接上一篇台球游戏实现

方法

加上物理模拟设置,然后将路径用小球图片加载的方式画出来。

关键代码

SimulateTrajectory()
{
    Ball *ball;
    b2Body *cueBody = _cue->getBody();
    float angle = cueBody->GetAngle();
    int count = _balls->count();
    b2Body *ballBody = _player->getBody();

    setOnSimulating(true);

    _world->SetContactListener(NULL);
//  ballBody->SetLinearVelocity(b2Vec2(_pullBack * cos(angle) * SHOT_POWER,
//      _pullBack * sin(angle) * SHOT_POWER));
    _player->getBody()->ApplyLinearImpulse(b2Vec2(_pullBack * cos(angle) * SHOT_POWER,
        _pullBack * sin(angle) * SHOT_POWER), _player->getBody()->GetWorldCenter());

    CCPoint tempPos[20];
//  b2Transform tempTrans[20];
    for (int i = 0; i < count; i++)
    {
        ball = (Ball *)_balls->objectAtIndex(i);
        if(ball->isVisible())
        {
            tempPos[i] = ball->getPosition();
//          tempTrans[i] = ball->getBody()->GetTransform();
        }
    }
    tempPos[count] = _player->getPosition();

    setPlayerPos(_player->getPosition());
//  tempTrans[count] = _player->getBody()->GetTransform();

    for (int j = 0; j < 32; j++)
    {
        _world->Step(_deltaTime, 10, 10);

        // i == 0表示白球,即_player
        for (int i = 1; i <= count; ++i)
        {
            ball = (Ball *)_balls->objectAtIndex(i-1);
            if (ball->isVisible())
            { 
                ballBody = ball->getBody();
                CCPoint curpos = ccp(ballBody->GetPosition().x * PTM_RATIO, ballBody->GetPosition().y * PTM_RATIO);
                {
                    _dot[i][j]->setVisible(true);
                    _dot[i][j]->setPosition(curpos);
                }
            }
            else
            {
                _dot[i][j]->setVisible(false);
            }
        }
        if (_player->isVisible())
        { 
            ballBody = _player->getBody();
            CCPoint curpos = ccp(ballBody->GetPosition().x * PTM_RATIO, ballBody->GetPosition().y * PTM_RATIO);
            {
                _dot[0][j]->setVisible(true);
                _dot[0][j]->setPosition(curpos);
            }
        }
        else
        {
            _dot[0][j]->setVisible(false);
        }
        _world->ClearForces();
    }

    for (int i = 0; i < count; i++)
    {
        ball = (Ball *)_balls->objectAtIndex(i);
        if (false == ball->isVisible())
        {
            continue;
        }
        ball->setPosition(tempPos[i]);
        ball->getBody()->SetTransform(b2Vec2(tempPos[i].x / PTM_RATIO, tempPos[i].y / PTM_RATIO), 0);
        ball->getBody()->SetLinearVelocity(b2Vec2(0, 0));
    }

    _player->setPosition(tempPos[count]);
    _player->getBody()->SetTransform(b2Vec2(tempPos[count].x / PTM_RATIO, tempPos[count].y / PTM_RATIO), 0);
    _player->getBody()->SetLinearVelocity(b2Vec2(0, 0));

    setOnSimulating(false);
}

效果

本文达到的效果是,在击打之前,就能预判球的走势。实现效果如下:
Cocos-2dx台球游戏中的路径预测_第1张图片

你可能感兴趣的:(游戏,预测)