-1

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

1 に答える 1

1

I wouldn't recommend using/trusting a function that you don't understand unless you absolutely have to. I would recommend learning the math behind computeGradients, which is readily available online. My linear algebra is kind of rusty, so I can't help too much with the trig. I think it boils down to keeping track of the position, direction, and right vectors of the tank, and the horizontal and vertical angles. The math is similar to that described here.

From what I DO remember, you must translate the tank to the origin of world space, apply the linear transformation matrices (rotation in this case), and then translate it back to its position (I don't know if computeGradients does that implicitly). This is based off my limited knowledge of how model matrices in OpenGL works.

Anyways I hope any of what I said helped.

于 2013-09-26T23:15:35.267 に答える