PDA

View Full Version : machine gun shooting bullets



pbivens
03-15-2015, 08:28 PM
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.


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.

EmJayJay
03-16-2015, 06:15 AM
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.

pbivens
04-09-2015, 09:36 PM
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.


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

pbivens
04-15-2015, 01:28 PM
can I please get some help, is this too hard of a question?

Carmine
04-15-2015, 02:37 PM
... 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.

pbivens
04-18-2015, 03:26 PM
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


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

pbivens
04-19-2015, 02:13 PM
well thank you carmine, I just need a little hint, I have almost solved my problem.

Carmine
04-19-2015, 02:33 PM
... 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.


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

pbivens
04-19-2015, 03:56 PM
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.

pbivens
04-19-2015, 04:02 PM
well I put back in the glutPostredisplay() and it finally works properly, YEAH!!!!!!!!! thanks for all the help.

pbivens
04-19-2015, 04:34 PM
I have one little bug in my code and that is that I have to press the space bar twice in order to shoot a bullet, thanks for all the help carmine.

Carmine
04-21-2015, 10:04 AM
... one little bug in my code and that is that I have to press the space bar twice in order to shoot a bullet .... Try making 'animate' a global variable. Then when up >= 18, set animate = 0.

pbivens
04-21-2015, 04:38 PM
cool that works, thanks for all the help!!!!!!!