Check if model is not in the space or intertwining with another model

Hey, this is a very advanced task in which I am not in the mental state to do the math my self.

I am generating a forest using assets and models that were provided to me by a friend, and as you can see in the following image, the models are placed inside or on top of each other, or in a bunched up group.

Would anyone know how to prevent this? By prevent I mean some type of equasion to figure out if an object that is a descendant of the model, is inside of another object from a different mode, then it regenerates the position

This is the generation script.

-- locals
local spwn = script.Parent
local objects = game.ServerStorage.objects

local spawningObjects = objects:GetChildren()

local point1rangeX = spwn.pointA.Position.X
local point2rangeX = spwn.pointB.Position.X
local point1rangeZ = spwn.pointA.Position.Z
local point2rangeZ = spwn.pointB.Position.Z

--local rangeY = spwn.Position.Y+0.250

local e -- object clone
--

-- generation
for i,v in pairs(spawningObjects) do
	if v then
		local traits = v.traits
		local amount = Random.new():NextNumber(traits.least.Value,traits.max.Value)

		repeat
			e = v:Clone()
			e.Parent = game.Workspace

			local xpos = Random.new():NextNumber(point1rangeX, point2rangeX)
			local zpos = Random.new():NextNumber(point1rangeZ, point2rangeZ)
			
			e:SetPrimaryPartCFrame(CFrame.new(Random.new():NextNumber(point1rangeX, point2rangeX), 3.875, Random.new():NextNumber(point1rangeZ, point2rangeZ)) * CFrame.Angles(0, Random.new():NextNumber(0, 360), 0))
			amount = amount-1
		until amount < 1
	end
end

Thanks.

Something like this may work (I marked where some code would run if an overlap occured with the warn())

for _, Model in ipairs(TREES) do
    local Params = OverlapParams.new()
    Params.FilterDescendantsInstances = {Model}
    Params.FilterType = Enum.RaycastFilterType.Exclude
    for _, Part in ipairs(Model:GetChildren()) do -- edit: Made it 'do', not 'end'
        if #workspace:GetPartsInPart(Part, Params) > 0 then
            warn("OVERLAP")
        end
    end
end

Gonna go ahead and correct a typo here while implementing this to test,

for _, Model in ipairs(TREES) do
    local Params = OverlapParams.new()
    Params.FilterDescendantsInstances = {Model}
    Params.FilterType = Enum.RaycastFilterType.Exclude
    for _, Part in ipairs(Model:GetChildren()) do -- not end
        if #workspace:GetPartsInPart(Part, Params) > 0 then
            warn("OVERLAP")
        end
    end
end

happens to the best of us

Woops xD

Well, while testing with this code:

-- locals
local spwn = script.Parent
local objects = game.ServerStorage.objects

local spawningObjects = objects:GetChildren()

local point1rangeX = spwn.pointA.Position.X
local point2rangeX = spwn.pointB.Position.X
local point1rangeZ = spwn.pointA.Position.Z
local point2rangeZ = spwn.pointB.Position.Z

--local rangeY = spwn.Position.Y+0.250

local e -- object clone
--

-- generation
for i,v in pairs(spawningObjects) do
	if v then
		local traits = v.traits
		local amount = Random.new():NextNumber(traits.least.Value,traits.max.Value)

		repeat
			e = v:Clone()
			e.Parent = game.Workspace

			local xpos = Random.new():NextNumber(point1rangeX, point2rangeX)
			local zpos = Random.new():NextNumber(point1rangeZ, point2rangeZ)
			
			e:SetPrimaryPartCFrame(CFrame.new(Random.new():NextNumber(point1rangeX, point2rangeX), 3.875, Random.new():NextNumber(point1rangeZ, point2rangeZ)) * CFrame.Angles(0, Random.new():NextNumber(0, 360), 0))
			
			local Params = OverlapParams.new()
			Params.FilterDescendantsInstances = {v}
			Params.FilterType = Enum.RaycastFilterType.Exclude
			for _, Part in ipairs(v:GetChildren()) do
				if #workspace:GetPartsInPart(Part, Params) > 0 then
					warn("OVERLAP")
					v:Destroy()
				end
			end
			
			amount = amount-1
		until amount < 1
	end
end

I got this error.
image
I personally have never encountered this before and have no idea how to fix it lol,

I guess you have some non-part instances in the tree?
Just add this line after the
for _, Part in…
line:

if not Part:IsA("BasePart") then continue end

After doing this, it prints an overlap a single time and then errors like this.


It may be because of the v:Destroy() part in line 38/39, but I am unsure of how else to go about this. For some reason I find my self not being able to think correctly at the moment, so excuse me if I’m a cornball and miss something really simple.

It seems to stop generating after this error

After a few changes it seemed to work pretty well, if you have any more input message it to me via Developer Forums. Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.