sprite collision

Started by
59 comments, last by JTippetts 4 years, 7 months ago

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

 

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?

- Jason Astle-Adams

ok when the bullet hits the plane sprite it simply passes through it basically it does nothing.

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.

Hello to all my stalkers.

Where is the code for checkCollide()?

- Jason Astle-Adams

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

 

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.

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.

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.

This topic is closed to new replies.

Advertisement