Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shooting Bullets..

Started by hammerstein_02 Jan 26, 2003 at 11:32 AM 12 replies 550+ views
Original Post
hammerstein_02
hammerstein_02
We are having trouble planning out our bullet system on our shootemup. Basically we have a base class cObject. From cObject we have cEnemy and cPlayer. Now we need to make cBullet, because all graphical objects are updated from within cObjectManager we obviously would like bullets to get added and updated along with all the other onscreen objects. cObjectManager ---- cObject --------- / \ \ cPlayer cEnemy cBullet Now, we know we are going to have to know who owns the object. Now our questions / concerns are: Can we efficiently generate hundreds if not thousands of bullets? How do we decide the speed and direction of a bullet? What would be the best method of tracking collisions using our design? Thanks for your time =*= If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore =*=
ctoan
ctoan
It is virtually imposible to TRACK hundreds, even more so THOUSANDS, of bullets in real time. The trick is to create and destroy the object in a single breath. You don''t need to know where the bullet is over it''s .1 second lifetime!

The solution is, then, to use a ray tracing technique instead. When the entity (player, enemy, etc) fires a bullet, trace the bullets trajectory (by extending a vector) and find the intersection (you can find papers on "ray tracing" all over the internet). This way, each bullet consumes NO extra memory, and consumes minimal CPU time.

It is true, however, that ray tracing consumes A LOT of time, though that is only true when you trace every pixel in a 640 x 480 screen (which you are CLEARLY not doing! )

Hope this helps,
Chris Pergrossi
Chris Pergrossi< ctoan >My Realm
hammerstein_02
hammerstein_02
this is in 2d btw, using directdraw.. just incase it matters.

=*=
If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore

=*=
ToohrVyk
ToohrVyk
ctoan : my 3d game engine handles ~2048 objects (asteroids) on-screen with point physics, and reallllly basic collision detection, smoothly on older computers.

Also, most "bullets" in shoot''em up games are rockets/missiles/plasma that fly around at 20 pixels/frame or so, so using instant-hit lines isn''t a good solution. Especially if you want the player to avoid them.

hammerstein: you can use a lot of objects if you want to. the hard thing is creating / destroying them, since new/delete are quite long. A solution would be to allocate a whole load of them at loading time, then add''em to the manager when they are fired and remove them without destroying them when they are done.

For collision, I may advise a quadtree-like system, inside which you would only add enemies and the player. Bullets move too fast for it to be appropriate. Then check each bullet against the quadtree.

For movement, store a position, orientation and speed vector inside the object, and simply add speed to position each frame.

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more
Alexandre Brien
Alexandre Brien
The way I did it was to create a bullet object every time someone fires. I didnt notice any slowdowns, but then again I didnt have thousands of bullets at the same time...

I had a CProjectile base class, and then I could just derive CBullet, CRocket, CPipeBomb etc... from it, each with their own model (none for bullet), velocity, life spawn and so on.


But uhh if you have a map with 100 player firing at each other with machine guns I guess it could be a problem to keep track of all the bullets...
In Construction : http://www.gdev.org
ToohrVyk
ToohrVyk
A test app here

approximately 2044 asteroids with various sizes at the very end... all of them being rendered. Alt+F4 to quit.

The above link will become obsolete soon (around 1 feb 2003).

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more
RatLord
RatLord
There is no reason that you can have hundreds of bullets updating all the time as Was mentioned above with regard to asteroids.

I wouldnt derive your bullets from your objects though. Make a seperate system for your bullets - much like a particle system.

Give each one

Vector position;
Vector velocity;
float lifeSpan;
float age;
int damage;

if u need to know who fired it you could stick in a pointer to its parent as well.

loop through your bullet list each frame adding velocity onto position etc. As for collision a simple check to see if their velocity takes them within an objects radius should suffice.

When their age reaches life span delete them. This shouldnt give you any speed problems unless your firing thousands or your on a REALLY old machine. If this is the case - maybe you could limit the total number of bullets and use an array of bullets instead.
ToohrVyk
ToohrVyk
I''d be using int Type instead of int Damage. That way, you can simply use float BulletTypes[Type].Damage, along with BulletTypes[Type].BitmapPicture. And it can even get better with animation and such (using modulus on the age...)

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more
starstriker1
starstriker1
In my game, I use a large array of about 1000 elements for my bullets. Whenever a bullet is fired, I add to "bulletcount" and fill in the new data for my bullet. I have a loop within my game loop that processes all my bullets (the loop goes around as many times as bulletcount''s value). To kill a bullet... I decrement the bulletcount and copy all the bullet data on top down.

Yeah, I know. Not the most efficient, but I find it easy to work with.
skjinedmjeet
skjinedmjeet
Remember: Max Payne simulated all of it's bullets, which was why when going into bullet time you can see each projectile moving.

If you try and snipe a guy, there will be a fraction of a second that elapses before you actually hit the guy.

-=skjin=-


[edited by - skjinedmjeet on January 29, 2003 4:04:26 AM]
Trienco
Trienco
quote:
Original post by starstriker1
I decrement the bulletcount and copy all the bullet data on top down.


if they dont need to be sorted: wouldnt it be easier to just copy the last bullet to fill the gap and leave the rest alone?

f@dzhttp://festini.device-zero.de
thona
thona
quote:
Original post by Useless Hacker
Or use a linked list.


Slower.

If you need dynamic size, a list of arrays is perfect - where you can add/drop arrays of lets say 1000 items each.

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)
RegardsThomas TomiczekTHONA Consulting Ltd.(Microsoft MVP C#/.NET)
Dreddnafious Maelstrom
Dreddnafious Maelstrom
Second Thona on that. Although in reality (meaning once you are trying to squeek out a higher cycle rate) it would probably be best (assuming speed is more important than memory) to just fake a dynamic array by preloading a high number of objects and just building a management structure to recycle your arrays.
"Let Us Now Try Liberty"-- Frederick Bastiat

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.