• Advertisement
Sign in to follow this  

Comparing two vectors...

This topic is 4742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to have simple collision detection in my game. Lists of enemies, bullets, etc. are stored in vectors I wrote this code in and attempt to compare my bullet with my enemy, which is a 50x50 box for now. this code produces unexpected results... not all enemies hit with bullets are erased... im using C++ allegro and msvc++ 6. here is the code...
	struct BULLET
	{
		int x,y;
	};

	struct ENEMY
	{
		int x,y,e1,type;
	};

	vector<BULLET> plr_bullets;
	vector<BULLET>::iterator cpb;
	BULLET temp;

	vector<ENEMY> enemy_v;
	vector<ENEMY>::iterator c_enemy;
	ENEMY etemp;


	/*Check player bullet to enemy collision*/
	if(!enemy_v.empty() && !plr_bullets.empty())
	{
		c_enemy = enemy_v.begin();
		cpb = plr_bullets.begin();
		while(cpb != plr_bullets.end())
		{
			while(c_enemy != enemy_v.end())
			{
				etemp = *c_enemy;
				temp = *cpb;
				if((temp.x >= etemp.x && temp.x <= etemp.x + 50) && (temp.y >= etemp.y && temp.y <= etemp.y + 50))
				{
					enemy_v.erase(c_enemy);
				}
				else
				{
					c_enemy++;
				}
			}
			cpb++;
		}
	}



Share this post


Link to post
Share on other sites
Advertisement
I think the line c_enemy = enemy_v.begin(); should be inside the first while loop. The way you have it would check the first bullet against all the enemies, but the subsequent bullets aren't checked against any enemies because you have already iterated to the end of the enemy list.

Share this post


Link to post
Share on other sites
This is semi-irrelevant, but instead of using that long if statment for checking collisions, you can use Windows (if you're using Windows) to check the rectangles. To do so, simply forward declare:
struct RECT { long left, top, right, bottom };
BOOL IntersectRect(RECT*,RECT*,RECT*);

Though you still have to check whether the windows libraries are compiled with your project.
Another thing: You might want to try using more object-oriented design to simplify your work.

Share this post


Link to post
Share on other sites
And deadimp, as far as i know allegro and windows.h dont get along well...

all.obj : error LNK2001: unresolved external symbol "long __cdecl IntersectRect(struct RECT *,struct RECT *,struct RECT *)" (?IntersectRect@@YAJPAURECT@@00@Z)

and I get tons of stuff about redefining stuff if I try to #include <windows.h>
Seemed nice but my big if statement seems to work better for me thanks anyways.

Share this post


Link to post
Share on other sites
how about this instead:


/*Check player bullet to enemy collision*/
if(!enemy_v.empty() && !plr_bullets.empty())
{
for(vector<BULLET>::iterator it1 = plr_bullets.begin(); it1!= plr_bullets.end(); ++it1)
{
for(vector<ENEMY>::iterator it2 = enemy_v.begin(); it2 != enemy_v.end();)
{
//compare A against B but not B against A
if((&*it1 < &*it2))
{

if((temp.x >= etemp.x && temp.x <= etemp.x + 50) && (temp.y >= etemp.y && temp.y <= etemp.y + 50))
{
enemy_v.erase(c_enemy);
}
else
{
it2++;
}

}
}
}
}






the key difference is the first if statement inside the nested loops. this compares the pointer values of each object... since each object has a unique memory address, A will be < B, but B will not be less then A. this means we only compare each object against each object a single time.

Share this post


Link to post
Share on other sites
Quote:
Original post by chad_420
And deadimp, as far as i know allegro and windows.h dont get along well...

all.obj : error LNK2001: unresolved external symbol "long __cdecl IntersectRect(struct RECT *,struct RECT *,struct RECT *)" (?IntersectRect@@YAJPAURECT@@00@Z)

and I get tons of stuff about redefining stuff if I try to #include <windows.h>
Seemed nice but my big if statement seems to work better for me thanks anyways.


Shouldn't the following work?

struct RECT
{
long left, top, right, bottom;
};

#define RECTINRECT( a, b )((((a)->right<=(b)->left)||((b)->right<=(a)->left)||((a)->bottom<=(b)->top)||((b)->bottom<=(a)->top)?FALSE:TRUE))

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement