Making a road generator

So recently, I tried to make a road generator. But I encountered a problem, how to make road rotate 2 degree in left or right, i used math.random to get a value and understand turn the road on the left or on the right. So now I had to find top right point of the model or left top point, but I didn’t understand how, it’s easy to find it when it’s 0 degree rotated, just use size measures and it’s done, but if the road is already rotated like in 5 degree, then it’s getting harder, as I need another formula and not just the size using, ig that I need trigonometrical functions, but I’m not that good at them to be honest.
I tried: taking the archimedes module script - was too long and it wasn’t the way, because I need to find the solution quickly and not read entire script and check every function. Can someone help me with finding a formula to find those points, that will be used in rotation the road?

1 Like

Are you actually trying to add roads end to end?
Maybe use look at in Cframe to get direction of the inserted road to add them by end to end?

I found a solution, tho not optimized I guess:

local function getModelBoundaries(model)
	local Boundary = {}
		
	local part = Instance.new("Part")
	part.Transparency = .5
	part.Name = "BoundaryBox"
	part.CFrame = model:GetBoundingBox()
	part.Size = model:GetExtentsSize()
	part.Parent = model
	local pcf = part.CFrame
	
	Boundary.RightTop = pcf * CFrame.new(-part.Size.X/2, 0, part.Size.Z/2)
	Boundary.RightCenter = pcf * CFrame.new(-part.Size.X/2,0,0)
	Boundary.RightBottom = pcf * CFrame.new(-part.Size.X/2,0,-part.Size.Z/2)
	
	Boundary.LeftTop = pcf * CFrame.new(part.Size.X/2, 0, part.Size.Z/2)
	Boundary.LeftCenter = pcf * CFrame.new(part.Size.X/2,0,0)
	Boundary.LeftBottom = pcf * CFrame.new(part.Size.X/2,0,-part.Size.Z/2)
	
	Boundary.Center = pcf
	
	part:Destroy()
	return Boundary
end


local function continueRoad(parent,road,angle)
	if not road.Main then return end
	local attachmentTop,attachmentCenter,attachmentBottom
	if math.abs(angle) == angle then --positive
		attachmentTop = "RightTop"
		attachmentCenter = "RightCenter"
		attachmentBottom = "RightBottom"
	else --negative
		attachmentTop = "LeftTop"
		attachmentCenter = "LeftCenter"
		attachmentBottom = "LeftBottom"
	end
	
	local roadMain = road.Main
	local newRoad = road:Clone()
	local newRoadMain = newRoad.Main
	newRoad.Parent = parent
	--boundaries
	local newBoundary = getModelBoundaries(newRoad)
	local oldBoundary = getModelBoundaries(road)
	
	newRoadMain.CFrame = (oldBoundary[attachmentTop]*CFrame.Angles(0,math.rad(angle),0))
	--update boundaries
	newBoundary = getModelBoundaries(newRoad)
	oldBoundary = getModelBoundaries(road)
	
	newRoadMain.CFrame += (newBoundary.Center-newBoundary[attachmentBottom].Position).Position
end

Keep in mind that there are some of my previous footsteps left, so you will have to change some things before using it because I did a ton of testing(Im dumb)

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