Sign in to follow this  
xM1k3x

Collision Detection Help

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
};


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Quote:
Original post by xM1k3x
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.


This is just a wild guess, having looked through your posted code and seeing nothing amiss, but since the issue is with bullets, is it possible that your bullets are so small and moving so fast that they actually skip over the enemy sprites?

Try slowing your bullets down to see if that "fixes" it. If so, you need to look into ways to correct this while maintaining the bullet's speed. There are complicated ways of doing this (raycasting) but a simple solution might just be to make the bullet's collision rectangle a bit wider than the bullet sprite. Bit of a hack but might do the job.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this