Skip to main content
GameDev.net gamedev.net
🔒 Locked

sprite collision

Started by phil67rpg Sep 1, 2019 at 10:20 PM 63 replies 28.6k views
Original Post
phil67rpg
phil67rpg

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


jbadams
jbadams

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
phil67rpg
phil67rpg

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

Lactose
Lactose

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.
jbadams
jbadams

Where is the code for checkCollide()?

- Jason Astle-Adams
phil67rpg
phil67rpg

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


NubDevice
NubDevice
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.

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.
Zakwayda
Zakwayda
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.

phil67rpg
phil67rpg

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.

Green_Baron
Green_Baron

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;


phil67rpg
phil67rpg

I just don't know why the drawcollision_one() function does not execute when there is an overlap it also outputs the collision string to the screen using cout and the "collision" string. here is my test code.


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();
		cout << "Collision" << endl;
	}
}


rileyman
rileyman

We don't know the values of "move_plane", "up", and "down". If those were all zero, the bullet is at y = -75, and the plane is at +100. So at the very least, "up" would have to be +170.1 or more for the collision to occur. Or "down" would have to be -170.1. Or some combination.

So I think we'd need to see how "up" and "down" are being updated.

Senior software developer with a passion for games, still hoping to break into the industry after all these years...
phil67rpg
phil67rpg

here is my up and down functions


void shoot()
{
	up++;
	if (up >= 175.0f)
	{
		up = 0.0f;
		glutIdleFunc(NULL);
	}
	glutPostRedisplay();
}

void timer(int v)
{
	down--;
	if (down <= -180.0f)
	{
		down = 0.0f;
	}
	glutPostRedisplay();
	glutTimerFunc(50, timer, 0);
}


phil67rpg
phil67rpg

hey anyone do you have input on my question?

fleabay
fleabay
2 hours ago, phil67rpg said:

hey anyone do you have input on my question?

You haven't asked a question.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.
Tom Sloper
Tom Sloper

Does it work now, Phil? If not, what have you tried (besides posting here)?

-- Tom Sloper    --      sloperama.com
phil67rpg
phil67rpg

well it does not work, I have tried using the cout command and it outputs "collision" to the screen but it does not draw the animated sprite to the screen. the problem is probably in the drawcollsion_one() function It looks like it should work but when the bullet hits the enemy plane it just passes through it but does not draw the collision animated sprite.

rileyman
rileyman

These are the things I'd be checking at this point:

  • Are the vertices being drawn in the correct order? If the polygon is facing the wrong way, you won't see anything.
  • Is it being drawn off screen?
  • Was the texture loaded properly?
Senior software developer with a passion for games, still hoping to break into the industry after all these years...
8Observer8
8Observer8

@phil67rpg You need to start with something very simple. For example, Pong 2D Game. In this game you have packets and a ball. This simple step-by-step instruction shows how to make collision detection between rackets, a ball, and walls. This tutorial uses OpenGL 1.1, C++ and Glut like you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.