0
\$\begingroup\$

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--;
        }
\$\endgroup\$
1
\$\begingroup\$

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 radius = 9;
            for (int yy = -radius; yy <= radius; yy++)
            {
                for (int xx = -radius; xx <= radius; xx++)
                {
                    if (xx*xx + yy*yy <= radius*radius)
                    {
                        int jj = xx + normX;
                        int ii = y + yy + normY;
                        int pixelOffset = jj + ii * 51;
                        ptr[pixelOffset] = 0xff000000;
                    }
                }
            }
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ which intersection code ? \$\endgroup\$ – andre Mar 10 '15 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.