Custom water physics

Hello

For the past week, I have been trying simulate custom water physics. i was able to create the wave system using gerstner waves.

Dont mind the music…Forgot to turn of desktop audio :smile:

I think the waves look alright, can easily be adjusted.
But the buoyance…no.

That 1 part gets affected by the waves, somewhat. But idk, doesnt look buoyancy to me.

as you can see im using floater parts. But idk. I have no idea how to make this behave like buoyancy.
The parts goes up the wave (kinda) but not down. Probably cuz the water collison is on. But If i turn it off. The part just falls through it. Like does not buoyance at all.

Ive tried following other game engine tutorials (tutorial) and tried to translate the code to lua. Which got me this result kinda.

function BuoyancyModule:Update()
	for _, floater_ in ipairs(self.floaters) do
		local floater = floater_.Parent
		warn(floater)
		local waterLevel = getWaterLevelAtPosition(floater.Position)
		local submergedDepth = math.clamp(waterLevel - floater.Position.Y, 0, floater.Size.Y)
		local submergedVolume = floater.Size.X * submergedDepth * floater.Size.Z
		local fluidDensity = waterPart.CustomPhysicalProperties.Density
		buoyancyFactor = calculateBuoyancyFactor(floater:GetMass(), submergedVolume, fluidDensity, gravity)
		local buoyancyForce = Vector3.new(0, math.abs(buoyancyFactor * submergedVolume * gravity), 0) 

		local bf = floater_:FindFirstChild("BuoyancyForce")
		if not bf then
			bf = Instance.new("BodyForce", floater_)
			bf.Name = "BuoyancyForce"
		end
		bf.Force = buoyancyForce

		-- Send the new position and velocity to the server
		updatePositionAndVelocity:FireServer(floater, floater.Position, floater.Velocity)
	end
end

Maybe I’m overcomplicating things, or maybe I’m really close to having the desired effect. I honestly have no idea.

I’ve been reading through other post from people trying to achieve the same. But I think I got a migraine from all the math.

Any help is greatly appreciated. Also if you need more code snippets, ill gladly provide.

Thanks for your time

1 Like