c++algorithm2dmarmalade

Blasting black circle in space invader


I'm trying to clone space invader, collision with the green barriers. https://www.youtube.com/watch?v=bLAhmnCZym4

Right now I have access to the pixels of the green barrier I would like to draw a solid black circle around the collision point of the bullet, right now I'm using the following code, but it spread random pixels, not solid black circle and it's center the hitting point of the bullet The result is shown here: https://i.sstatic.net/mpgkM.png

int radius = 50;
            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;
                        uint8 pixelOffset = j + i;
                        ptr += pixelOffset;
                        *ptr = 0xff000000;
                    }
                }
            }

Solution

  • Pixels are normally stored in a raster order so you need to change

    uint8 pixelOffset = j + i;
    

    to

    int pixelOffset = j + i*pitch;
    

    where pitch is the width of your image.

    In addition, each time you write a pixel you are moving ptr to a new location so you get a diagonal line.

    Replace

                        ptr += pixelOffset;
                        *ptr = 0xff000000;
    

    with

                        ptr[pixelOffset] = 0xff000000;