2D shooting game (Newbie questions)
Hi all,
I would like to ask which of the following ways could I improve the performance of my shooting game, so that it can run smoothly
1. If I have several types of bullets, each type of bullets have
different size, speed and acceleration (but they may have same
collision detection by checking the bounding box), should I
a) Only create
a single class, and use enum to identify the bullet type (but it needs
a few memory to store the bullet type)
OR b) Create an abstract class of the
bullet, and then create several subclass for different bullet types
2. Assume there are thousands of bullets moving in each frame, hundreds
of bullets are created by enemies, and also hundreds of bullets are
moving out of the screen. How do I handle and update all these bullets?
a) Use a
simple array (e.g. array size of 10000) to store the bullets. When the
bullet is out of the screen, set a flag to false so that it will not
process any more. If a bullet is created, check whether there are empty
slots (in the array) available to create the bullet
OR b) Use a generic list to store the
bullets. If a bullet is out of screen, remove it. If a bullet is
created, add to the list
3. Each bullets (also the flight) have size. When I draw the bullet, I need to know the location of the bullet.
a) Store
the top left coner of the bullet, so that when I draw the bullet, I can
directly draw the texture at suitable location. But when I need to
detect collision, I may need extra calculations to find the center.
OR b) Store the center of the bullet,
so I can detect collsion in the simplest way. But I need to find the
top left corner when I draw the texture
Thank you very much for your help.
[1740 byte] By [
Summoner] at [2007-12-25]
1) u can do both, i would use A, cause the class storing the bullet information should be the same for each bullet, only difference is some of the parameters (parameters each bullet type should have) and a different model to render. So u can set the parameters of the bullet into 1 class and a reference to the model class.
2) method A is bad, first of all u will have to loop trough the entire array each time to see if the flag is true or false, also when u create a bullet u gone loop trough the entire array again. If ure array size is 1000 this will decrease the performance a lot. It depends a little on what language u use. Cause u are talking about generic list i assume u are using managed code (C# or VB .NET 2.0) so u can just use the list classes. Maybe the generic list class is the best option in if u are using method A for question 1. Just make a list of type BulletClass to store all bullets, if a player shoots, create the bullet class instance and add it to the list. if its out of the screen remove it from the list.
When using native code u can choose to use the template libraries (like <vector> collection template) or create your own collection class where u manually manage the memory and shift memory once a bullet is removed.
3) Allways store the center of an object, i don't see why u would need the top left corner of the drawn object?
I'm don't know how u create / draw your sprites to render the bullet but i think this would be the best way. When loading the lever, create a sprites for each bullet, make sure the center of the sprite is 0, 0, 0 (so create it around the x0,y0 position) when drawing a bullet instance use the matrices to position the bullet sprite using the bullet coordinates stored in your bullet class.