|
Title: Firing bullets? Similar to space invaders. Post by: BrandonQ on December 09, 2012, 11:31:44 PM I am interested to know everyone choice in your algorithms you use for a shooting mechanism, I am having trouble finding the best option. I am currently developing a small space invaders like game.
My current plan is to have the normal 'Player' class and from there I will have a 'Bullet' class in which will contain a static array which holds all the bullets currently on screen. From the 'Player' class I will have a 'Shoot()' method which will create a new bullet and send it to the stop of the screen if it otherwise has not collided with an instance of the 'Enemy' class. Thanks, Brandon. P.S. Just so you are aware, I am developing using the Slick2D framework built on Java and OpenGL. Title: Re: Firing bullets? Similar to space invaders. Post by: dr.crow on December 10, 2012, 03:57:32 AM I would prefer to use an ArrayList or a linked list to store the bullets, not a static array.
Title: Re: Firing bullets? Similar to space invaders. Post by: Xienen on December 10, 2012, 05:17:10 AM You could also have the Player class contain the array of bullets that it has shot and from there check for collisions with enemies. You could then have the enemy bullets stored in each enemy's class and have them just check for collisions with the player. Obviously the alternative would be to have a reference back to the shooter in each bullet instance, in which case a static ArrayList in the Bullet class would work just as well(other than needing to check whether the shooter is the Player or Enemy every time you check the bullet's collision, I guess).
Title: Re: Firing bullets? Similar to space invaders. Post by: DelishusCake on December 10, 2012, 09:07:32 AM I would use an ObjectPool (http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/objectpool.html) to create a bunch of instances of the bullet object at the beginning of the game. As the player shoots, the object pool gets the first available bullet and fires it. The bullet then becomes "available" when it hits an enemy or goes off screen.
This way, the game isn't constantly creating new bullets, nor is it running collision detection on bullets that aren't actually being used. Title: Re: Firing bullets? Similar to space invaders. Post by: vids on December 10, 2012, 08:59:10 PM Sometimes it's a bad idea to go the OOP way with everything. If we're talking about a lot of bullets here you might just be better off holding the bullets outside the entities and inside the 'World' class if there is one.
Title: Re: Firing bullets? Similar to space invaders. Post by: Xienen on December 11, 2012, 05:00:09 AM That's true, but that would really be the same thing as having a static ArrayList or ObjectPool within the Bullet class. Hrmm, on second thought, I guess that's not true, because if you have it within the World or something similar, you'd probably have better access to the Enemy array to check for collision. Depending on your code layout, vids may have the best suggestion here; it should be combined with DelishusCake's suggestion of creating an ObjectPool, ideally.
Title: Re: Firing bullets? Similar to space invaders. Post by: kinglake on December 11, 2012, 05:25:43 AM I would use an ObjectPool (http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/objectpool.html) to create a bunch of instances of the bullet object at the beginning of the game. As the player shoots, the object pool gets the first available bullet and fires it. The bullet then becomes "available" when it hits an enemy or goes off screen. This way, the game isn't constantly creating new bullets, nor is it running collision detection on bullets that aren't actually being used. Same way that i do it - best way i've found so far. Title: Re: Firing bullets? Similar to space invaders. Post by: s_l_m on December 11, 2012, 06:30:49 AM I have done it with a static array of a bullet struct (in java you would have to use an array of objects), where each bullet has a Boolean active variable so you don't run collision detection on each one. It takes slightly more memory but it should run faster than doing it with linked lists and is probably the simplest way to do this. The nice thing about doing it with linked lists though is that you will only use as much memory as you need, and you can have as many bullets as you have memory for them (don't expect much in terms of frame rate at that point, but it is theoretically possible)
I don't think I have it around anymore, but I wrote a simple bullet hell engine just to play around with a long time ago. If I can find it I could send it to you as an example of what not to do :) I think I may have formatted the drive it was on though edit: I should have mentioned, with linked lists you can have more than one list (or a branching list), and move objects between the lists depending on where they are on the screen, this way you could do a tree structure to speed up collision detection (if speed ever becomes an issue). This is only really an advantage if you have a crazy amount of bullets/ are targeting a weak platform but it is worth looking into because it would be difficult/wasteful to do such a tree structure with a static array. Title: Re: Firing bullets? Similar to space invaders. Post by: vids on December 11, 2012, 09:43:39 AM Data oriented design. I'd think of it as a particle system rather than a set of separate addressable units/entities.
I'd even drop the Bullet structure altogether if you're doing bullet hell, manage separate static arrays (DelishusCake style) like, Code: bulletsAlive[];bulletsVel[];bulletsPos[]; I think it's better to do the collision detection in one place rather than execute a call for every bullet, also faster to loop when data is not fragmented by structures (cache misses). |