PDA

View Full Version : collision detection with multiple sprites



pbivens
04-25-2015, 04:19 PM
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!!!!!!!!!

pbivens
04-27-2015, 01:25 PM
well I might use an looping structure. can you please tell if this is a good route to follow.

pbivens
04-29-2015, 09:41 PM
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");
}

pbivens
04-30-2015, 06:13 PM
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;
}

IonutCava
05-01-2015, 08:50 AM
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.

pbivens
05-01-2015, 01:22 PM
so I should use a vector?

pbivens
05-03-2015, 01:22 PM
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;
}

pbivens
05-04-2015, 06:15 PM
hey carmine can get some more help, I really like your advice

pbivens
05-06-2015, 04:26 PM
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
}

pbivens
05-08-2015, 06:22 PM
can I please get some help, I am very close to solving my problem.

pbivens
05-09-2015, 03:03 PM
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
}

pbivens
05-12-2015, 06:04 PM
can I get a little help please

Alfonse Reinheart
05-12-2015, 07:28 PM
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...

pbivens
05-13-2015, 11:24 AM
actually I am using glut to render my code

pbivens
05-14-2015, 09:08 PM
can I please get some help

Alfonse Reinheart
05-14-2015, 10:08 PM
actually I am using glut to render my code

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.

pbivens
05-15-2015, 10:25 AM
ok thanks I will post elsewhere