algorithm for bullets to base collision in space invader

Started by
2 comments, last by AhmedCoeia 9 years, 2 months ago

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);
 
}
}
Advertisement

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

Omae Wa Mou Shindeiru

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.

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

This topic is closed to new replies.

Advertisement