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

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

上一篇台球游戏实现

方法

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

关键代码

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);
}

效果

本文达到的效果是,在击打之前,就能预判球的走势。实现效果如下:

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...