Hello. everyone
today i’ll be dicussing ocean simulation
this post will try to take a glimpse at how difficult it can be to create a game such as sea of thieves
I will try to summarize the topic as much as possible because I do not want to use these equations.
to get start we first need know about sine
the first mathematical function i can think of for water waves is the sine
function although this could work
it isn’t as realistic as actual water to make this look more like waves we need to take a step back
imagine a point following the path of circle
if we duplicate this side by side with slightly different offsets we can see waves these are gertzner waves and this is what i will be implementing today
to get start we have to create a generator of waves
but first we need something so the waves can follow it as the we want
.as you see here the more you add, the more realistic and better your sea becomes much better
local Waves ={
{Vector3.new(1,1,0.3),100}, -- 100 it's wavelength and the vector it small moves
{Vector3.new(0,1,0.25),120},
}
So now we need an equation waves
Point is bone position and RunTime is tick
local function GerstnerWave(Point: Vector3)
local SyncTime = math.abs(tick()-RunTime)
local p = Vector3.new()
for i,Wave in pairs(Waves) do
local steepness = Wave[1].z
local wavelength = Wave[2]
local k = pi2/wavelength
local c = math.sqrt(9.8/k)
local d = Vector2.new(Wave[1].x, Wave[1].y).unit
local f = k*(d:Dot(Vector2.new(Point.x,Point.z)) - c * SyncTime+(tick()-RunStart))
local a = steepness/k
local cosf = math.cos(f)
local sinf = math.sin(f)
p = p + Vector3.new(d.x*(a*cosf), a*sinf, d.y*(a*cosf))
end
return p
end
so you can run the function by code
--//Use loop
Bone.Transform = CFrame.new(GerstnerWave(Bone.WorldPosition)/4)
And here is the final! you can change waves size by change math.pi*2
The smaller the number, the larger the size, and vice versa.
Example
[Normal waves]
[Lower Waves]
i forgot to turn plugins off
[Big Waves]
I really tried to make the topic as simple and clear as possible because the topic contains a lot of equations, but I only took the important equations only.
FULL CODE
local Plane = workspace:WaitForChild("Plane")
local Camera = workspace.CurrentCamera
local RunTime = tick()
local RunStart = tick()
local pi2 = math.pi/2
local Waves ={
{Vector3.new(1,1,0.3),100},
{Vector3.new(0,1,0.25),120},
}
local function GerstnerWave(Point: Vector3)
local SyncTime = math.abs(tick()-RunTime)
local p = Vector3.new()
for index,Wave in pairs(Waves) do
local steepness = Wave[1].z
local wavelength = Wave[2]
local k = pi2/wavelength
local c = math.sqrt(9.8/k)
local d = Vector2.new(Wave[1].x, Wave[1].y).unit
local f = k*(d:Dot(Vector2.new(Point.x,Point.z)) - c * SyncTime+(tick()-RunStart))
local a = steepness/k
local cosf = math.cos(f)
local sinf = math.sin(f)
p = p + Vector3.new(d.x*(a*cosf), a*sinf, d.y*(a*cosf))
end
return p
end
while task.wait(.01) do
for index, Bone in pairs(workspace.Plane:GetChildren()) do
if Bone:IsA("Bone") then
Bone.Transform = CFrame.new(GerstnerWave(Bone.WorldPosition)/4)
end
end
end
anything you want me to help you with wave just ask ^-