大佬教程收集整理的这篇文章主要介绍了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台球游戏中的路径预测所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。