problem with laser gun!

This is a discussion on problem with laser gun! within the Game Programming forums, part of the General Programming Boards category; Hi there, I am starting a game using the Win32 API. This is my first game. The game is going ...

  1. #1
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002
    Posts
    48

    problem with laser gun!

    Hi there,

    I am starting a game using the Win32 API. This is my first game. The game is going to be an imitation of those asteroid games.

    Well i made the ship, or aircraft, the background (supposed to be water) and the laserbeam that you can shoot. However, i dont know how to shoot it more then once: Everytime I shoot, the old laser beam, dissapears and the new one comes in place. Do I need to use a dynamic array for such reppetitive actions, since its not known how many times the gun will shoot? (would a vector be the right type of dynamic array?)

    well check it out if you want to better understand what i mean,

    move - use arrow keys
    shoot - use space bar

    tell me about any bugs.

    action
    Attached Files Attached Files

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    There are several ways to track this:

    Either use a linked list or use a an array with a fixed number of elements.



    Array version
    Code:
    typedef int BOOL;
    
    struct point2D
    {
       double x;
       double y;
    };
    
    struct Bullet
    {
       point2D ScreenPos;
       point2D Velocity;
       double Speed;
       BOOL Active;
    };
    
    
    #define MAXBULLETS 50
    
    Bullet BulletArray[MAXBULLETS];
    
    
    //Sample function
    void MoveBullets(void)
    {
      for (int i=0;i<MAXBULLETS;i++)
      {
         if (BulletArray[i].active==1)
         {
              double sx=BulletArray[i].ScreenPos.x;
              double sy=BulletArray[i].ScreenPos.y;
              double spd=BulletArray[i].Speed;
              double vx=BulletArray[i].Velocity.x;
              double vy=BulletArray[i].Velocity.y;
              sx+=(vx*spd);
              sy+=(vy*spd);
              BulletArray[i].ScreenPos.x=sx;
              BulletArray[i].ScreenPos.y=sy;
          }
       }
    }

    The problem with this method is that you basically have x number of 'slots' available to use. You must check each 'slot' to make sure that it is active or being used. If it is then you must update it. So if you have 10 active bullets, you will still check all 50 slots which wastes cycles. There are ways to make this faster but you still have x number of slots available no matter what.

    A linked list will allow you to have an infinite amount of bullets (theoretically) and will not require you to parse through objects that are not active. This is because if the bullet is in the list, it is active.

    There are several examples of linked lists here on the board. I leave this as an exercise for you to figure out its implementation in your particular case. If you have problems...post them here and I'll help you through them. But here is a start:


    Code:
    typedef int BOOL;
    
    struct point2D
    {
       double x;
       double y;
    };
    
    struct Bullet
    {
       Bullet *Next;
       point2D ScreenPos;
       point2D Velocity;
       double Speed;
       BOOL Active;
    };
    
    ...
    Bullet *Start;
    Bullet *End;
    Bullet **BulletList;
    
    void AddBullet(Bullet *newBullet);
    void Remove(Bullet *remBullet);
    Or you can also use the linked list structures available in the STL if you don't want to re-invent the wheel.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Generally you would have a bullet array. Would would be very much un dynamic. As the possibility for unlimmited bullets is a flawed one considering draw time,and processor ability. Basicly you use some math to figure out how many bullets you need in the array (or you can be unmathamatical and just pick a value).

    If it takes a bullet 10 seconds to get from my gun barrel to the end of the screen (where it is no longer considered for collision), and I can fire once every 10 seconds, I should have a bullet array of 10, maybe 11 for a cushion.

    Every time you fire, set the next bullet of your array to the correct velocity, and fire. then load the next bullet, etc.


    hope that helps.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Like this:


    Code:
    int BulletSlot=-1;
    if (keys[Fire_Bullet])
    {
       for (int i=0;i<MAXBULLETS;i++)
       {
          if (BulletArray[i].active=0)
          {
             BulletSlot=i;
             break;
          }
        }
        if (BulletSlot==-1)
        {
           //No free bullet slots in array - full - can't fire
           //Should never happen...but just in case
        }
        else
        {
            BulletArray[i].Velocity.x=cos(DEGTORAD(Player.angle));
            BulletArray[i].Velocity.y=sin(DEGTORAD(Player.angle));
            BulletArray[i].Speed=Bullet_Speed;
            BulletArray[i].ScreenPos.x=Player.x+Some_Offset_X;
            BulletArray[i].ScreenPos.y=Player.y+Some_Offset_Y;
            BulletArray[i].Active=1;
            PlaySound("firebullet.wav");   //or something like this
         }
    }
    Or something similar. Some_Offset_X and Some_Offset_Y are so that it looks as though your bullet is coming from a gun on the player's ship - otherwise it would come from the dead center of the player's ship which looks....well....stupid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. OpenGL shooting game
    By sl4nted in forum Game Programming
    Replies: 6
    Last Post: 12-01-2006, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21