Jump to content
  • Advertisement
phil67rpg

OpenGL sprite collision

Recommended Posts

13 minutes ago, rileyman said:

It's helpful to organize your game loop to update the entire game state first, then perform all of your rendering in one go.

can you explain this more specifically.

Share this post


Link to post
Share on other sites
Advertisement

Eep, I'll do my best. 😏

Updating your game state is where you're changing all the variable values... Changing the position of the bullet and plane you already know.  Setting a flag indicating whether a collision occurred can be another part of your game state.

Rendering is where you draw everything.  What I suspect is that the following is happening:

  • If a collision occurred, draw the collision sprite.
  • Clear the buffer.
  • Draw the bullet and plane.
  • Swap the buffer to the screen (ie. the "glutPostRedisplay" call).

To make sure everything you're drawing actually gets shown on the screen, you need to clear the buffer first, then draw everything, then call "glutPostRedisplay" at the end (and only once).

Articles like this one covering game loops are good reading for any game dev.

Share this post


Link to post
Share on other sites
3 hours ago, rileyman said:

When you call "checkCollide", you could instead set a variable (let's say, "bool drawCollisionFlag").  And then in your draw / render pass, check the value of that flag and call "drawcollision_one" if true.

well I tried this but still  does not work

void coll_plane_one()
{
	//draw bullet
	float x = -2.5f + move_plane;
	float y = -75.0f + up;
	float oWidth = 5.0f;
	float oHeight = 5.0f;
	//draw plane
	float xTwo = -10.0f + move_plane;
	float yTwo = 100.0f + down;
	float oTwoWidth = 20.0f;
	float oTwoHeight = 20.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		collision_flag = true;
		cout << "Collision" << endl;
	}
}
void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture[2]);

	for (float i = -130.0f; i <= 130.0f; i += 40.0f)
	{
		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex2f(i, 100.0f + down);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2f(i+20.0f, 100.0f + down);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2f(i+20.0f, 80.0f + down);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2f(i, 80.0f + down);
		glEnd();
	}

	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(-10.0f+move_plane, -80.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(10.0f+move_plane, -80.0f);
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(10.0f+move_plane, -100.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(-10.0f+move_plane, -100.0f);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, texture[1]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(-2.5f+move_plane, -75.0f+up);
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(2.5f+move_plane, -75.0f+up);
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(2.5f+move_plane, -80.0f+up);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(-2.5f+move_plane, -80.0f+up);
	glEnd();
	if (collision_flag == true)
	{
	drawcollision_one();
	}
	coll_plane_one();
	glPopMatrix();
	glutSwapBuffers();
	glDisable(GL_TEXTURE_2D);
}

 

Share this post


Link to post
Share on other sites
9 hours ago, phil67rpg said:

actually I have done pong before in glut, I want  to finish this game

That tutorial is not about pong for you. It is about: collision detection, better code organisation, game loop in GLUT and so on.

Edited by 8Observer8

Share this post


Link to post
Share on other sites
1 hour ago, phil67rpg said:

that site is not secure

Just allow your browser to load this site. Click the "Advanced" button in Chrome.

I wrote to author about this problem. Michael answered me:
 

Quote

Hey,

sorry about that. I renewed the SSL certificate yesterday but it didn't seem to work yet. We'll work on it :)

 

Edited by 8Observer8

Share this post


Link to post
Share on other sites

Based on the code you're showing above, you're checking whether "collision_flag" is true before you call the "coll_plane_one" function, which is where you set that flag.

I also would have expected the "coll_plane_one" call to occur in the update phase of the game loop, but that isn't the actual cause of the issue.  It's still hard to tell if there might be other issues causing the collision sprite to not appear, as we're only seeing snippets of the entire program at different points in time.  It's probably going to come down to stepping through your code and watching variable values change in the debugger.

Share this post


Link to post
Share on other sites

well I think my problem is that when I call drawcollision_one function outside the renderscene loop it does not draw the animated sprite. I think it is a scoping problem.

Share this post


Link to post
Share on other sites
5 hours ago, phil67rpg said:

well I think my problem is that when I call drawcollision_one function outside the renderscene loop it does not draw the animated sprite. I think it is a scoping problem.

what makes you think it's a scoping problem?

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!