Jump to content
• Advertisement

# algorithm for bullets to base collision in space invader

This topic is 1240 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

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 this post

##### Share on other sites
Advertisement

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

#### Share this post

##### Share on other sites
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 this post

##### Share on other sites

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();

}
}
}

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
• Advertisement

• 15
• 13
• 9
• 12
• 10
• ### Forum Statistics

• Total Topics
631442
• Total Posts
3000089
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!