Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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--;
        }
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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;
                    }
                }
            }
share|improve this answer

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.

share|improve this answer
    
which intersection code ? –  andre Mar 10 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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