Collision Detection Help

Started by
10 comments, last by Edge Damodred 15 years, 9 months ago
Hey All im looking for someone to look over my collision detection function its not complicated or anything and it normally works well but right now I am having some issues with it. Here is the collision detection function:

int CollDet(PlayerShip sprite1, EnemyShip sprite2)
{
   RECT rect1;
   rect1.left = sprite1.x + 1;
   rect1.top = sprite1.y + 1;
   rect1.right = sprite1.x + sprite1.width-1;
   rect1.bottom = sprite1.y + sprite1.height-1;

   RECT rect2;
   rect2.left = sprite2.x + 1;
   rect2.top = sprite2.y + 1;
   rect2.right = sprite2.x + sprite2.width-1;
   rect2.bottom = sprite2.y + sprite2.height-1;

   RECT dest;
   return IntersectRect(&dest,&rect1,&rect2);

}

Here is how I check for collision:

for(int count = 0; count < NUMFIRE; count++)
			{
				
				if(CollDet(PlayerGun[count],EnemyPawn[0]))
				{
					EnemyPawn[0].isAlive = false;
				}
				if(CollDet(PlayerGun[count],EnemyPawn[1]))
				{
					EnemyPawn[1].isAlive = false;
				}
				if(CollDet(PlayerGun[count],EnemyPawn[2]))
				{
					EnemyPawn[2].isAlive = false;
				}
			}

Here is the for statement that draws the enemy sprites on the screen as you can see it has an if statement that checks if they still should be drawn:

for (int count = 0; count < EnemyPawnCount; count++)
		  {
			if(!EnemyPawn[count].isAlive == false)
			{
			  position3.x = (float)EnemyPawn[count].x;
			  position3.y = (float)EnemyPawn[count].y;

			  sprite_handler->Draw(
				enemy_pawn,
				NULL,
				NULL,
				&position3,
				D3DCOLOR_XRGB(255,255,255));
			  }
		  }

So im sure its a basic problem but I dont see it so I need some fresh eyes.Thanks for having a look.
Advertisement
What issues are you having with it?
-------------------------------------------------my forum LINK: here
Quote:Original post by bobwrit
What issues are you having with it?


When a bullet hits the enemy it is supposed to make the enemy ship disappear registering a hit. But its not disappearing. So im not sure whats not working... but I know that the collision must not be doing what its supposed to do.
I'm not great with C in any form but this looks questionable to me:

if(!EnemyPawn[count].isAlive == false)


Maybe switch it to:

if( EnemyPawn[count].isAlive != false)


Might be way off though, I'm real rusty with the C language family.
Quote:Original post by MSW
I'm not great with C in any form but this looks questionable to me:

*** Source Snippet Removed ***

Maybe switch it to:

*** Source Snippet Removed ***

Might be way off though, I'm real rusty with the C language family.


I dont believe thats the problem because I have another statement:

if(!Player.isAlive == false)			{            //draw the sprite            sprite_handler->Draw(                player_ship,                 NULL,                NULL,                &position,                D3DCOLOR_XRGB(255,255,255));        			}


And that statement executes correctly. So its weird thats why I am looking for fresh eyes to look over my code since the collision works when checking for the player ship colliding with an enemy ship but not with a bullet colliding with an enemy ship.
Don't take this the wrong way as it just seems odd to me, but why are you doing it that way?

Why not:

If( Player.isalive == true )
{//draw sprite}

And

if( EnemyPawn[count].isAlive == true )
{//draw enemy}

You are setting the player.isalive and EnemyPawn[count].isalive to true, correct?




Also it might help to see the struct() declarations for playership, playergun and enemyship.
Quote:Original post by MSW
Don't take this the wrong way as it just seems odd to me, but why are you doing it that way?

Why not:

If( Player.isalive == true )
{//draw sprite}

And

if( EnemyPawn[count].isAlive == true )
{//draw enemy}

You are setting the player.isalive and EnemyPawn[count].isalive to true, correct?




Also it might help to see the struct() declarations for playership, playergun and enemyship.



your suggestion is basically the same thing I am already doing just reversed. Anyways here are my class declarations:

class EnemyShip{public:	int x , y , firex , firey;       // x and y variables for enemy coordinates	int width , height, firewidth , fireheight;	int bufferx , buffery , firebufferx, firebuffery;    // buffer variable is used for movement if any	void AI();	void CallFire(); // Function Prototype	bool isAlive;};



class PlayerShip{public:	int x , y , gunx , guny;        // Variables to handle Coordinates	int movex , movey;	int width , height , gunwidth , gunheight;	void CallMovement();  // Function Prototypes	void CallFire();	bool isAlive;};
Post your code for the this function:

IntersectRect(&dest,&rect1,&rect2);
Quote:Original post by snisarenko
Post your code for the this function:

IntersectRect(&dest,&rect1,&rect2);


IntersectRect is not a function I made its a windows function. The issue is not with the CollDet function because it works in another area its in my implementation of it. Something is not working correctly but I cant figure out what, thats why I am asking all you guys to check it over.
In your CollDet-function, are you sure the offsets you are applying (+1 and -1) are correct? Try playing around with them. If you set them high enough, theoretically collision should occur even if you don't hit the object. That way you can find out if it works.

Best of luck!
Student at NITH, Norway2nd year of Gameprogramming BachelordegreeC++ enthusiast

This topic is closed to new replies.

Advertisement