This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin
In my last article, I created a firing mechanism that takes an assigned amount of projectiles, and instantiates them evenly around 360 degrees of rotation. My objective for this article is to really take it for a test drive!
More on how the math works here. https://levelup.gitconnected.com/creating-new-enemy-types-the-porcupine-blowfish-part-3-firing-projectiles-in-perfect-rotation-e1a1755093b4
While the above code works just fine, I was having problems with the output on my shorter and more condensed version. I was losing a percentage of the 360 degrees of rotation and there was an obvious gap at the end of the firing circle. After my wife crunched some advanced numbers to test my algorithm (thank you!!!), she assured me that it works, so something must be happening on my end in Visual Studio.
The algorithm divides integer values (whole numbers), so it’s very important that I cast those as float values to keep the fraction numbers after the decimal point. My rotation variable is a float value, and I need those values to match. I thought I was properly casting my values as a float, but upon further inspection…I typed float on the wrong side of the parentheses. User error! In the end, the remainder somewhere was getting rounded to the nearest whole number and I was losing the needed data to assure proper rotation.
After properly casting my float values to retain the needed decimal value to get the correct rotation, I found that the condensed algorithm works perfectly!
Here is the final algorithm with descriptive variable names. Feel free to try it out in your game and let me know how it works!
I was not content to stop there, so I decided to tinker and really see what this thing can do! The more projectiles I feed it, the smaller the rotation angles will be. I had never tested anything more than 60, so I decided to see if the conversion holds up while instantiating an amount of projectiles that is greater than my maximum degrees of rotation (360).
As the saying goes, “go big or go home”. To start out, I am going to fire a random amount of projectiles between 431 and 753, whenever I press the space key.
The lag before the projectiles instantiate is not a video capture error. That’s my laptop hanging momentarily while the for loop iterates through hundreds of individual projectile rotations.
Ok, let’s back off it a little and see where the lag drops.
Let’s try between 200 and 300 projectiles. It felt smooth enough that I decided to really let that Narwhal have it!
WARNING: The next two clips are a bit flashy, and I’m not sure if that could potentially trigger any seizures or other adverse effects.
That still felt like a little lag when I was going trigger happy. Let’s see how a range of 100–200 looks.
This one felt smooth with no hang-ups crunching numbers.
Let’s dial it back to my the original settings that game me a problem.
Looks good to me! Dangerous, but solid rotation.
I will use these values for the actual game. This way, no more than 8 projectiles will fire. These values could also be serialized into variables that a designer could adjust for difficulty levels.
Here is the more reasonable attack.
Now that my stress test is complete, I want to test the maximum degrees of rotation. So far I have only been using 360 as a value. What happens if I change that value to 180 for instance? Let’s find out. I introduce a local float variable called maxDegreesOfRotation and assign it a value of 180. I then insert the new variable into the equation where 360 used to be.
As you can see, it does work, but it looks asymmetrical. The final projectile isn’t moving perfectly down, so I need an extra step in the algorithm to make this 180 value symmetrical.
What if you want to rotate this around to fire in another direction? Just add a value to the end of the algorithm. Here I added 90 degrees to each projectile.
Now the whole wave of projectiles fire down rather than to the side!
Well, I am more than content to move on to the next step and finish up this new enemy type. Thanks for reading!
Refining and Stress Testing my Firing Algorithm was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin
Jared Amlin | Sciencx (2021-09-03T14:11:55+00:00) Refining and Stress Testing my Firing Algorithm. Retrieved from https://www.scien.cx/2021/09/03/refining-and-stress-testing-my-firing-algorithm/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.