Skip to main content
gamedev.net
🔒 Locked

Space invader barrier implementation algorithm

Started by AhmedCoeia Mar 10, 2015 at 10:59 PM 1 replies 1.7k views
Original Post
AhmedCoeia
AhmedCoeia

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,

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--;
        }
jbadams
jbadams

Ahmed, this seems to be the same question you asked on Feb 24th, Feb 19th, Feb 15th, and on Feb 9th.

All of these are building on exactly the same basic problem, so instead of constantly reposting what is basically the same question with a few small details changed as you've progressed you should reply again to your older topic so that people can see what you have been doing and what advice has already been given.

I'm closing this topic, and suggesting that you post in the most recent of your previous ones instead. Let people know which of their suggestions you've already tried, what progress (if any) you've made, and what problems you're still having.

- Jason Astle-Adams
jbadams
jbadams

One additional note: I just found that this post is also on the same topic, and I would probably agree with frob's assessment that this project seems to be above your current skill level. It might be beneficial to put this aside for now and go back to basics before attempting it again.

- Jason Astle-Adams

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.