collision detection with multiple sprites

well I tried googling it but not find much, I am still making a space invaders/galaga game. I have drawn the bugs sprites and space ship and bullets. I want to implement AABB collision detection affecting multiple sprites. I am unsure how to begin, I will try googling it more. I want to thank carmine for all the help given to me. THANKS ALOT!!!

well I might use an looping structure. can you please tell if this is a good route to follow.

well I worked on the following code.


#include <iostream>
#include <vector>

using namespace std;

vector <int> game_objects(2);

int main()
{
	for (int i = 1; i < game_objects.size(); i++)
	{
		for (int j = 0; j < i; j++)
		{
			if (game_objects[i]==game_objects[j])
			{
				cout << "Collision" << endl;
			}
			cout << "No Collision" << endl;
		}
	}
	system("pause");
}

well I have stubbed out some collision detection code, this is good for one on one collisions but I want to use collision detection on several objects here is my code


#include <iostream>

using namespace std;

struct rect
{
	float top;
	float left;
	float bottom;
	float right;
};

bool rectCollision(rect a, rect b)
{
	return !(b.left > a.right || b.right < a.left || b.top < a.bottom || b.bottom > a.top);
}

int main()
{
	rect a;
	rect b;

	a.top = 0.0f;
	a.left = 0.0f;
	a.bottom = 0.0f;
	a.right = 0.0f;

	b.top = 0.0f;
	b.left = 0.0f;
	b.bottom = 0.0f;
	b.right = 0.0f;

	if (rectCollision(a, b) == 1)
	{
		cout << "Collision" << endl;
	}
	
	else if (rectCollision(a, b) == 0)
	{
		cout << "No Collision" << endl;
	}
	
	system("pause");

	return 0;
}

If you have all of your sprites, why not store all the “alive” bugs in a vector somewhere. When you fire a bullet, keep track of it and its up-to-date bounds.
Each animation frame, just loop over the vector of bugs and use your “rectCollision(bullet, bug[i])” to check if you hit something.
Repeat the process with your ship instead of the bullet to see if you died. Repeat the process with any projectile the bugs might fire to see if you got shot.

so I should use a vector?

well I have stubbed some code that puts a struct in a vector and prints it out. next I have to integrate it into my game code, am I on the right path? here is my code so far.


#include <iostream>
#include <vector>

using namespace std;

struct rect
{
	float top;
	float left;
	float bottom;
	float right;
};

int main()
{
	vector <rect> bug;

	for (float i = -8.5f; i <= 7.5f; i += 2.0f)
	{
		bug.push_back({ 10.0f, i, 9.0f, i + 1.0f });
	}

	for (int j = 0; j < 9; j++)
	{
		cout << bug[j].top << " " << bug[j].left << " " << bug[j].bottom << " " << bug[j].right << endl;
	}
	system("pause");
	return 0;
}

hey carmine can get some more help, I really like your advice

well I have worked more on my code, I think that I am really close to solving my problem. Here is my code so far. Please help.


void collision_vec()
{
	for (float i = -8.5f; i <= 7.5f; i += 2.0f)
	{
		bug.push_back({ 10.0f, i, 9.0f, i + 1.0f });
	}
}

void collision()
{
	rect bullet;

	collision_vec();
	
	bug[j].top;
	bug[j].left;
	bug[j].bottom;
	bug[j].right;

	bullet.top = -7.5f + up;
	bullet.left = -0.25f + scroll;
	bullet.bottom = -8.0f + up;
	bullet.right = 0.25f + scroll;

	if (rectCollision(bullet, bug[j]) == 1)
	{
		drawScene_coll();
	}
	// return 1 if collision
	// return 0 if no collision
}

can I please get some help, I am very close to solving my problem.

well I have made some progress, but I want to know if I can shorten this piece of code


void collision_vec()
{
	for (float i = -8.5f; i <= 7.5f; i += 2.0f)
	{
		bug.push_back({ 10.0f, i, 9.0f, i + 1.0f });
	}
}

void collision()
{
	rect bullet;

	collision_vec();

	bullet.top = -7.5f + up;
	bullet.left = -0.25f + scroll;
	bullet.bottom = -8.0f + up;
	bullet.right = 0.25f + scroll;

	if (bullet.left >= -8.5f && bullet.right <= -7.5f)
	{
		j = 0;
	}

	else if (bullet.left >= -6.5f && bullet.right <= -5.5f)
	{
		j = 1;
	}

	bug[j].top;
	bug[j].left;
	bug[j].bottom;
	bug[j].right;

	if (rectCollision(bullet, bug[j]) == 1)
	{
		drawScene_coll();
	}
	// return 1 if collision
	// return 0 if no collision
}

can I get a little help please

Well, this is an OpenGL forum, not a general programming forum. So collision detection is generally off-topic, since it’s not a part of OpenGL. Not unless you’re trying to use OpenGL to do collision detection…

actually I am using glut to render my code

can I please get some help

Just because your code using OpenGL somewhere doesn’t make your problem an OpenGL problem. Your problem is about the collision detection in your code, which is off-topic for the forum.

Also, please stop bumping your thread. We’ve seen it. If people choose not to respond, that’s their choice.

ok thanks I will post elsewhere