# problem with laser gun!

• 11-24-2003
actionbasti
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

action
• 11-24-2003
VirtualAce
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 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.
• 11-24-2003
Jeremy G
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.
• 11-24-2003
VirtualAce
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
{