game project

I am going back to a game project I have worked on before. I am able to get rock sprites to move down the screen and can shoot them with bullet sprites and then they disappear. what I want is a collision sprite to be drawn after a bullet hits a rock and then the rock is erased. here is some of the code I am using, I may have asked this question before but has been a while since I have worked on this project.


void collision_rock_one()
{
	glEnable(GL_TEXTURE_2D);//collision sprite
	glBindTexture(GL_TEXTURE_2D, _textureId_four);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-7.0f, 10.0f + down, 0.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(-7.0f, 9.0f + down, 0.0f);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(-6.0f, 9.0f + down, 0.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(-6.0f, 10.0f + down, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

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

	float xTwo = -7.0f;
	float yTwo = 9.0f+down;
	float oTwoWidth = 0.5f;
	float oTwoHeight = 0.5f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		board[0][0] = 1;
	}
}

void erase_rock_one()
{
	if (board[0][0] == 1)
	{
		glColor3f(0.0f, 0.0f, 0.0f);
		glBegin(GL_POLYGON);
		glVertex3f(-7.0f, 10.0f + down, 0.0f);
		glVertex3f(-7.0f, 9.0f + down, 0.0f);
		glVertex3f(-6.0f, 9.0f + down, 0.0f);
		glVertex3f(-6.0f, 10.0f + down, 0.0f);
		glEnd();
	}
}

am I asking the wrong type of question, can I please get some help.

You seem to be misunderstanding a few things.

You don’t erase objects from a scene.

What you do instead is redraw the full scene, but excluding the object you want removed.

You should be redrawing the full scene every frame anyway, so suppose that you have 4 objects in the scene; a normal frame might look like this:

  • Draw object 1
  • Draw object 2
  • Draw object 3
  • Draw object 4

Now suppose a bullet hits and destroys object 3. You don’t erase object 3. Instead, your frames now look like this:

  • Draw object 1
  • Draw object 2
  • Draw object 4

How you do this is entirely up to you. You might maintain something like a std::vector (or similar) of objects to be drawn, and remove objects that are no longer to be drawn. Or you might set a flag in an object so that your drawing code can know to skip it. Or you might do it an entirely other way. The important points are: (1), no, you don’t erase objects from the scene, instead you simply redraw the scene without the objects you want removed, (2) you should be redrawing the scene every frame anyway, so this should be utterly trivial to do, and, (3), no, it’s not inefficient to do this, it’s what all games do; Quake did it in 1996; is Quake slow?

what I mean by erase is that I draw an object using glColor3f(0.0f,0.0f,0.0f); which turns the sprite to black.

Why? Just stop drawing this object instead of drawing it “invisible”.

this is probably a dumb question but what do you mean by stop drawing an object, I am just a little confused, I will work on this problem more.

Something like this pseudo code


if (Rock_not_destroyed == true)
{
    ... // do draw setup
    glBegin(GL_POLYGON);
    ...  // draw your vertices
    glEnd();
}


If your Rock is destroyed, everything inside the if block is not executed. So instead of making it black to be invisible, you just skip drawing it.

thanks programmerx I will have to work on this problem, do you have anymore input that might help me?

Well, best thing you can do, if you really want to build a game is work through a lot of tutorials and actually understand what they are doing and why:

I know it is not as interesting and thrilling as building a real game, but knowing the basics is essential and will spare you a lot of frustration and confusion.

Well… to be honest, when I started, I didn’t listen to the guys telling me to learn the basics… :stuck_out_tongue: . But I wish I had… would have saved me a lot of time. Anyways, good luck with your project.

At this point, general programming concepts are probably more important than anything specific to OpenGL.

First, you need to decide how to structure the data which holds the state of the world. E.g. player position and orientation, a list of rocks, a list of bullets. Each “tick” (which isn’t necessarily the same as a frame; ticks should be at a constant rate if the frame rate varies), you need to update the state of the world: move the player, move rocks, move bullets, add new bullets, check for collisions between bullets and rocks, split/remove rocks. Each frame, you need to render the world.

These principles hold for any game. For something like Asteroids, maintaining and updating the world state is fairly simple. For more complex games this gets more difficult as you can’t implement long-term actions using e.g. loops; you have to return to the main loop at the end of each tick so the world state has to hold enough information to resume the action on the next tick.

I am watching some youtube videos on linear algebra, they will help me understand matrixes and vectors. I am also going to watch some videos on opengl as well. thanks for all the help.

I am studying a text on linear algebra I am reading and doing the odd exercises in the back of the chapters, I am on chapter 2 which is all about matrices, which will help me in my graphics programming using opengl.

well I am still studying linear algebra, I am on chapter 3 which is determents , this should prove to be very interesting.

well I am working on a checkers game, I want to drag and drop the pieces. I am able to draw the board and the pieces. here is the code I am working on.


void drawScene_move()
{
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId_three);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	//Back
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glColor3f(1.0f, 1.0f, 1.0f);

		glBegin(GL_POLYGON);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-3.0f, -1.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-3.0f, -2.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-2.0f, -2.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-2.0f, -1.0f, 0.0f);
		glEnd();

	glDisable(GL_TEXTURE_2D);
}

void blank_piece()
{
	if (board[5][1] == 1)
	{
		drawScene_move();
	}
}

void onMouseButton(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		cout << x << " " << y << endl;
	
		if (x >= 280 && x <= 320 && y <= 360 && y >= 330)
		{
			board[5][1] = 1;
		}
		if (x >= 240 && x <= 280 && y <= 330 && y >= 300)
		{
			board[4][0] = 1;
		}
		if (x >= 320 && x <= 360 && y <= 330 && y >= 300)
		{
			board[4][2] = 1;
		}
		blank_piece();
	}
	glFlush();
}

void drawScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	drawScene_white();
	drawScene_black();
	drawScene_blank();
	blank_piece();
	glFlush();
}

I am able to click on a piece and make it disappear but I want to drag and drop the piece I have selected.

well I have made some progress in my checkers game, I want to drag and drop the pieces. here is the code I am using.


void black_piece()
{
	if (board[5][1] == 1)
	{
		drawScene_move();
	}
	if (board[4][0] == 1)
	{
		drawScene_move_one();
	}
	if (board[4][2] == 1)
	{
		drawScene_move_two();
	}
}

void mouseMotion(int x, int y)
{
	cout << x << " " << y << endl;
	board[(y - 180) / 30][(x - 240) / 40] = 1;

	black_piece();
	glFlush();
}