I'm trying to write a simple 2D openGL tank game in C++.
The first snippet is my missile constructor, second is my tank's void function to compute tank position.
I'm trying to have my missile shoot at the same direction as the tank, so I'll use the angle from my void tank function (angle += angularVelocity;)
Therefore in the direction part in my missile constructor, I tried to set x and y direction to be cos and sin of the tank's angle.
I thought I can just grab tank's angle using a pointer like this gTanks->angle.
But it doesn't compile.
Visual Studio warned me I can't use cos and sin that way (I have #include <math.h> though).
And it also says the i in gTanks is undefined. (I know it's undefined but I don't know how to define my number of tanks in missile constructor...)
Do I set the missile direction in the constructor or do I have to set it in a function?
And is gTanks->angle referring to the calculated angle in the void function (angle += angularVelocity;), or is it referring to tank's constructor angle member?
Missile::Missile()
{
speed = 5.0;
size = 0.1;
maxRange = 15.0;
position[0] = 0.0;
position[1] = 0.0;
position[2] = 0.0;
color[0] = 1.0;
color[1] = 1.0;
color[2] = 0.001;
direction[0] = cos * gTanks[i]->angle;
direction[1] = sin * gTanks[i]->angle;
direction[2] = 0.0;
notBouncy = false;
totalTraveled = 0.0;
elapsedTime = 1.0;
}
void Tank::computePosition( double elapsedTime )
{
float velocity;
float angularVelocity;
if ( elapsedTime > MINIMUM_MILLISECOND_UPDATE )
{
angularVelocity = rotate * angularSpeed * elapsedTime;
velocity = moving * speed * elapsedTime;
angle += angularVelocity;
prevPosition[0] = position[0];
prevPosition[1] = position[1];
prevPosition[2] = position[2];
position[0] += velocity * cos( angle * M_PI / 180 );
position[1] += velocity * sin( angle * M_PI / 180 );
}
}