Gerstner wave aligning not working

hello, i’m having some issues with a gerstner wave function i mirrored from unity.

-- // MADE BY PUG64 >:)

-- // SERVICES


-- // VARIABLES

local WaveFunctions = require(game.ReplicatedStorage.Modules.WaveFormulae)
local RS = game:GetService("RunService")
local Camera = workspace.CurrentCamera
-- // FUNCTIONS

-- // MAIN
local Origins = {}

for Index,Bone:Bone in pairs(workspace.Ocean.Plane:GetChildren()) do
	if Bone:IsA("Bone") then
		Origins[Bone] = Bone.WorldPosition
	end
end

local Waves = { -- 1 wavelength 2 speed 3 amplitude 4 steepness 5 direction
	[1] = {15,1,2.6,1.2,Vector2.new(1, 0)},
	[2] = {12,1,3,1.6,Vector2.new(2, 0.4)},
	[3] = {12,1,3,1.2,Vector2.new(0, 1)},
}

local Speed = 1
local Scaling = workspace.Ocean.Plane.Size.X

local function GetData(origin,Time)
	local gridPoint = origin/Scaling

	local p = gridPoint
	local tangent = Vector3.new(1, 0, 0)
	local binormal = Vector3.new(0, 0, 1)
	
	for i,v in pairs(Waves) do
		local d_pos,d_nor,d_t,d_b = WaveFunctions:GerstnerFormula(Time,origin,table.unpack(v))
		p += d_pos
		tangent += d_t
		binormal += d_b
	end
	
	local position = p
	local normal = binormal:Cross(tangent).Unit
	
	return position,tangent,normal
end
local function getfirst()
	for i,v in pairs(Origins) do
		return v
	end
end
RS.RenderStepped:Connect(function()
	
	local Time = os.clock() * 3
	
	local Post,Tant,Normt = GetData(Vector3.new(0,0,0),Time)
	workspace.Part.Position = Vector3.new(0,0,0) + Post
	for Index,Bone:Bone in pairs(workspace.Ocean.Plane:GetChildren()) do
		if Bone:IsA("Bone") and (Camera.CFrame.Position - Origins[Bone]).Magnitude <= 400 then
			local Pos,Tan,Norm = GetData(Origins[Bone],Time)
			Bone.Position = Origins[Bone] + Pos
			Bone.Axis = Tan
			Bone.SecondaryAxis = Norm
		end
	end
end)

local script

local Waves = {}


function Waves:GerstnerFormula(aTime:number,Position:Vector3,waveLength:number,Speed:number,Amplitude:number,Steepness:number,Direction:Vector2)
	local k = 2.0 * math.pi / waveLength
	local kA = k * Amplitude
	local D = Direction.Unit
	local K = D * k; 
	
	local S = Speed * 0.5
	local w = S * k
	local wT = w * aTime
	
	local KPwT = K:Dot(Vector2.new(Position.X,Position.Z)) - wT
	local S0 = math.sin(KPwT)
	local C0 = math.cos(KPwT)
	
	local XZ = Vector2.new(Position.X,Position.Z) - D * Steepness * Amplitude * S0
	local y = Amplitude * C0
	
	local B = Vector3.new(
		1 - (Steepness * D.X * D.X * kA * C0),
		D.X * kA * S0,
		-(Steepness * D.X * D.Y * kA * C0)
	)
	local T = Vector3.new(
		-(Steepness * D.X * D.Y * kA * C0),
		D.Y * kA * S0,
		1 - (Steepness * D.Y * D.Y * kA * C0)
	)
	
	local B = B.Unit
	local T = T.Unit
	
	local Normal = T:Cross(B)
	
	local Position = Vector3.new(XZ.X,y,XZ.Y)
	return Position,Normal,T,B
end

return Waves

module

practically what i’m trying to do is align a part to the water, but it’s not working.


for some reason this happens, when it’s 0 0 0 it’ll be perfect but anything else just is wack

Weirdly, I’ve helped someone with this exact same problem before:

Try reading through that whole thread and see how far you can get. The OP eventually figured it out so you might also want to shoot them a message.

Nice, seems to be a solution.


This is my plane, made from quad’s i think.

I started with making this function right here:

local function FindClosestPoints2(OriginalPos)
	local Found = {}

	local Distances = {}
	
	for Bone,Position in pairs(Origins) do
		Distances[#Distances + 1] = {Object = Bone; Distance = (Bone.WorldPosition - OriginalPos).Magnitude}
	end
	
	table.sort(Distances,function(a,b)
		return a.Distance < b.Distance
	end)
	
	return {Distances[1],Distances[2],Distances[3]}
end

This seems to work really well, it gets the position and normal 90% of the time.

Although…

For some reason, two of the points lock onto the same bone, and i can’t figure out why.
: - )

damn, figured out that my plane has duplicate bones and i’ve been struggling for 45 mins not knowing why, i’ll mark your post as a solution thx

alright, came back to this…
do you know how i can use my function in a way that won’t bug?
sometimes it gets in the middle of two quads and just bugs out for a sec and it’s really dissatisifying

Part of the problem could be that “the three closest bones to me” and “the three bones that make up the triangle I’m on” are two different questions that can have different answers.

You might be on one triangle, but be closest to the points of a different triangle.

Idk if that’s your particular problem but it might be.

how could i find the triangle i’m on then?

I gave some suggestions in that thread

I don’t know what exact method the OP ended up using, they didn’t share that in the thread. You could try to PM them

1 Like

i’ll try asking him, i’ll see if i can try your suggestions too. Thank you.

2 Likes