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

前端之家收集整理的这篇文章主要介绍了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);
}

效果

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

原文链接:https://www.f2er.com/cocos2dx/340497.html

猜你在找的Cocos2d-x相关文章