Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: collision detection with multiple sprites

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127

    Talking 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!!!!!!!!!

  2. #2
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127
    well I might use an looping structure. can you please tell if this is a good route to follow.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127
    well I worked on the following code.
    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");
    }

  4. #4
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127
    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
    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;
    }

  5. #5
    Junior Member Newbie IonutCava's Avatar
    Join Date
    May 2012
    Location
    Bristol, U.K.
    Posts
    27
    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.

  6. #6
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127
    so I should use a vector?

  7. #7
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127
    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.
    Code :
    #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;
    }

  8. #8
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127
    hey carmine can get some more help, I really like your advice

  9. #9
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127
    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.
    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();
     
    	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
    }

  10. #10
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    127
    can I please get some help, I am very close to solving my problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •