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