Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BlackGhost

How would you program bullets?

This topic is 5721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am in the process of making my first game (other than tetris, pinball and pacman crap) and am generally satisfied with what I have accomplished so far. The game is a classic 2d platform, but I also wanted to give an arcade feel with fast action, which is mainly implemented with many characters and bullets in the screen. In general I have implemented the sprites and bullets as classes with the same base class, and update their positions on every loop cycle. Obviously, this is unavoidable for sprites where you can''t say how they will move (for example, the hero will read user input and a monster might need to decide to change direction) but ineffective for bullets, especially when you expect to have as many as 25-30 of them in the screen simoultaneously. I have thought about it but can''t seem to find a good alternate implementation for the bullets, that would take less cpu on every game cycle. What I need then is an idea on how this could be accomplished: the bullet updating its X and Y properties in a uniform way each cycle, without perhaps being implemented as a class and taking too much time to be processed. The language I am using is C# but I don''t think that''s very important in this question. I''m interested in the concept so opinions from C++ or other language''s developers would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
I'd have no problem what so ever with 30 bullet sprites being updated each frame. In the days of 386's this was a problem perhaps, but with the most lowend computers well over 400mhz, I don't think your cpu cycles are going to be teribbly hurt by proccessing bullets every frame.

[edited by - Illumini on April 18, 2003 4:44:17 PM]

Share this post


Link to post
Share on other sites
You could have a Movement class, defining position, and velocity. Then have a MovementManager, which is basically an array or list of Movement objects. Bullets would be a subclass of movement. When instantiated you''d add to the MovementManager. When destroyed you''d remove from the MovementManager.

Once per frame, you call MovementManager''s update function. This will just loop through each Movement object, and do x+=dx, y+=dy. No function call overhead, or anything per object, just a nice tight little loop.

Share this post


Link to post
Share on other sites
Illumini is right. With bullets having a constant velocity, all you will have to do for an update is a scaler-vector multiply and a vector addition. That will take almost no time, especially for any sane amount of bullets (< 1000).

The hard part would be deciding how to handle collision with the bullets. But since the search feature is back, I''d advise you to use that and search one of the other 200 threads on bullets in games.

Share this post


Link to post
Share on other sites
bullets travel very fast (2700 km/h)

so why dont u raycast the bullet direction
so this will be instantatnious movement

also this will make collision detection much easier because at a very high speed a bullet may pass by a whole object in one frame

[edited by - Ilici on April 18, 2003 5:10:40 PM]

Share this post


Link to post
Share on other sites
Thanks for your replies. Ilici, this is not a realistic game where we could approximately evaluate where the bullet goes instantly, but an arcade game where bullets are visible and travel at fairly low speeds. When too many bullets get on the screen (50 or 60) I do experience a substantial slowdown (10-15 fps less), and that's why I made this post.

Anyway, I think Illumini is right about processing speeds, so either C# and .NET is to blame, or something else happens (like the drawing functions in DirectDraw i'm using, I have an old graphics card). I just thought about this last one, and am going to test it sometime (see how FPS go with all the bullets, but without drawing them).

[edited by - blackghost on April 18, 2003 5:52:18 PM]

Share this post


Link to post
Share on other sites
Instead of creating new bullets each time a gun is fired, why not create a whole bunch to start with and reuse them? This would cut down on object creation, and only require you to allocate dead bullets and re-initialise them. A wrap-around array springs to mind.

Share this post


Link to post
Share on other sites
I think your problem is the way you render your bullets, if you loss lots of frames per seconds, that might be the rendering which is slow.

I would create a linked list, which should handle every bullet action and movement.

Share this post


Link to post
Share on other sites
I agree with fractionMan, you might get a speed hit from allocating new space for a bullet each time then destroying it then having to create a new one. The life of a bullet is usually pretty short so if your getting upwards of 50-60 bullets on the screen at once, you get a high turnover rate where your deleting objects you could recycle instead. Creating a static array of MAX_BULLETS and just using bullets from that list is one way of doing it or you could use a free-list of bullets where when the life span of a bullet is up, instead of getting deleted it gets moved to the free-list. When you need a new bullet you see if there are any in the free-list first that you can recycle and if not then create a new bullet. That way, at the end of the life of the game, you will never have created more than the top number of bullets you used at any given time and you are not limited to the size of a static array. I hope that made sense.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!