所以这是我遇到问题的程序部分的代码。我的目标是让炮塔旋转,但我需要它在坦克的轴上旋转,这样它的运动看起来有点自然。我不知道该怎么做,我尝试过的一切都不起作用,所以我只需要知道从这里去哪里。并且给出了 computeGradient 函数,所以我必须使用它来计算如何更改炮塔的 x 和 y 位置。任何帮助是极大的赞赏。
#include <fstream>
#include <iostream>
#include <string>
#include "engine.h"
#include "constants.h"
using namespace std;
void initTankState(float& tankX, float& tankY, float& turretX, float& turretY, int& tankAngle, int& turretAngle){
tankX = SCREEN_WIDTH/2;
tankY = SCREEN_HEIGHT*.75;
turretX = tankX;
turretY = tankY;
tankAngle = 0;
turretAngle = 0;
}
void loadInfantryData(string path, int& numInfantry, float*& infantryPosX, float*& infantryPosY, int*& infantryAngle){
fstream fin;
fin.open(path, ios::in);
fin>>numInfantry;
for (int i = 0; i<numInfantry; i++)
{
fin>>infantryPosX[i]>>infantryPosY[i]>>infantryAngle[i];
}
}
void changeGameState(string command, float& tankX, float& tankY, float& turretX, float& turretY, int & tankAngle, int & turretAngle, int numInfantry, float* infantryPosX, float* infantryPosY, int* infantryAngle){
float x;
float y;
computeGradient(x, y, tankAngle);
/*if (command == "up")
moveForward(tankX, tankY, tankAngle);
else if (command =="down")
moveBackward(tankX, tankY, tankAngle);
else*/ if (command == "left")
turnLeft(tankX, tankY, tankAngle);
else if (command == "right")
turnRight(tankX, tankY, tankAngle);
else if (command == "pleft")
{
panLeft(turretAngle);
turretX = x;
turretY = y;
}
else if (command == "pright")
{
panRight(turretAngle);
turretX = x;
turretY = y;
}
}
void updateTurret(float, float, float&, float&, int, int)
{
}
void panLeft(int &turretAngle) //here
{
turretAngle++;
if (turretAngle <0)
turretAngle = TANK_NUM_SPRITES-1;
if (turretAngle >TANK_NUM_SPRITES-1)
turretAngle = 0;
}
void panRight(int &turretAngle) //and here are where I've run into a block
{
turretAngle--;
if (turretAngle >TANK_NUM_SPRITES-1)
turretAngle = 0;
if (turretAngle <0)
turretAngle = TANK_NUM_SPRITES-1;
}
void moveForward(float &, float &, int &, float)
{
}
void moveBackward(float &, float &, int &, float)
{
}
void turnLeft(float &tankX, float &tankY, int &tankAngle)
{
tankAngle++;
if (tankAngle <0)
tankAngle = TANK_NUM_SPRITES-1;
if (tankAngle >TANK_NUM_SPRITES-1)
tankAngle = 0;
}
void turnRight(float &tankX, float &tankY, int &tankAngle)
{
tankAngle--;
if (tankAngle <0)
tankAngle = TANK_NUM_SPRITES-1;
if (tankAngle >TANK_NUM_SPRITES-1)
tankAngle = 0;
}
void noAction(float &, float &, int &)
{
}
//Trigonometric Calculation
void computeGradient(float & dx, float & dy, int tankAngle){
int tmpAngle = ( (TANK_NUM_SPRITES-tankAngle))%TANK_NUM_SPRITES;
float radianAngle=(2*PI)*(float(tmpAngle)/TANK_NUM_SPRITES)-(PI/2.0);
dx = cos(radianAngle);
dy = sin(radianAngle);
}