Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: machine gun shooting bullets

  1. #1
    Senior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    170

    Cool machine gun shooting bullets

    well I am trying to shoot bullets like in asteroids, I want to press the space bar once and have it shoot one bullet from the space ship to off the screen. At the moment when I press the space bar it moves the bullet but stops the bullet when I release the space bar. here is the code I am working on.
    Code :
    void bullet(void)
    {
    	up += 0.5f;
    	if (up == 10.0f)
    	{
    		up = 0.0f;
    	}
    	glutPostRedisplay();
    }
    void handleKeypress(unsigned char key, int x, int y)
    {
    	switch (key)
    	{
    	case 27:
    		exit(0);
    		break;
    	case 32:
    		bullet();
    		break;
    	}
    }
    let me know if you need more explanation, I am using opengl and c++. also let me know if this is an appropriate question.

  2. #2
    1. This is completly unrelated matter when it comes to OpenGL.

    2. Your program seems to be active through key input and not active by itself where input affects the program.

    3. The bullet stops moving because you have binded the movement update into the button press event and not in its own advancement update which shouldn't be connected in anyway to the key press handler besides the initial bullet launch.

    4. glutPostRedisplay should be moved at the end of your update cycle which there currently isn't in this code.

    5. Minimium Update Loop [Handle Input -> Other Updates -> Render]

    6. If there can be only 1 bullet fired then you need to prevent additional firing from happening. In your case if(up == 0.0f) bullet() in the input handler.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    170
    well after studying the previous post, I have tried something different, I think I am very close to solving my problem. I just need a little hint. Here is my adjusted code.
    Code :
    void bullet()
    {
    	if (fire == true)
    	{
    		for (float i = 0.0f; i <= 15.0f; i += 0.5f)
    		{
    			up = i;
    		}
    		glutPostRedisplay();
    	}
    }
     
    void handleKeypress(unsigned char key, int x, int y) {
    	switch (key) {
    	case 27: //Escape key
    		exit(0);
    		break;
    	case 32:
    		bullet();
    		fire = true;
    		break;
    	}
    	glutPostRedisplay();
    }

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    170
    can I please get some help, is this too hard of a question?

  5. #5
    Senior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    394
    ... but stops the bullet when I release the space bar. here is the code I am working on.
    If you want stuff to move on the screen by themselves (i.e. without holding a key down), you have to use glutIdlefunc. Google it. Look for sample code, etc. Good luck.

  6. #6
    Senior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    170
    well I have almost solved my problem, with glutIdleFunc my bullet shoots over and over again, which is good but I want it to shoot like in asteroids, Here is my code
    Code :
    void bullet()
    {
    	up += 0.5f;
    	if (up >= 18.0f)
    		up = 0.0f;
    	glutPostRedisplay();
    }
     
    void handleKeypress(unsigned char key, int x, int y) {
    	switch (key) {
    	case 27: //Escape key
    		exit(0);
    		break;
    	case 32:
    		glutIdleFunc(bullet);
    		break;
    	}
    	glutPostRedisplay();
    }

  7. #7
    Senior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    170
    well thank you carmine, I just need a little hint, I have almost solved my problem.

  8. #8
    Senior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    394
    ... but I want it to shoot like in asteroids ...
    Not sure what you mean by that. Under what circumstances do you want to stop the bullet? I've added two scenarios to your code. I assume 'up' is a global variable? You don't need 'glutPostRedisplay' in both the 'bullet' routine and the 'handleKeypress' routine. I've removed one of them. Do you understand how 'static' variables work? 'animate' could also be a global variable, but I try to avoid using globals.

    Code :
    void bullet (void)
    {
        up += 0.5f;
        if (up >= 18.0f)  {
           up = 0.0f;
           glutIdleFunc (NULL);     // This will stop the animation when 'up' exceeds 18.
        }
    }
     
    void handleKeypress (unsigned char key, int x, int y) 
    {
        static int animate = 0;                                 // Initially animation is turned off.
     
        switch (key) {
           case 27:   exit(0);   break;
           case 32:
              animate = !animate;                               // This will start and stop animation every time space bar is touched.
              if (animate)  glutIdleFunc (bullet);
              else          glutIdleFunc ( NULL ); 
           break;
        }
     
        glutPostRedisplay();
    }

  9. #9
    Senior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    170
    well I tried the above code, but it still does not move the bullet except when the space bar is pressed, thanks for all the help.

  10. #10
    Senior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    170
    well I put back in the glutPostredisplay() and it finally works properly, YEAH!!!!!!!!! thanks for all the help.

Page 1 of 2 12 LastLast

Similar Threads

  1. sprite bullets
    By pbivens in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-24-2018, 04:53 PM
  2. Help with Bullets
    By Unreal Player in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 05-31-2005, 03:09 AM
  3. Help with Bullets
    By Unreal Player in forum OpenGL: General
    Replies: 6
    Last Post: 05-31-2005, 03:09 AM
  4. Drawing methods for bullets and such
    By TreeMan in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-21-2002, 12:22 AM
  5. Shooting
    By in forum OpenGL: Basic Coding
    Replies: 13
    Last Post: 01-15-2001, 10:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean