How can I make this smoother?

I made a script that generates smooth 2D terrain. The way it works is, every 8 units we generate a random value, and a part. In-between each random value we make parts that are interpolated to make it look like 2D Terrain:

Here is the script:

-- Settings
local noiseScale = 8
local amplitude = 10
local mapSize = 512
-- End of settings
local permutations = {}
for x = -mapSize / 2, mapSize / 2, noiseScale do
	permutations[x] = (math.random(0, 256) / 128) - 1
end

for x = -mapSize / 2, mapSize / 2, noiseScale do
	local part = Instance.new("Part")
	part.Anchored = true
	part.Size = Vector3.new(1, 1, 1)
	part.Parent = workspace
    part.Size = Vector3.new(1, (permutations[x] + 1)*amplitude, 1)
	part.CFrame = CFrame.new(x, 0 + (part.Size.Y / 2), 0)
end

function interpolate(x,y,alpha) --simple linear interpolation
	local difference=y-x
	local progress=alpha*difference
	local result=progress+x
	return result
end

for x = -mapSize / 2, mapSize / 2 do
	if x % noiseScale > 0 or x % noiseScale < 0 then
		
		
		local part = Instance.new("Part")
		
		part.Anchored = true
		part.Size = Vector3.new(1, 1, 1)
		part.Parent = workspace

		
		local a = 0
		local b = 0
		local c = 0
		
		for i = x - noiseScale, x do
			if i % noiseScale == 0 then
				a = permutations[i]
				
				break
			end
		end
		
		for i = x, x + noiseScale do
			if i % noiseScale == 0 then
				b = permutations[i]
				
				break
			end
		end
		
		for i = x - noiseScale, x do
			if i % noiseScale == 0 then
				c = (x - i) / (noiseScale - 1)
				
				break
			end
		end
		
		local value = 0
		
		value = interpolate(a, b, c)
				
		part.Size = Vector3.new(1, (value + 1)*amplitude, 1)
		part.CFrame = CFrame.new(x, 0 + (part.Size.Y / 2), 0)
	end	
end

The only issue is, it’s kinda “trianglish” Like look:

Is there any way to make it smoother like this?

Is there any way I can do this?

I assume I would use Spline interpolation instead of normal interpolation, but Idk how to do it.

1 Like

Hope this helps, you can check out the Module. But remember, if you use the Module then you should send credits to the creator.

1 Like

The goal is not to use math.noise to generate terrain. Sorry.

2 Likes

Sorry, I didn’t look at the code. I guess it would suffice to create several pieces, wouldn’t it? Creating parts between parts, that’s the true meaning of tween and that’s what tween service does. So, for me creating more parts should be enough (e.g. if you have a part at position {0,0,0} and you have a part at position {10,0,0}, then just create a part at position {5,0,0}. This sounds at the beginning only nonsense and senseless, but you can continue this practically purely theoretically on the infinity, so more parts, more beautiful)

1 Like

I thought tweenService was for moving parts. I’m not moving parts, I’m creating parts with fixed positions.

2 Likes

I suppose it would really be to have more parts

3 Likes

The issue is, each slope is a straight line. How would I make it so each slope is less steep near the top and bottom?

Have a loop at some interpolation methods. You’re using linear interpolation. Try out cosine interpolation possibly.

1 Like

I guess I wasn’t clear enough:

Here is a Wikipedia page.

So now you can think further.

I’m sorry, but this is too much for me. Still, I hope I helped you. But really click on the links. Tweening is when you make multiple copies of the same object Said by me it sounds weird, but if you really want to have something smooth like the TweenService does (and the TweenService is also used in many other places, like color changes. So you should always check it out first).

Yes. In lua:

local function CosineInterpolate(a, b, t)
    local t2 = (1 - math.cos(t * math.pi)) / 2;
    return(a * (1 - t2) + b * t2);
end
2 Likes

Linear Interpolation - Lerp

The Formula of lerping is simple:

local function lerp(Position1, Position2, Alpha)
    return Position1 + Alpha * (Position2 - Position1)
end

So yeah, the third parameter is the alpha.

Cosine interpolation would be the one I should use because linear interpolation is triangle, but cosine interpolation is steeper in the middle, but at the top and bottom, it gets less steeper.

Thanks for your attempt to help me though.

Cosine interpolation fixed the issue, I think I should also experiment with other interpolation options, thanks your your help @nicemike40, you too @Eternalove_fan32.

1 Like

So what you’re really asking for is something like adding a fillet to your lines.

That’s some fun math if you don’t wanna do the easy way :slight_smile:

Here’s something you can follow to calculate where a circle of radius R would sit nestled between two lines. That’s the bulk of the work, you can probably figure out how to apply that to your situation.

I might take a crack at this though, sounds fun.

2 Likes

Update: I made a desmos demonstrating the math required here. Warning: It’s not really notated well. Might transfer to lua at some point :slight_smile:

1 Like