Jump to content

  • Log In with Google      Sign In   
  • Create Account

bullets, lasers, rockets

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:


Posted 22 September 1999 - 01:33 PM

Hello there...

I am currently writing a top down-ish shooter in C++. I've got most of the graphics routines done, but I am wondering how to implement a shooting routine.

Some thoughts were:

1. When the user presses [shoot], a new bullet structure is created and inserted into an array, bullets[number of bullets]. Then during the rest of the loop, this array is calculated and bullets are moved, etc.

The problem is that when bullets "die" (hit a wall, go off the screen), how do I remove them from the array, and create a "free space" to put the next bullet in?

And how would you cope with Unlimited Bullets - i.e. being able to shoot as quickly and as much as you want - NOT just having 8 shots on the screen, and you have to wait for these shots to "die" before you can shoot again.

This is the only practical method I can think of, if you have any suggestions, please help me out here!

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:


Posted 20 September 1999 - 04:22 PM

I first tried a linked list. This worked great, that is until I had a whole bunch of memory allocation and deleting problems and realized that I could accomplish the same thing with a static array and a lot less work. The thing that you have to realize is that you _don't_ need unlimited bullets, as that would make no sense. My bullet structure is something like 30-40 bytes in size, which means that I could hold roughly 25,000 bullets in one MB of RAM. As for the removing of bullets from the array, just make it so that when the player shoots, a loop executes that checks for the first empty spot in the array and inserts the bullet there. Make sure this loop is an infinite loop that breaks on inserting the bullet, cause if it's only a for loop, it may not run long enough to find an empty spot. Another thing is to make all the bullet elements in your bullet array pointers, for the obvious reason of creation and deleting.

You can check out the game I am working on at : http://members.home.net/chris-man/

Hope this helps.

#3   Members   -  Reputation: 157


Posted 20 September 1999 - 05:04 PM

Umm... i disagree

