Sign in to follow this  
xM1k3x

Real quick question

Recommended Posts

Hey guys just need your help with a function I need to pass an array to the function but I have never really done so before so this is what I have but its not working how would I make this function work? int CollDet(PlayerShip sprite1,PlayerShip 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); }

Share this post


Link to post
Share on other sites
sprite2[].x + 1;

What is sprite2[] meant to do? Your problems have nothing to do with the fact that you're passing it to a function - you have to index into an array. I think what you're looking to do is iterate over the array and test for collision for every element in it.

However, because arrays decay to pointers when passed to functions, you lose information about the length of the array. You need to either a) pass the length of the array to the function then loop over it, or b) pass a wrapper around an array that makes it behave sensibly, and won't lose information like this, such as Boost.Array.

I'd rewrite to give an example, but since I don't know exactly what the return value of the function, or of IntersectRect, represents, I can't.

Share this post


Link to post
Share on other sites
you can pass an array like above


struct SHIP{
float x,y;
float width,height;
};


void Coll(SHIP *ship,int numsprite)
{
for (int k=0;k<numsprite;k++)
{
printf("ship %d:x=%f y=%f width=%f Height=%f\n",k,ship[k].x,ship[k].y,ship[k].width,ship[k].height);
}
}

int _tmain(int argc, _TCHAR* argv[])
{
SHIP *Ship= new SHIP[2];
Ship[0].x=1;
Ship[0].y=2;
Ship[0].width=3;
Ship[0].height=4;
Ship[1].x=10;
Ship[1].y=20;
Ship[1].width=30;
Ship[1].height=40;

Coll(Ship,2);


return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
sprite2[].x + 1;

What is sprite2[] meant to do? Your problems have nothing to do with the fact that you're passing it to a function - you have to index into an array. I think what you're looking to do is iterate over the array and test for collision for every element in it.

However, because arrays decay to pointers when passed to functions, you lose information about the length of the array. You need to either a) pass the length of the array to the function then loop over it, or b) pass a wrapper around an array that makes it behave sensibly, and won't lose information like this, such as Boost.Array.

I'd rewrite to give an example, but since I don't know exactly what the return value of the function, or of IntersectRect, represents, I can't.


So your saying I cant just call the function like this:

for(int count = 0; count < ENEMYNUM; count++)
{
if(CollDet(Ship, EnmeyFire[count]))
{
ship->Destroy(); //or soomething
}
}

So I couldnt do that and have the function just take the index that is provided to it?

Share this post


Link to post
Share on other sites
Quote:
Original post by xM1k3x
So your saying I cant just call the function like this:

for(int count = 0; count < ENEMYNUM; count++)
{
if(CollDet(Ship, EnmeyFire[count]))
{
ship->Destroy(); //or soomething
}
}

So I couldnt do that and have the function just take the index that is provided to it?


You can do that, you just want to make the CollDet function accept a PlayerShip as a parameter (not an array of PlayerShip's).


int CollDet(PlayerShip sprite1, PlayerShip 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);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
Quote:
Original post by xM1k3x
So your saying I cant just call the function like this:

for(int count = 0; count < ENEMYNUM; count++)
{
if(CollDet(Ship, EnmeyFire[count]))
{
ship->Destroy(); //or soomething
}
}

So I couldnt do that and have the function just take the index that is provided to it?


You can do that, you just want to make the CollDet function accept a PlayerShip as a parameter (not an array of PlayerShip's).


int CollDet(PlayerShip sprite1, PlayerShip 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);
}



Awesome thanks.

Whats the best way to destroy the EnemyShip[count] once it has had a collision?

Its an array based ship so how should I destroy it?

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