前端之家收集整理的这篇文章主要介绍了
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[@H_403_38@20];
// b2Transform tempTrans[@H_403_38@20];
for (int i = @H_403_38@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 = @H_403_38@0; j < @H_403_38@32; j++)
{
_world->Step(_deltaTime,@H_403_38@10,@H_403_38@10);
// i == @H_403_38@0表示白球,即_player
for (int i = @H_403_38@1; i <= count; ++i)
{
ball = (Ball *)_balls->objectAtIndex(i-@H_403_38@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[@H_403_38@0][j]->setVisible(true);
_dot[@H_403_38@0][j]->setPosition(curpos);
}
}
else
{
_dot[@H_403_38@0][j]->setVisible(false);
}
_world->ClearForces();
}
for (int i = @H_403_38@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),@H_403_38@0);
ball->getBody()->SetLinearVelocity(b2Vec2(@H_403_38@0,@H_403_38@0));
}
_player->setPosition(tempPos[count]);
_player->getBody()->SetTransform(b2Vec2(tempPos[count].x / PTM_RATIO,tempPos[count].y / PTM_RATIO),@H_403_38@0);
_player->getBody()->SetLinearVelocity(b2Vec2(@H_403_38@0,@H_403_38@0));
setOnSimulating(false);
}
本文达到的效果是,在击打之前,就能预判球的走势。实现效果如下: