sprite collision

I am working on a 1945 plane game. I have got a bullet to hit a plane and then draw collision sprite but then it redraws the plane sprite again. I want it to black out the plane sprite after the collision sprite is drawn for a brief period of time. I am using the sleep function to make a small delay. here is the code I am working on.


void collision(int value)
{
//	flag[0][0] = 1;
	glutTimerFunc(1000, collision, 5);
}

void coll_plane_one()
{
	float x = -0.5f + scroll;
	float y = -8.0f + up;
	float oWidth = 0.5f;
	float oHeight = 0.5f;

	float xTwo = -7.5f;
	float yTwo = 9.0f + down;
	float oTwoWidth = 1.0f;
	float oTwoHeight = 1.5f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		drawScene_collision_one(-7.5f, 10.0f);
		Sleep(50);
		glutTimerFunc(50, collision, 5);
	}
}

void erase_sprite()
{
	if (flag[0][0] == 1)
	{
		glColor3f(0.0f, 0.0f, 0.0f);
		glBegin(GL_POLYGON);
		glVertex3f(-7.5f, 10.0f+down, 0.0f);
		glVertex3f(-7.5f, 9.0f+down, 0.0f);
		glVertex3f(-6.5f, 9.0f+down, 0.0f);
		glVertex3f(-6.5f, 10.0f+down, 0.0f);
		glEnd();
	}
}

void drawScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	bullet();
	drawScene_plane_two();
	coll_plane_one();
	erase_sprite();
	drawScene_plane_one();
	glutSwapBuffers();
}

I know I have asked a similar question before, but I am still stuck.

how do you turn off a sprite?

OpenGL doesn’t know anything about the scene you’re trying to render. You don’t “turn on” or “turn off” anything. You draw what you want to draw for the frame you want to draw it in. And if you don’t want to draw it that frame, then you don’t draw it that frame.

So you “turn off” a sprite by not drawing it. You still have to draw everything else. So sleeping in the middle of your loop is not helping anyone.

what do you mean by frame?

The GL calls between successive calls to SwapBuffers() (e.g. glutSwapBuffers() in your case). In your program, that’s one execution of drawScene().

cool thanks is it possible to use glutSwapbuffers more then once?

Yes. You’re doing it. Your drawScene() function is being called repeatedly in a loop. It’s just that with GLUT, the loop is behind-the-scenes in glutMainLoop().

how do I stop drawing a sprite in the drawScene function? also I think that clock_t c = clock(); function will help in my problem.

Don’t want to be rude, but maybe you should spend some time in learning more about programming in general (especially object oriented programming). It does not make any sense to program a game if you don’t know the simple basics. If somebody gives you an answer that solves the problem (what Alfonse Reinheart already did) you will end up asking the next question about some basics in a few days because you didn’t learn them. You should get a good book or a fitting Internet source and start reading and programming tutorials. If you don’t understand some things in a tutorials you can always ask here but you need to learn them before trying to make a game (even a simple one).

So to give you a hint, what you can do:
Create a class called “Plane” which stores information about the plane like its position or if it is alive. It provides functions which give you information about its current status and to modify the status. It might even have a function which draws it like your function “drawScene_plane_one()”. Then you could put something like the following into your code function:



// ... somewhere in your definitions section

class Plane
{
    // ... fill with details about a plane
}

// ... somewhere during initialization declare an instance of Plane
Plane plane1;


// ... in your draw scene
if(plane1.isAlive())
{
    plane1.draw();    // draw plane, cause it is not dead
}
else
{
    if(plane1.isExploding) // if it is exploding, draw a fireball, if not, don't do anything
    {
        plane1.drawExplosionAnimation();
    }
}
...

This is just an example what you can do and not necessarily the best way to do things. Anyways, try to do it like that. You probably need to do some tutorials before you are able to implement the missing class code. Try for example this one: Object Oriented Programming in C++ - Gillius's Programming

Greetings

i am also studying a college level text on c++ and am doing the exercises in the backs of the chapters, I am on ch.7 which is on classes and objects

well before I use classes I am going to implement some Boolean values. I have almost solved my problem. I am able to draw a plane then shoot it and draw a collision sprite, all I want to do next is black out the collision sprite.


void wait()
{
	if (sprite == true)
	{
		drawScene_collision_one(-7.5f, 10.0f);
		sprite = true;
	}
	else if (sprite == false)
	{
		sprite = false;
	}
}

void coll_plane_one()
{
	float x = -0.5f + scroll;
	float y = -8.0f + up;
	float oWidth = 0.5f;
	float oHeight = 0.5f;

	float xTwo = -7.5f;
	float yTwo = 9.0f + down;
	float oTwoWidth = 1.0f;
	float oTwoHeight = 1.5f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		sprite = true;
	}
}

void drawScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	bullet();
	drawScene_plane_two();
	coll_plane_one();
	wait();
	drawScene_plane_one();
	glutSwapBuffers();
}

like I said I have almost solved my problem, I just need a little hint.

can I please get some help I am almost solved my problem.

What’s your question? You didn’t ask one.

We’re happy to answer specific OpenGL-related questions, but we’re not going to go fix your code for you.

ok sorry about that I will try harder