I have used linked lists to hold it and have found that to work quite well (and I think wasting a whole mb of memory for holding bullets is a great big waste... but maybe it's just me.)

The reason i don't like the array idea is when you have a lot of enemies and a lot of shots alive. Say you have 10 enemies... each has shot 5 times. And you yourself have shot 3 times yourself. In one iteration, 6 of your enemies and yourself decide to shoot... now think of all the searching that goes on just to find an empty slot!!! I used to do the array idea, but i found it to be just too slow.

With linked list i could just allocate and dealloc as I please. It may cost more time with fewer shots on screen, but if there's only a few shots you won't notice... as more shots/enemies appear, the list starts to go faster as it doesn't have to search. Also, it saves memory.

Just my thoughts...

and g'luck with the game.

#4   Members   -  Reputation: 122


Posted 20 September 1999 - 09:06 PM

Hm, you think that alloc function doesn't search for free space in memory pool? Yes, it do, and it much slower than searching for something you know to search for. I can suggest only one thing:
First, start with allocation memory for, say, 10 bullets. Then, if you want to add new bullet, check if there is enought free space (num_active_bullets


#5   Members   -  Reputation: 127


Posted 20 September 1999 - 10:04 PM

Well if you alloc a pool of memory for your program at start up, of the maxium free memory and lock it.... Youll pull of what quake does basicly. Then you can just alloc,malloc from that pool. Just watch for not to much memory fragmentation.

#6   Members   -  Reputation: 122


Posted 21 September 1999 - 12:08 AM

Or, as a completely different solution, do bullets like they have in Death Rally- you don't even see them because as soon as you fire them they hit. No array needed.



#7 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:


Posted 21 September 1999 - 01:42 AM

Thanks a heap everyone. Although I didn't understand about some of that stuff, I now know about all these wonderful things I didn't know about earlier (ie linked lists).

Thanks LackOfKnack - most weapons hit automatically, but for a rocket launcher, I though it would be cool to have a rocket actually on the screen.

Anyway, i'd just like to say thanks...

#8 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:


Posted 21 September 1999 - 04:29 AM

Maybe you can help me then Queasy, you seem to know about linked lists. I agree that they are faster for when you have more bullets, and my linked list worked great. That is, it worked great on my computer! As soon as I tried it on another computer, it would crash after some number of bullets. This number was very odd too. First it would crash after the 22nd bullet fired, no matter how many bullets were on screen, or when it was fired. I changed some memory allocation code and then it crashed every time after the 36th bullet. This didn't happen on my computer, even though it is a really old and crappy computer. Do you have any idea of why this could be happening?

On another note, I noticed that when I changed to the array, the game did not slow down noticeably when I had more bullets on screen (Well, it did, but that was because of all of the explosions happening; this slowdown occurred with the linked list as well). But you are right, I certainly do not like the idea of having all that memory used for bullets when it could be as clean as a linked list.


#9   Members   -  Reputation: 128


Posted 21 September 1999 - 05:54 AM

Just thought I'd chime in because I didn't see this mentioned...

There's no need to search for an empty slot if you're doing array bullets. Make a static integer, say, curbullet, set it to 0 at the start of the program. Whenever you need a new bullet, put it in bulletarray[curbullet], then increment curbullet. if curbullet is greater than the size of the array, set it back to zero.

I've found that this method usually works well, with one catch: you have to calc every bullet in the array, so it's best to keep the arrays small (I think I set mine to like 50 or 100, tops). The good news is that this helps to avoid slowdown, since you're doing the same amount of work regardless of how many bullets are actually on the screen; the bad news is that when a new bullet is created, it has the potential to overwrite one that's still active... but with 50 bullets on the screen, no-one's going to notice.

Mason McCuskey
Spin Studios

#10 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:


Posted 21 September 1999 - 08:43 AM

The list method is the generic solution.
Works well, and scaleable for as many bullets as you have memory for. However, if your concerned about effienciy then the array method is proably acceptable. However as you noted you'll have to search the array for an open bullet slot, to add new bullets, which is a performance hit but given a reasonable array size, proably negilable. A simple way of seeing which slot is open would be to simulate a link list within the array. Where each slot holds information about the next open slot, and the previous slot its linked too. If your array is small, say under 255 elements thats just 2 bytes of extra data per slot. You manage it like a linked list but since the elements are already allocated there wont be a hit in terms of allocation/deallocation, which for c++ is considerable with all the constructors and destructors. Ya Good Luck!


#11   Members   -  Reputation: 157


Posted 21 September 1999 - 10:25 AM


Yes, i've had your problem before, and the solution was pretty stupid

Heh, what was happening to me (and i'm not sure about you) was that my ptr to the head of the list was not updated to see the next element. So basically... i guess all i can say is be careful when you allocate the next element, and make sure it's linked properly.

If you want (and if i have time ) i can look at that part of the source for you...

And about the speed issue... i have found from experience that it's faster without the arrays (way back in the dos days). But if you (guys) say there's no speed difference when you tried it then i guess i'm wrong

#12 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:


Posted 21 September 1999 - 11:37 AM

OK thanks for the help then. I believe that I shall stay with the array, simply because it is working great at this time and I also do not expect to ever exceed 100 weapons on screen at the time. Oh, and sorry about the double posts. They were made from my school, which is running crappy hardware, and I kinda clicked "post" twice

#13 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:


Posted 22 September 1999 - 07:52 AM


Email me and I'll give you my routine for bullets and other on-screen items(minus the main game sprites)


#14   Members   -  Reputation: 270


Posted 22 September 1999 - 01:33 PM

Another solution is a combination of the linked list and arrays...a linked list of arrays.

Have the array structure so that it contains the "normal" number of bullets you expect to exist at one time. If you exceed that number, you create a new array of bullets and link it to the existing one.

You could then add a "count" to the array so that you know if the array is full or empty. Empty arrays could be deallocated or just left so that you don't have to worry about allocating them again.

Say you estimate that you will need 25 bullets on average. Then you create a structure that includes a counter and space for 25 bullets as well as a "next" pointer. Scanning 25 slots is seldom going to be slow, so hunting an empty or "dead" slot isn't really a problem. You know if the structure is full, if it is you skip to the next batch of bullets, allocating it if necessary.

My take on the setup.

Samu Games

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.