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

Cocos-2dx台球游戏中的路径预测,第1张

概述Cocos-2dx台球游戏中的路径预测 接上一篇台球游戏实现 方法 加上物理模拟设置,然后将路径用小球图片加载的方式画出来。 关键代码 SimulateTrajectory(){ Ball *ball; b2Body *cueBody = _cue->getBody(); float angle = cueBody->GetAngle(); int count = 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);}
效果

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

总结

以上是内存溢出为你收集整理的Cocos-2dx台球游戏中的路径预测全部内容,希望文章能够帮你解决Cocos-2dx台球游戏中的路径预测所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1076183.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存