How would I make a tree generator?

I’m trying to achieve randomly generated trees not a selection from a set of premade trees

I have an idea, you could start with the base for the tree, then a ray is cast in some direction, you create a branch the size of the ray with its position being the origin / the limit , do the same for the forks but with a smaller size

How would I make a part the size of a ray?

using ray.magnitude :shallow_pan_of_food:

1 Like

Maybe you could get the length of the ray and set one or two of the Vector3 size values to the length.

@ThanksRoBama has already made a palm tree generator, what you will find useful is how the parts are made an attached to the tree. For something else other than a palm tree you will have come up with your own.

Edit: CCing @DaMajestick, this should help with the CFrames

ah ok thanks I will try using rays

It would have the prerequisite of the front side being the one with the branches’ point, although you could circumvent that buy using rightvector or upvector.

Also how would I use ray.Magnitude? Isn’t magnitude a measurement for distance?

yes, i think there was a way to establish a ray’s lenght, and if it touched nothing it would return where the ray would end based on lenght provided. so then you use (origin - hitPosition).Magnitude to get the effective distance between the start and end.

Firstly, why rays? You can just use CFrames.

Im not very good with CFrames how would I do what you suggested?

If you get the relative CFrame of a branch then you can multiply it with half the Y axis size which will get you the tip of the branch then you can create another branch there. Also, you can use CFrame.Angles if you want to rotate the new branch.

alright, thank you for the explanation

Rays are used to check collisions. idk why you need it in this case

you just need to generate a random direction in a 3d cone relative to the previous generated direction.

local rng_v = Random.new()

function RandomVectorOffset(v, maxAngle) --returns uniformly-distributed random unit vector no more than maxAngle radians away from v
    return (CFrame.new(Vector3.new(), v)*CFrame.Angles(0, 0, rng_v:NextNumber(0, 2*math.pi))*CFrame.Angles(math.acos(rng_v:NextNumber(math.cos(maxAngle), 1)), 0, 0)).LookVector
end

The function by

you’ll start by getting the first direction from the up vector of the base

local max_angle = math.pi/8
local FirstDirection = RandomVectorOffset(BasePart.CFrame.UpVector, angle)

and then

local SecondDirection = RandomVectorOffset(FirstDirection, angle)

and keep going like that in a loop getting random directions that are limited by the angle you setted up and building the tree trunk

would this allow for a branch off of another branch or just building branches on a trunk

yes for sure
if you want 2 branchs
make 2 directions out of the previous direction instead of 1 direction


I actually just learnt this by researching your problem in a more mathematical way

I’m having troubles understanding, these are just angles right?

It’s not easy btw. basically I gave you just the key to the problem you’re facing
which is generating a random direction in a 3D cone.
I might implement it in my free time
I’ll make sure to share it with you and the community if it is successful

ah alright I’m just very confused as to how you build off of the angles

1 Like