# Space invader barrier implementation algorithm

I've been struggling with implementing the space invader bullet to barriers. I wanted right now to blast a circle around, when the bullet hits the barrier and modifies the circle. As shown in the video, the algorithm looks wrong, http://youtu.be/VMBczCsP3J4 I have tried my best to debug it, but I've failed.

here is the algorithm:

// if there is intersection
if (IntersectsWith((*it)->BoundingRect, barrierRect))
{
// subtract (*it) bullet position from barrrier's rect
int normX = (*it)->Position.x - barrierRect.x;
int normY = (*it)->Position.y - barrierRect.y;

// barrier image pixels
uint32* ptr = (uint32*)pixels;
// height of the barrier
int y = 31;

for (int i = 0; i < 31; i++)
{
int pixelOffset = y + normX * 51;
int color = ptr[pixelOffset];
// look up the color if its not black, it should be green
if (color != 0xFF000000)
{
// delete the bullet
Bullet *bullet = *it;
it = ship->Bullets->erase(it);
delete bullet;
ship->Canfire = true;
isCollision = true;

// blast a circle around that bullet hit position
{
{
{
int j = x + normX;
int i = y + normY;
int pixelOffset = j + i * 51;
ptr[pixelOffset] = 0xff000000;
}
}
}
break;
}
// go up
y--;
}


Your pixelOffset calculations are inconsistent...

int pixelOffset = y + normX * 51;


is different than...

int j = x + normX;
int i = y + normY;
int pixelOffset = j + i * 51;


One uses x * 51 + y and the other uses y * 51 + x.

EDIT You are also reusing variable i and y.
I think you want your blast circle to be based on your outer y value in addition to the inner. Notice y + yy + normY below.

            // blast a circle around that bullet hit position
{
{
{
int jj = xx + normX;
int ii = y + yy + normY;
int pixelOffset = jj + ii * 51;
ptr[pixelOffset] = 0xff000000;
}
}
}


Looks to me that your intersection code is not returning the correct position of impact. This could due that that when the game checks whether the bullet collide with the object, the bullet is already inside the barrier. You need to modify the intersection code to return the exact moment when the bullet hits the barrier.

• which intersection code ?
– user54791
Mar 10, 2015 at 22:16