shooting bullets

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2030 Views - Last Post: 22 June 2014 - 07:15 PM Rate Topic: -----

#16 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: shooting bullets

Posted 09 June 2014 - 03:14 PM

View Postpbivens85, on 09 June 2014 - 07:25 PM, said:

so am I close to the answer?


Huh? Did you read my post?
Was This Post Helpful? 0
  • +
  • -

#17 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 09 June 2014 - 06:03 PM

yes I did sorry.
Was This Post Helpful? 0
  • +
  • -

#18 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: shooting bullets

Posted 10 June 2014 - 05:10 AM

And would you say that you are close to the answer?
Was This Post Helpful? 0
  • +
  • -

#19 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 12 June 2014 - 08:59 PM

well I read your post and looked at the code, but I am still a little confused about how to proceed, sorry I am a little dense but I am still reading the opengl programming guide which is called "the red book" by some people, I am almost done reading it, five chapters to go, wish me all the best.
Was This Post Helpful? 0
  • +
  • -

#20 Blindman67  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 491
  • Joined: 15-March 14

Re: shooting bullets

Posted 12 June 2014 - 10:05 PM

You are still going for it. Lets see if we can get you moving.

Your code with some comments

// draws a green spaceship on the screen
// looks something like  /\
//                      /  \
//                     /_/\_\
// Its position on the screen is set by the current Matrix which
// describes it x and y position, its rotation, and its scale
void SpaceCraft(void){
    glBegin(GL_LINE_LOOP);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, -0.5f, 0.0f);
    glVertex3f(-0.5f, -1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glVertex3f(0.5f, -1.0f, 0.0f);
    glEnd();
}

// draws a green dot as a bullet relative to the spaceships position
// up represents the distance from the spaceship
// I do not know where up is declaired
void Bullet(void){     // best to have functions start with capitals
    glBegin(GL_POINTS);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, up, 0.0f);
    glEnd();

    up += 0.1f;         // move the bullet away from the ship
    if (up >= 10.0f){   // when its about 5 times the size of the ship away
       up = 1.0f;  // put the bullet back at the start of the ship
    }
}

// this is the main loop and is called once every frame 
void display(void){  
    glClear(GL_COLOR_BUFFER_BIT);   // clear the current display buffer
    glColor3f(0.0f, 1.0f, 0.0f);    // set the current render colour to green 
                                    // do not think you need that here as its done in
                                    // the SpaceCraft and bullet function
    glPushMatrix();    // pushes an extra copy of the matrix onto the stack
                       // this matrix that is on the stack defines where abouts the
                       // spaceship and bullets are on the screen
    SpaceCraft();      // Draws the spaceship
    Bullet();          // draws and moves the bullets    
    glPopMatrix();     // remove the matrix from the stack. You could do without
                       // push and pop for the time being/
    glutSwapBuffers(); // points to s fresh display buffer and presents the newly draw buffer
                       // to the display hardware 
    glFlush();         // tell openGL to execute all the command sent to it above.
}

// Handles the keyboard input. Called from somewhere.
// key is the key pressed
// x and y seam to have no meaning here
void handleKeypress(unsigned char key, int x, int y){
    switch (key){
        case 27:      // handle ESC key
            exit(0);  // set exit stuff
            break;
        case 32:      // handles the space key

            // this bit is incorrect
            Bullet(); // but here you just draw the bullet and move it
                      // up while the space bar is down (or pressed)
                      // you want it to start a bullet
            break;
    }
   glutPostRedisplay();  // tells openGL that there is new stuff to render
}




So lets change this to work.
The space bar should start a bullet moving. It should not display the bullet. We need to create a flag that lets you know that the bullet has been shot and is moving. So where ever you declaired "up" you need to create a boolean (true false) var
bool bulletActive = false;   // set it to false. No bullet at the start

In the key press you need to change this to true if it is false. When ever this flag is true a bullet will be moving
in handleKeypress
   case 32:
      if(bulletActive == false){ // if no bullet is active
         bulletActive = true;   // turn on the bullet
         up = 1.0f;             // make sure the bullet is at the ships nose
      }
      break

