# algorithm for bullets to base collision in space invader

## Recommended Posts

AhmedCoeia    510

Hi All,

I'm trying to clone space invader, and right now I'm in the step of implemening bullets with base(Green ones) collision.

So I considered each base as a rectangle and a bullet as a rectangle. I have an image that consists of small green pixels, that I want to overdraw as a damage.

I can't come up with an algorithm.. or an idea how to do that

Right now I'm trying to draw the damage bitmap over the hit position of the bullet, but it's not redrawn each frame..

void CheckBulletsBarriersCollision(Ship * ship, vector<Barrier*> barriers, Resources *resources)
{
bool isCollision = false;
CIwSVec2 collisionPoint;

for (vector<Barrier*>::iterator itBarrier = barriers.begin(); itBarrier != barriers.end(); itBarrier++)
{
for (list<Bullet*>::iterator it = ship->Bullets->begin(); it != ship->Bullets->end(); it++)
{
float left = (*it)->Position.x - 5;
float top = (*it)->Position.y - 13;
if (CheckCollision(left, top,
(*itBarrier)->Position.x,
20
, (*itBarrier)->Position.y - (*itBarrier)->mBarrierImage->GetHeight()
, 36))
{
isCollision = true;
collisionPoint.x = left;
collisionPoint.y = top;
break;
}
}
}

if (isCollision)
{
DrawDamage(collisionPoint.x, collisionPoint.y, resources);

}
}

##### Share on other sites
LorenzoGatti    4449

You are calling DrawDamage() only once, with the data for a random collision, rather than once per collision.

##### Share on other sites
Brain    18906
Base as a rectangle might not work as nicely as bits of the base get chipped away, collisions will be triggered before the base is actually hit by the bullet.

You could do this the way it was done when space invaders was created, e.g. using a collision mask which is ANDed against the sprites using logic operations. Look up 2d collision masks on Google for some good examples.

This is still reasonably efficient and will teach you some concepts useful for understanding how sprites are stored in the graphics ram.

##### Share on other sites
AhmedCoeia    510

I tried to google  and look at the concept but still didn't get it on how to solve the problem.  A pseudo code is obviously helpful.

Right now I'm using that code

for (vector<Barrier*>::iterator itBarrier = barriers.begin(); itBarrier != barriers.end(); itBarrier++)
{
for (list<Bullet*>::iterator it = ship->Bullets->begin(); it != ship->Bullets->end(); it++)
{
float left = (*it)->Position.x - 5;
float top = (*it)->Position.y - 13;

if (CheckCollision(left, top,
(*itBarrier)->Position.x,
22
, (*itBarrier)->Position.y - (*itBarrier)->mBarrierImage->GetHeight()
, 30))
{
l = left;
t = top+ (*itBarrier)->mBarrierImage->GetHeight()*2;

break;
CIwTexture *tex = (*itBarrier)->mBarrierImage->GetMaterial()->GetTexture();
tex->SetModifiable(true);
tex->MakeModifiable(true);
uint8 *texls = tex->GetTexels();
uint8 *texlsEnd = texls + tex->GetTexelsMemSize();

}
}
}


## 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