Taking a random position from a circle

Hello everyone!
Well, I was bored at studio and I decided that I want to make a thunder bolt, basically.
What I want is, I want to make thunder bolt’s small part looking through a random point selected by a small circle. As I saw from other posts, It requires math as I am not really good at math. So I am really confused on how to make this. Basically I need to take a position from a small circle. But how will I do that? I’d appreciate if someone explains this to me. Thanks!

1 Like

So you’re saying that all you need is a 1 3D position in a 3D circle?

No, I need a 3D Position in a 2D circle.

are you trying to make a mini - map?

No, As I said in my post. I am trying to make a basic thunderbolt which a small part will be looking to a random position in a circle.

So first you already have the Y.

Now you need a random X and a random Z coordinate and there are a few ways to do this:

  1. Get a random X and Z that will give you a point inside the rectangle that is blocking the circle.
    Though the problem with this method is that it could give you points outside the circle, in this region:image
    In this case you should check if the point is inside the circle by checking its distance from the center, and if it’s not inside - discard it and generate a new point.

  2. Use some trig - get a random radius value and a random angle value.
    Use those to get your X and Z, like this:

local x = math.cos(theta) * randomRadius
local z = math.sin(theta) * randomRadius

(Theta is a number between 0 and 2 pi, randomRadius is a number between 0 and your circle’s radius)

1 Like

Pretty bad and old code, but anyways.

local SizeX = 10
local SizeZ = 10

local X = math.random(0,SizeX)
local Z = math.random(0,SizeX)
local IsInverseX = math.random(1,10)
local IsInverseZ = math.random(1,10)
if IsInverseX < 5 then
      X = -X
end
if IsInverseZ < 5 then
      Z = -Z
end

local Pos = Vector3.new(X,Your_Y_Value,Z)
1 Like

you should take the middle position, of the circle, and then in your script add up a maximum of half the diameter(or - instead of + of course), that for Z, X, and for Y I assume it always hits in the same height

You might want a uniform result, so you should generate a random number for the radius, but square root it. (If you don’t then your lightning bolts will more often end up clumping towards the inner portion of the circle)

local randomRadius = defaultRadius * math.sqrt(math.random())
local x = math.cos(theta) * randomRadius
local z = math.sin(theta) * randomRadius

A great explanation why you would square root it:

Also this might be useful if you’re confused on the trig.

4 Likes