?How Waves systeam works

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

:sick:I will try to summarize the topic as much as possible because I do not want to use these equations.

image

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
:clock1:

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 :face_with_spiral_eyes:

[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 ^-

8 Likes

Nahh… you got the subway surfer plugin… How much brain rot can you handle???

Also belongs in #resources:community-tutorials -

Nevermind you just changed it.

5 Likes

yea haha i just forgot to turn it into tutorials i did that btw

What about the bones? How and where can I put them?

Use blender for that.

about bones just use blender so you can create a plane
They are the most important thing in the code because they move the ocean as we want.


here download plane (sorry i removed the texture)
better use another plane because my plane have +1400 bones and that cost lag
ocean plane.rbxm (14.3 کیلوبایت)

1 Like

Best settings wave animation

local Waves ={
	{Vector3.new(1,0,rng:NextNumber(0.1,0.3)),math.random(99,100)},
	{Vector3.new(-1,-1,rng:NextNumber(0.1,0.5)),math.random(99,120)},
	{Vector3.new(1,1,rng:NextNumber(0.1,0.3)),math.random(99,120)},
	{Vector3.new(1,1,rng:NextNumber(0.1,0.2)),math.random(99,100)},
	{Vector3.new(-1,0,rng:NextNumber(0.1,0.6)),math.random(99,100)},
	{Vector3.new(0,1,rng:NextNumber(0.1,0.7)),math.random(99,100)},
	{Vector3.new(-1,0,rng:NextNumber(0.1,0.4)),math.random(99,100)},
	{Vector3.new(0,1,rng:NextNumber(0.1,0.6)),math.random(99,100)},
	{Vector3.new(-1,0,rng:NextNumber(0.1,0.8)),math.random(99,100)},
	{Vector3.new(0,1,rng:NextNumber(0.1,0.3)),math.random(99,100)},
	{Vector3.new(-1,0,rng:NextNumber(0.1,0.3)),math.random(99,100)},
}