Jump to content
  • Advertisement
phil67rpg

OpenGL sprite collision

Recommended Posts

all I want to do is draw a animated sprite when a bullet  hits a  sprite. here is my collision detection function and my sprite drawing function all I need is a second pair of eyes to look at my code.

void drawcollision_one()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[3]);

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f + screen, 0.0f, 0.0f);

	glVertex3f(10.0f, -10.0f, 0.0f);
	glTexCoord3f(0.167f + screen, 0.0f, 0.0f);

	glVertex3f(10.0f, 10.0f, 0.0f);
	glTexCoord3f(0.167f + screen, 1.0f, 0.0f);

	glVertex3f(-10.0f, 10.0f, 0.0f);
	glTexCoord3f(0.0f + screen, 1.0f, 0.0f);

	glVertex3f(-10.0f, -10.0f, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

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

 

Share this post


Link to post
Share on other sites
Advertisement

You've told us what you want your code to do.

You haven't told us what it actually does?

 

So, does it work?  If not, what does it do incorrectly?

Share this post


Link to post
Share on other sites

Post from almost 1 year ago for reference (exact same code "logic", some variable values changed), following the exact same posting pattern/order as before. (Sprite sheet animation followed by collision).

My guess is next topic is giving up on this game and swapping to snake in C#, or some other project.

Share this post


Link to post
Share on other sites

here is my collision function

bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
{
	// AABB 1
	float x1Min = x;
	float x1Max = x + oWidth;
	float y1Max = y + oHeight;
	float y1Min = y;

	// AABB 2
	float x2Min = xTwo;
	float x2Max = xTwo + oTwoWidth;
	float y2Max = yTwo + oTwoHeight;
	float y2Min = yTwo;

	// Collision tests
	if (x1Max < x2Min || x1Min > x2Max) return false;
	if (y1Max < y2Min || y1Min > y2Max) return false;

	return true;
}

 

Share this post


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

// Collision tests if (x1Max < x2Min || x1Min > x2Max) return false; if (y1Max < y2Min || y1Min > y2Max) return false;

That. Feels broken.

Perhaps think the other way around. Instead of focusing outside the potential overlap area, reverse it. Redesign not to test that it isn't, like you do currently, but to test that it is. (Hunt: && instead of ||)

Whoops...I thought  there was a collision issue...my fail...I'm blinded by the magic numbers.

Share this post


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

That. Feels broken.

All other issues in this thread aside, in the interest of preventing possible confusion for future readers I'll say that although I haven't confirmed the code in question is correct, it's not immediately obvious to me that it isn't. If you think it's wrong it might be helpful to specify exactly how it's wrong.

Quote

Perhaps think the other way around. Instead of focusing outside the potential overlap area, reverse it. Redesign not to test that it isn't, like you do currently, but to test that it is. (Hint: && instead of ||)

Irrespective of whether this particular implementation is correct, I'd argue there's nothing wrong with expressing the algorithm as it's expressed here. This is essentially a separating axis test, which is often expressed exactly in this way - that is, returning false if any axis is a separating axis, and returning true if there is no such axis.

Share this post


Link to post
Share on other sites

actually the collision function works just fine, I have stubbed it out. I just have a problem with the drawcollision_one() not executing when the collision is detected.

Share this post


Link to post
Share on other sites

Yes, it is executed when there is an overlap.

Tested with

float x = 1.0f;
float y = 1.0f;
float oWidth = 5.0f;
float oHeight = 5.0f;
//draw plane
float xTwo = 0.0f;
float yTwo = 0.0f;
float oTwoWidth = 10.0f;
float oTwoHeight = 10.0f;

 

Edited by Green_Baron

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!