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 radius = 9;
for (int y = -radius; y <= radius; y++)
{
for (int x = -radius; x <= radius; x++)
{
if (x*x + y*y <= radius*radius)
{
int j = x + normX;
int i = y + normY;
int pixelOffset = j + i * 51;
ptr[pixelOffset] = 0xff000000;
}
}
}
break;
}
// go up
y--;
}