• FEATURED

View more

View more

View more

\$16

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

## OpenGL missile direction questions

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1C++ noobie  Members

Posted 14 May 2014 - 02:56 PM

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[i]->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[i] 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[i]->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, 14 May 2014 - 02:58 PM.

### #2Brother Bob  Moderators

Posted 14 May 2014 - 03:42 PM

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[i] 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[i]->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.

### #3C++ noobie  Members

Posted 14 May 2014 - 04:04 PM

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?

### #4Brother Bob  Moderators

Posted 14 May 2014 - 04:26 PM

I don't understand what you mean. If gTanks is a vector of tanks, then gTanks[i] is a specific tank indicated by the index i. I'm merely suggesting that you pass gTank[i] 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.

### #5SeanMiddleditch  Members

Posted 14 May 2014 - 07:13 PM

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.

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

### #6EarthBanana  GDNet+

Posted 14 May 2014 - 08:51 PM

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?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.