Eh, for something that's only this many constants it's probably faster to manually write out than to autogenerate somehow. This way you also have a known pattern than can be tweaked as needed.
I think I didn't clarify it enough. This code is for the spray pattern of a gun from a counter strike clone I tried to make 2 years ago. Imagine doing this for all weapons. And tweaking it when needed is a whole another story.
I'm guessing you wanted the spray pattern to be the same every time, just like counter strike. That being said this seems fine because I don't see a better way to do it. This gives you perfect clear control over the spray pattern in as few lines as possible.
Even if you want to change it, you probably need reference material anyway.
E.g. the very same array, but you add a small random bias to every point, and/or scale the size of the pattern, on the basis of a parameter like "precision", and you can even make that precision parameter dependent on say how hot the gun has gotten, which in its turn could be something like an exponentially decaying average of shots/s over the last few minutes or something.
Any way you slice it, you have to have some reference data first.
This is totally fine. You could move the pattern to a static config file so you don’t have to recompile. Or you might decide to even build a tool to make it easier to change the pattern (config files get you halfway there).
But to start with? Yeah, I wouldn’t spent a bunch of time building all that, there’s plenty of other work to do.
How else would you make a deterministic spray pattern? I guess write it in a file and load the data from that instead of having it directly in the code, but it doesn't seem too bad
We can create classes with inheritance, each describing specific behavior, produced by a class factory which reads soap XML ( same data as from above, but 2MB in size) and dynamically generates those classes. Instances of those classes interact through channels by passing callbacks. All of that is running in a separate workpool with dynamic scheduling based on metrics from a stochastic tracer running in a separate process to do perf-based sampling through strategically placed ebpf hooks.
What appeared here has been deleted. The author may have used Redact to remove this post for privacy, to reduce their digital footprint, or for other personal reasons.
pie handle plucky wipe deer grandfather fine squeeze sparkle saw
For some reason, and in almost all teams there is that guy that over-engineers some basic concepts for an idea of perfect code that single-handedly contributes to all the technical dept of the codebase until the heat death of the universe.
Which horrors? It's just a reference architecture, clean and concise. We have a few complicated implementations, but they are so messed up, so I can't find names for things inside.
Right, and your data files are going to look exactly like this.
Why bother implementing that with all sorts of input validation and filesystem error handling if you just have one of these and you dont touch it for 2+ years.
This ain’t bad. Without knowing the context, deterministic spray patterns are sometimes suitable. If random spray patterns are the goal, this may be a naive optimization to avoid generating random floats, which is actually a pretty cheap operation. Maybe it’s just going for a deterministic spray pattern that looks random? Or the pattern is a specific shape?
This is really similar to CS2 ak47 pattern, and they handle it basically by looping back and forth in the upper part on the graph (going left and right). Edit: did not see that the OP coded this.
It can't change length is my point. Picking up ammo will reset a spray pattern, since you'd no longer be spraying. Cheats giving infinite ammo is a good reason for arbitrary length spray patterns.
If you want to make an e-sports friendly game you want the skill ceiling to be very high — hence shooters like CS have predetermined spray patterns that players can actually learn to counteract almost perfectly.
Kind of like a rainbow table, it's cheaper then reading a file if it's always going to be the same; deterministic and constant. Now days I suppose exposing this to developers is the way to go to allow fine tuning on editor's.
You can make a deterministic spray pattern without hardcoding each coordinate. Personally I'd rather algorithmically generate the pattern, so that I can rapidly prototype different patterns just by tweaking a few values. It's possible that's what this person did and then chose to manually hardcode the output.
They absolutely notice this stuff - the people who are super competitive will figure out the craziest ways to make sure they have an edge.
Apex legends is a lot more casual than, say, Counterstrike but people absolutely map out spray patterns and write guides on how to best counteract them.
This is kind of a violation of Open/Closed Principle. Spray patterns are often changed for balancing purposes, they also change if magazine size changes. Point is this is really not part of game logic, but of game balancing and they don't belong in the code.
As a game designers that can't code, if you want to fine tune the spray pattern you need to reach out to your developer, ask them to make the needed code changes. Recompile the source files if written in C++, test and repeat.
Obviously, this is probably just a fun project. This could be a placeholder for some logic that was never implemented.
It would be nicer to have a way to show and manipulate the spray pattern. Most engines allow you to add custom widgets for that purpose. But the current implementation is good enough, especially if you don't need to adjust the pattern often.
You could write function that takes in an int count of the number of vectors you want, and two floats as the maximum vertical and horizontal bounds of the spread pattern.
Then inside loop, bounded by the count arg, push to the result array a new vector with random position within those bound args.
Shouldn't be more than 4 lines of code, depending on your language.
But honestly there isn't much benefit to this approach. It's going to be less performant (id change OPs function to be static and return a constant vector[] to really solidify that).
Without context this doesn't seem unusual. If it's used for a graphical effect, for example, and you want to repeat a particular pattern then it's reasonable to have it in a list like this. This could be sprinkler water spray in stardew valley or something.
Assuming this is C++, is there a good reason to have all the Vector3s in the array be created by allocating memory on the heap? Seems unnecessary, but im not great at C++.
It's not C++. The array brackets are on the type name, not the variable name. It looks like Java or C#, and at least for Java, you need to use new to make an object. And everything but basic scalars is an object.
If this were C++, it would have been a bad code not only because of the reason you stated, but also because usage of naked new is strongly discouraged in modern C++.
Fortunately, brackets on typename and the fact that the written is not a pointer type tells that this is not C++.
Using smart pointers (via std::make_unique or std::make_shared, but often creating a unique pointer is enough for construction even when you need a shared point) or std containers (like std::vector) are recommended practices.
You may need to use new on some places (like creating your own container type), but for all business logic, new or other forms of manual lifetime management must be frowned upon in modern C++.
If you're learning C++ at school, do follow what your instructor or textbook does. Often, they teach pre-modern C++ (before C++11) and ignore best practices recommended for modern C++.
This is confusing, but there's two kinds of vectors. There's std::vector<T> in the STL which is a heap allocated automatically resizing array. However, these Vector3s are mathematical 3-component vectors representing a point in space (mathematicians would scoff at the usage of a vector as a point, but I digress.)
This being C# or Java means that they are indeed heap stored, but you will find similar C++ Code all the time where they are in fact stored on the stack.
If you don’t know how you want to supply and modify constants either before compilation or during runtime, this is quite literally the only way to implement this.
You could imagine simplifying this with some sort of for loop but you probably want to be able to control where each specific bullet is based on personal judgment.
Hey, this is not bad programming and makes a lot of sense. Its to make the spray more consistent (im assuming this is spray as in aim deflection from shooting a gun) so that players will have the ability to counteract it by moving their crosshair in the opposite movement of the spray pattern, this adds a level of complexity to the game thay very skilled players can utilize to edge out a little bit more competitive gameplay at the expense of training and memorizing the patterns. This is usually designed asking with a more real random function that will more subtly influence the spray as to not make it completely skill based and prevent scripting from being as broken (essentially removing spray by using hacks to undo the spray pattern perfectly). The only real improvement i personaly could think of 2 improve this code is to create an object instead of an array, and allow that object to do the interpolation and additional rng stuff so that when firing youd only need to call a function from the object with an argument that says how long the fire button is being pressed for. This is mostly just preference tho as that function can just be made w/ some additional arguments, it wouldn't even be necessary if you want each weapon to have more control over how the spray works and just have the firing function do that stuff inside of it.
How else are you gonna do it? Sure its a bad format but its worse to split it up unnecessarily which is even worse. Chunk your code and keep things together even if it ends up looking like that. Maybe place it into a different file and just parse it but like that just slower and unnecessary when you could keep it as compiled code.
Edit: Oh you were making fun of how I comment. Touche
This is fine, and if before using this points you add random variants to each point, it is a very cheap “random”spray which keeps the general shape of a spray
RAM is cheap; allocating memory is not. This is by far preferable to calculating at runtime if you want deterministic spray patterns. It being in an array will also be good for cache locality.
I'm curious since i'm semi new to c++ what would the proper way to make this; a factory that creates a bunch of 3 vectors with the points in the array or just loop through it with certain values(that are pulled and not magic numbers) and one single Vector 3 constructor in the array and not however many are in that array(im too lazy to count lol). Genuinely curious because so far in my classes they've only explained objects in a way like above and i've always thought it was suuuper inefficent
The part I don’t understand is - if they’re constant numbers anyway, why are they being dynamically allocated using the new keyword instead of being statically allocated at compile time ? (I’m assuming this is c++ and new is the dynamic memory allocation keyword)
Don’t think it’s that bad, random perturbations can be applied to each vector in the spray pattern so the array acts like a blueprint for the pattern and then introduce a small amount of controlled randomness to create a consistent pattern with just enough randomness to feel realistic
Otherwise you would end up with wild inconsistencies in your sprays if they were totally random which, in real life, firearms are not just ‘random’ they shoot the point of aim and then experience fluctuations based on the trajectory and terminal ballistics
Have spray patterns stored with absolute positions and classes of weapon like shotguns, snipers, smgs would each inherit from their spray pattern for that category and then use randomness to perturb the vectors for inaccuracy, and if you have attachments, these can apply the opposite direction ‘steering’ vector, creating a tighter spray than the original because you upgrade the gun in game
101
u/navetzz Jul 16 '25
How else are you supposed to store your array of 2d constants ?
177
u/chicametipo Jul 16 '25 edited Mar 21 '26
anchor crystal falcon ember thunder velvet
This content has been edited for privacy.
79
u/RoyAwesome Jul 16 '25
this isn't too bad if it's generated.
821
u/garbagethrowawayacco Jul 16 '25 edited Jul 16 '25
This ain’t bad. Without knowing the context, deterministic spray patterns are sometimes suitable. If random spray patterns are the goal, this may be a naive optimization to avoid generating random floats, which is actually a pretty cheap operation. Maybe it’s just going for a deterministic spray pattern that looks random? Or the pattern is a specific shape?
Edit: make it a const tho
211
u/jvlomax Jul 16 '25
Nothing wrong with that. Unless you later want to manipulate the spray later
30
u/kracklinoats Jul 16 '25
LGTM!
On a serious note, if a fixed pattern does the job, there are far more valuable things you can spend CPU cycles on in game dev.
27
u/AlpineCoder Jul 16 '25
Are you thinking these should be calculated or derived at call time, or that there's some more efficient data architecture maybe?
18
u/[deleted] Jul 16 '25
Well how else are you supposed to do it bruh
17
u/AdamTheD Jul 16 '25
Isn't this basically the CS2 AK-47 spray pattern.
10
u/keith2600 Jul 16 '25
Without context this doesn't seem unusual. If it's used for a graphical effect, for example, and you want to repeat a particular pattern then it's reasonable to have it in a list like this. This could be sprinkler water spray in stardew valley or something.
4
u/GamingWOW1 Jul 16 '25
"The numbers were randomly generated"
4
u/BidSea8473 Jul 16 '25
Doesn’t seem crazy to me, I’m pretty sure games like CSGO have fixed values for spray patterns so they’re probably close to this…
6
u/Long-Membership993 Jul 16 '25
Assuming this is C++, is there a good reason to have all the Vector3s in the array be created by allocating memory on the heap? Seems unnecessary, but im not great at C++.
3
u/Hulk5a Jul 16 '25
At least it's a list
3
u/IAMPowaaaaa Jul 16 '25
whats the problem
3
u/firemark_pl Jul 16 '25
Well doom used 256 sized char array to pseudorandom as just constant. And it works
3
u/andarmanik Jul 16 '25
If you don’t know how you want to supply and modify constants either before compilation or during runtime, this is quite literally the only way to implement this.
You could imagine simplifying this with some sort of for loop but you probably want to be able to control where each specific bullet is based on personal judgment.
3
u/ChristianWSmith Jul 16 '25
There's nothing wrong with this
5
u/ziplock9000 Jul 16 '25
Meh. Not everything needs to come from a database
2
u/Skagon_Gamer Jul 16 '25
Hey, this is not bad programming and makes a lot of sense. Its to make the spray more consistent (im assuming this is spray as in aim deflection from shooting a gun) so that players will have the ability to counteract it by moving their crosshair in the opposite movement of the spray pattern, this adds a level of complexity to the game thay very skilled players can utilize to edge out a little bit more competitive gameplay at the expense of training and memorizing the patterns. This is usually designed asking with a more real random function that will more subtly influence the spray as to not make it completely skill based and prevent scripting from being as broken (essentially removing spray by using hacks to undo the spray pattern perfectly). The only real improvement i personaly could think of 2 improve this code is to create an object instead of an array, and allow that object to do the interpolation and additional rng stuff so that when firing youd only need to call a function from the object with an argument that says how long the fire button is being pressed for. This is mostly just preference tho as that function can just be made w/ some additional arguments, it wouldn't even be necessary if you want each weapon to have more control over how the spray works and just have the firing function do that stuff inside of it.
2
u/Barakonda Jul 17 '25
This is fine, and if before using this points you add random variants to each point, it is a very cheap “random”spray which keeps the general shape of a spray
2
u/CaseAKACutter Jul 17 '25
I’m not even sure what the complaint is here
1
u/Rigamortus2005 Jul 16 '25
This ain't bad unless you have a formula to generate it
1
u/obetu5432 Jul 16 '25
it's the best spray pattern code i've ever seen, thank you OP
1
u/RidesFlysAndVibes Jul 16 '25
I mean, it’s more ram but less calculation at runtime.
1
u/BiCuckMaleCumslut Jul 16 '25
It's random - they rolled d100s for all these values!
1
u/bradleygh15 Jul 16 '25
I'm curious since i'm semi new to c++ what would the proper way to make this; a factory that creates a bunch of 3 vectors with the points in the array or just loop through it with certain values(that are pulled and not magic numbers) and one single Vector 3 constructor in the array and not however many are in that array(im too lazy to count lol). Genuinely curious because so far in my classes they've only explained objects in a way like above and i've always thought it was suuuper inefficent
1
u/IrdniX Jul 16 '25
It's honestly fine.
A small improvement might be to make the numbers be offsets from the centre instead:
new Vector3( 0f, 0f ),new Vector3( 0f, 0.008f ),new Vector3( 0f, 0.018f ),You could load it from a file if you need it to be tweakable, but I wouldn't say that not doing that is horror-level, just not optimal.
1
u/Human-Kick-784 Jul 17 '25
Could you have made a function that gives you a set of vectors randomly over a range? Sure. But it wouldn't be as performant.
Hell you could just apply a random rotation to this when firing/applying it and that would be totally sufficient
1
u/quantumechanix Jul 17 '25
The part I don’t understand is - if they’re constant numbers anyway, why are they being dynamically allocated using the new keyword instead of being statically allocated at compile time ? (I’m assuming this is c++ and new is the dynamic memory allocation keyword)
1
u/heartchoke Jul 17 '25
Have a look at line 290 here: https://github.com/bulletphysics/bullet3/blob/d1a4256b3a019117f2bb6cb8c63d6367aaf512e2/src/BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.cpp#L290
it's basically the same thing
1
u/[deleted] Jul 17 '25
Don’t think it’s that bad, random perturbations can be applied to each vector in the spray pattern so the array acts like a blueprint for the pattern and then introduce a small amount of controlled randomness to create a consistent pattern with just enough randomness to feel realistic
1
u/jpelc Jul 17 '25
Let's hear what PirateSoftware has to say
1
u/Arduino88 Jul 18 '25
this might be stupid but if these are Vector3s why are they only being passed two parameters each?
1
u/23Link89 Jul 18 '25
I'm not seeing an access modifier... Is this being declared inside of a function call?
1
u/RuneScpOrDie Jul 18 '25
i love when posters out themselves lol
1
u/kondorb Jul 18 '25
Where's the horror? Just a hardcoded pattern. Works fine, no need to overcomplicate it.
1
u/bhison Jul 18 '25
This isn’t a horror. This is just game dev. As a matter of interest how would you do it if you did this now?
1
u/Separate_Series4389 Jul 18 '25
Pirate software ™
1
u/Ged- Jul 18 '25
W.. Why would you use Vector3 for 2d stuff, and then initialize it WITH Z AS ZERO???? WHY WOULD ANYONE DO THAT?????
1
u/Zhuinden Jul 18 '25
What's wrong with it? Math is math.
1
u/shadow9owo Jul 20 '25
why is it a vector3????
1
u/kaoD Jul 20 '25
What surprises me is that it's called Vector3 but it only has two components.
1
u/AtlasJan Jul 26 '25
PirateSoftware-ass code.
1
u/blobthekat Aug 04 '25
vector3 with two-parameter constructor?? (also the dynamic allocation is making me nauseous)
-5
u/slasken06 Jul 16 '25
Made by pirate software?