I came across this question when I was designing a video game in C#.
If we consider games such as Battlefield or Call of Duty, hundreds or even thousands of bullets are flying at the same time. Events are triggered constantly, and from what I know, this sucks a lot of processing power … or does it? I want to know how various game developers manage (2D and 3D) bullets and what the most efficient method for each is.
I read the question How are bullets simulated in video games? but it doesn't touch on how bullets work from a program design perspective.
I had a couple ideas, but each have their drawbacks:
Most efficient method I could think of (for 2D games):
Say I was to create a class called Bullet, and for however long the user holds down a button, every 0.01 seconds a Bullet object would be made. This Bullet has:
1 Velocity
2 Starting position of where it is being shot from
3 Sprite texture
4 An on-hit effect
Since the bullet would be its own class, it could manage the drawing, moving, and action listeners itself.
Wouldn't it be hard on the processor to process thousands of these objects being instantiated, then destroyed (when on-hit effect is triggered)? RAM space?
Efficient method for 3D games - Another thought I had was:
Lets say I create a weapon class. This weapon has various features, some of which:
1 Detect where the weapon is aiming, and determine if it's looking at a target
2 Trigger an animation of the gun shooting
3 Has a doDamage() method that indicates something to subtract health from whatever the gun is pointed at
4 Notifies a bullet animation class when button pressed
I could then create a static class, say BulletAnimation, that could get notification from where the gun that triggered it is, where that gun is pointed at (for the bullet destination), and information on an appropriate sprite and velocity to use for the bullet. This class then draws sprites (on a new thread maybe, idk) based on both positions and desired sprite, to simulate a bullet being fired from a gun.
The latter seems much harder to code, and wouldn't it take lots of processing power to constantly call the static to do this for thousands of bullets at a time? Getting constant updates on both starting and ending positions would be hard as well.
My question is, what is the most efficient way game creators do it? Does this method change from 2D to 3D games?
pew-pew-pew
technology :) \$\endgroup\$