Public Group

# OpenGL missile direction questions

This topic is 1676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 );

}
}

Edited by C++ noobie

##### Share on other sites

Visual Studio warned me I can't use cos and sin that way (I have #include <math.h> though).

cos and sin are functions, so you have to pass the angle as a parameter just like you did in the second code listing.

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...)

What do you intend for i to be? I also assume gTanks is a global array of tanks. I would rather pass the specific tank to the constructor and not rely on global states at all.

Missile::Missile(Tank const &tank) {
...
direction[0] = cos(tank.angle);
direction[1] = sin(tank.angle);
...
}


And then you construct you missile by passing the correct tank to its 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?

It is the value of the angle member variable at the time you access it.

##### Share on other sites

Thank you Bob. You cleared up some of my questions :)

What do you intend for i to be? I also assume gTanks is a global array of tanks. I would rather pass the specific tank to the constructor and not rely on global states at all.

gTanks is a global vector of tanks. But I'm instructed to use vector for tank so I can have multiple tanks shooting at each other at once.

If I have to use vector, not specific tank, how would you put it?

##### Share on other sites

I don't understand what you mean. If gTanks is a vector of tanks, then gTanks is a specific tank indicated by the index i. I'm merely suggesting that you pass gTank to the missile constructor instead of accessing the global array directly. The missile doesn't have to know about all possible tanks, only the tank that fired it so you know from where and in which direction it was fired.

##### Share on other sites
And this is the point where I deeply recommend using vectors (confusingly, not at all the same thing as std::vector!) instead of plain angles.

http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1
http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2
http://blog.wolfire.com/2010/07/Linear-algebra-for-game-developers-part-3
http://blog.wolfire.com/2010/07/Linear-algebra-for-game-developers-part-4

Vectors can look a bit more complicated than angles at first. However, they're actually quite a bit easier to work with once you learn how to use them, and they add an incredible amount of flexibility and power over raw angles.

##### Share on other sites

You cant just create a Missiles for all of your tanks in your vector at once - you have to loop through your vector and create a missile from each tank

You can do this using the above poster's constructor

Missile::Missile(Tank const &tank) {
...
direction[0] = cos(tank.angle);
direction[1] = sin(tank.angle);
...
}

and then loop through your vector

for (int i = 0; i < gTanks.size(); ++i)
{
Missile m(gTanks[i]);
// do something with the missile
}


Not sure what exactly your going for though - all tanks shooting missiles?

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013597
×