Archived

This topic is now archived and is closed to further replies.

need some help on shooting..

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

Recommended Posts

this is my code in DrawGLScene()....
          if(bulletFired == true) {
glPushMatrix();
glTranslatef(bulletStartPos, bulletSpeed, 0.0f);
glRotatef(0, 0.0, 0.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0, 50, 0);
glVertex3f(3, 63, 0);
glVertex3f(0, 70, 0);
glVertex3f(-3, 63, 0);
glEnd();
if(bulletSpeed>650){
bulletFired = false;
bulletSpeed = 0;
}
bulletSpeed+=1.0f;
glPopMatrix();
}

...the problem is that only a SINGLE bullet can be fired at a single time.... i've only been doing this for about a month and all this stuff is really new to me. any help would be appreciated. [edited by - rotaerc on November 28, 2003 9:35:13 AM]

Share on other sites
You need to keep an array (or vector) of bullets. This allows you to iterate through and draw each bullet in turn. Using an array means there is an upper limit on the number of bullets you can use at any one time. Managing an array is also more difficult. Each bullet will need to know its bulletStartPos and bulletSpeed. It is probably best (easiest) if you store your bullet information in a class (or struct).

I would recommend using a vector to store your bullets:
std::vector<Bullet> bullets;// when firing a bulletbullets.push_back(Bullet(x, y));// when drawingstd::for_each(bullets.begin(), bullets.end(), drawBullet);

For this you would need a drawBullet function that takes a Bullet as it's argument and does the drawing as shown below. You would also need to put your bullet invalidation code (bulletFired = false) after the drawing and remove each bullet from the vector as they get used up.

PS. please don't crosspost. It wastes database space and often results in people expending effort answering a question that has already been answered.

Enigma

EDIT: angle brackets in std::vector declaration.

[edited by - Enigma on November 28, 2003 12:18:49 PM]

Share on other sites

i''ve used arrays before but it''s my first time seeing ''vector'' so i don''t know what it is.

can you explain real quick? a simple example would be wonderful.

PS. i won''t crosspost. it was actually a mistake and i couldn''t figure out how to delete the former post.

Share on other sites
A vector is kind of like an array without an upper bound (it can grow as long as you need it). You can push values onto it (like a stack) and pop values off of it.

Say you have the following values:
4 1 6 9 3

Now you can push values onto the back of the vector (say, 8 and 2), which gives you:
4 1 6 9 3 8 2

You don''t need to (manually) dynamically allocate more memory, the vector STL (Standard Template Library) class takes care of it for you.

Hope that helps!

---
K-1 Productions: Come visit us here.

Share on other sites
a very good and clean explaination on the mechanics....

...uh you mind if i ask for a code example?

Share on other sites
google for "pixelate" and "still trying to learn the STL" they did a good article on it with lots of examples of vector use

Share on other sites
http://www.pixelate.co.za/
http://echellon.hybd.net/

i did my homework.

which one?

i can't find the article.

[edited by - rotaerc on November 28, 2003 12:09:01 PM]

Share on other sites
found it!!

thanks a bundle to everybody~

Share on other sites
I would create a new class named missile and then make linked lists in your player class, it works very good and you can create a very dynamic system with that!