In the display function you need to test if the bullet is active and display it only if so
so replace the Bullet call with
if(bulletActive == true){ // only display the bullet if active
   Bullet();              // draw and move the bullet
}



Now in the bullet function you need to turn the bullet off when it gets to distance so change to
up += 0.1f;
if(up >= 10.0f){ // the bullet has gone the distance
   bulletActive = false;   // turn off the bullet
                           // don't worry about up as that is reset by the key press
}


Ok that should get the basic bullet working

Code below. As you have quite some missing code you will have to place the declaration of bulletActive next to up's declaration.
Also I am only showing you the logic. we can get to more details coding styles later if you are interested,.

bool bulletActive;   // put this where you create up;
bulletActive = false;   // put this where you first give up a value;

void SpaceCraft(void) {
    glBegin(GL_LINE_LOOP);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, -0.5f, 0.0f);
    glVertex3f(-0.5f, -1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glVertex3f(0.5f, -1.0f, 0.0f);
    glEnd();
}
void Bullet(void) {
    glBegin(GL_POINTS);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, up, 0.0f);
    glEnd();
    up += 0.1f;
    if (up >= 10.0f) {
        bulletActive = false;
    }
}
void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f, 1.0f, 0.0f);
    glPushMatrix();
    SpaceCraft();
    if (bulletActive == true) {
        Bullet();
    }
    glPopMatrix();
    glutSwapBuffers();
    glFlush();
}
void handleKeypress(unsigned char key, int x, int y) {
    switch (key) {
    case 27:
        exit(0);
        break;
    case 32:
        if (bulletActive == false) {
            bulletActive = true;
            up = 1.0f;
        }
        break
    }
    glutPostRedisplay();
}




Now if you want to continue we can add more bullets some other time.

This post has been edited by Blindman67: 12 June 2014 - 10:10 PM

Was This Post Helpful? 0
  • +
  • -

#21 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 18 June 2014 - 06:40 PM

well I tried your code but it still does not work, it does not shoot any bullets, I may have a mistake in my code so far. Here is my new code.
bool bulletActive = false;

void SpaceCraft(void)
{
	glBegin(GL_LINE_LOOP);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, -0.5f, 0.0f);
	glVertex3f(-0.5f, -1.0f, 0.0f);
	glVertex3f(-1.0f, -1.0f, 0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, -1.0f, 0.0f);
	glVertex3f(0.5f, -1.0f, 0.0f);
	glEnd();
}

void bullet(void)
{
	glBegin(GL_POINTS);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, up, 0.0f);
	glEnd();
	
		up += 0.1f;

		if (up >= 10.0f)
		{
			bulletActive = false;
		}
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0f, 1.0f, 0.0f);
	glPushMatrix();
	SpaceCraft();
	if (bulletActive == true)
	{
	bullet();
	}
	glPopMatrix();
	glutSwapBuffers();
	glFlush();
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		if (bulletActive == false)
		{
		bulletActive == true;
		up = 1.0f;
		}
		break;
	}
	glutPostRedisplay();
}


Was This Post Helpful? 0
  • +
  • -

#22 Blindman67  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 491
  • Joined: 15-March 14

Re: shooting bullets

Posted 18 June 2014 - 08:04 PM

Line 56 should be
bulletActive = true;
not
bulletActive == true; // this will never fire the bullet
Was This Post Helpful? 0
  • +
  • -

#23 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 18 June 2014 - 09:19 PM

well the code works but it still does not give the machine gun type of performance like in asteroids. thanks for all the help blindman.
Was This Post Helpful? 0
  • +
  • -

#24 Blindman67  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 491
  • Joined: 15-March 14

Re: shooting bullets

Posted 19 June 2014 - 12:03 AM

