これは、私が問題を抱えているプログラムのセクションのコードです。私の目標は砲塔を回転させることですが、その動きが自然に見えるように戦車の軸を中心に回転させる必要があります。どうすればいいのかわからない、試したことはすべてうまくいかないので、ここからどこへ行くべきかを知る必要があります。そして、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);
}