Yes, that looks about right. So you have an angle for the rocket, and now you want to add a bit of randomness to the angle to simulate spread. Ok, let’s start:
local angle = math.atan2(self.dir.y, self.dir.x)
The angle you get from math.atan2() is in radians. We can convert to and from radians using math.rad() and math.deg():
print(math.rad(45)) -- 0.78539816339745
print(math.rad(180)) -- 3.1415926535898 (pi)
print(math.deg(0.78539816339745)) -- 45
print(math.deg(math.pi)) -- 180
You create random numbers using math.random():
math.random() -- a value between 0.0 and 1.0
math.random(100) -- a value between 0 and 100
math.random(20, 40) -- a value between 20 and 40
math.random(-20, -20) -- a value between -20 and 20
Let’s use that to create some spread!
local angle = math.atan2(self.dir.y, self.dir.x)
local spread = math.rad(math.random(-10, 10)) -- -10 to 10 degrees, converted to radians
-- add the spread to the original angle
angle = angle + spread
-- the rest is the same
local rot = vmath.quat_rotation_z(angle)
local props = { dir = self.dir }
factory.create("#bulletfactory", nil, rot, props)
One important thing to note is that math.random() will always return the same sequence of numbers. You can influence which sequence of number that will get generated by providing the algorithm with what’s called a seed. This seed is used as the basis for the random number algorithm. It’s quite common to use the current time as the seed:
math.randomseed(os.time())
Remember to call this function only once, perhaps in the init() function of some script.