For many bullets you need to keep track of each bullet and if each bullet is active. You also have to mack sure that there is some time between each shot so that you dont fire all of them just touching the space bar.
To do this we need an array with one entry for each bullet. This arrray has the bullets position and if it is active or not
This is not the best way to do it. You should be using vectors, but I am not getting into that for now. I am trying to keep it as simple as possible. This is about the simplest way to have many bullets that behave like the game you want.


// create all the vars and structures you need
#define FIRE_RATE 10       // number of frames between bullet fire
                           // smaller number give more bullets per second
                           // if you set it to zero there is no delat between bullets
#define NUMBER_BULLETS 3   // set the number of bullets you want
                           // you can set this to as many as you want. but the max number of bullets
#define BULLET_SPEED 0.1f  // constant for bullet speed
#define BULLET_DISTANCE 10.0f // distance the bullet can travel                          

struct bulletStruct {      // create a structure for each bullet
    float pos;             // the position of the bullet
    bool active;           // true if the bullet is on
}
bullets[NUMBER_BULLETS];   // now create an array one for each bullet

int fireRepeatTimer = 0;   // this is used to track the time since the last bullet was shot.
                           // you want a little time between each bullet
                           
// as you do not show the setup code where some setup now needs to be done I have added this to indicate
// that the bullet array needs to be set up
bool firstRun = true;      // true when the code is first run


void SpaceCraft(void) {
    glBegin(GL_LINE_LOOP);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, -0.5f, 0.0f);
    glVertex3f(-0.5f, -1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glVertex3f(0.5f, -1.0f, 0.0f);
    glEnd();
}

// need to add the argument pos so each bullet can be draw at its location. 
// As you do not show any function definitions I will assume you dont have any.
// if you do you will have to add void bullet(float pos); where that function is defined 
void bullet(float pos) {
    glBegin(GL_POINTS);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, pos, 0.0f);  // replace up with the passed variable pos
    glEnd();
}

void display(void) {
    int i;
    if (firstRun == true) {                    // check if this is the first time run.
                                               // if so then set up the bullets. 
                                               // You should move this code into the setup code
                                               // which you have not included
        for (i = 0; i < NUMBER_BULLETS; i++) { // for each bullet
            bullets[i].active = false;         // turn it off
            bullets[i].pos = 0.0f;             // its position
        }
        firstRun = false;                      // no longer first run
    }
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f, 1.0f, 0.0f);
    glPushMatrix();
    SpaceCraft();
    
    // now we do each bullet and draw it if active
    for (i = 0; i < NUMBER_BULLETS; i++) {  // for each bullet
        if (bullets[i].active == true) {    // is the bullet active
            bullets[i].pos += BULLET_SPEED; // move the bullet
            bullet(bullets[i].pos);         // draw the bullet. We now pass the position
                                            // to the function
            if (bullets[i].pos > BULLET_DISTANCE) {  // check if the bullet has gone the distance
                bullets[i].active = false;  // turns of the bullet
            }
        }
    }
    // update the fireRepeatTimer
    if (fireRepeatTimer > 0) {
        fireRepeatTimer--;
    }
    glPopMatrix();
    glutSwapBuffers();
    glFlush();
}

void handleKeypress(unsigned char key, int x, int y) {
    int i;
    switch (key) {
    case 27:
        exit(0);
        break;
    case 32:
        if (fireRepeatTimer == 0) {                // check that enough time has passed since
                                                   // last bullet shot
            for (i = 0; i < NUMBER_BULLETS; i++) { // check for a free bullet
                if (bullets[i].active == false) {  // if the bullet is free fire it
                    bullets[i].active = true;      // turn it on
                    bullets[i].pos = 1.0f;         // set its starting pos
                    fireRepeatTimer = FIRE_RATE;   // set countdown till next fire is alowed
                    break;                         // break from the loop
                    
                }
            }
        }
        break;
    }
    glutPostRedisplay();
}



Was This Post Helpful? 0
  • +
  • -

#25 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 22 June 2014 - 07:15 PM

well I have read both the opengl superbible and the opengl programming guide and am working on a c++ book just to brush up on my c++ skills. when I get done with this book I am going to work more on my game, am I going in the right direction?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2