Custom Buoyancy System

Im trying to make a custom buoyancy system for ships and im honestly not getting anywhere
https://gyazo.com/3242fad0b0bbb267bb85008a36ba23d8
this is the current code im using

local beforeSubmerge = game.Workspace.Water.Position.Y + 1
local offset = 3
local count = 0
repeat wait() until game.Workspace.Part

game:GetService("RunService").Heartbeat:Connect(function()
	if game.Workspace.Part.Position.Y < game.Workspace.Water.Position.Y then
		local displasementMultiplier = math.clamp(-game.Workspace.Part.Position.Y / beforeSubmerge,0,1) * offset
		print(displasementMultiplier)
		if count % 10 then
		game.Workspace.Part.Velocity = (Vector3.new(0,math.abs(-9.81) * displasementMultiplier,0) * (game.Workspace.Part.Mass*(game.Workspace.Part.Position - game.Workspace.Water.Position).magnitude/math.pow(0.5,2)))
		end
		count += 1
	end
end)
1 Like

This must be a dampening problem with the velocity as nothing is limiting the oscillations, try applying some spring equations.

Could you use a BodyPosition | Roblox Creator Documentation and set the .D and .P values to decrease the Force instead of increase it when it reaches the proper height?

Also, why are you using math.abs(-9.81)?
math.abs returns the + value for the calculation in the brackets following it, so basically you are saying 9.81.

Also Roblox gravity is 196.2 studs/second squared. Why are you using earth gravity of 9.81 meters/second squared?

What happens if you decrease the offset = 3 value?

i basically used unity’s gravity value cuz i literally retransformed it from c# to luau (the entire code was in c#) so i didnt know how it would react to this

gotcha, gonna try to use body position. i also tried to look for a solution on another post and they used water volume, depth, water resistance there etc. i may try to understand whats in it again

Pretty sure quenty uses like some sort of velocity based bouyancy for his game so yeah. ( or he uses terrain water physics since buoyancy exists)

Or try using actual Terrain water. My boats work pretty well with it.

i want to use mesh water so that isnt really the case ¯_(ツ)_/¯

local Object = game.Workspace:WaitForChild("Part")

local beforeSubmerge = 1
local DisplacementAmount = 3

local Water = game.Workspace.Water
local TopWater = Water.Position.Y + 0.5*Water.Size.Y

--local F = (0.5 * Object:GetMass())*2 * 2.05 * 96

local DisplacementMultiplier = math.clamp(Object.Position.Y / beforeSubmerge,0,1) * DisplacementAmount
print(DisplacementMultiplier)


local waterDrag = 0.5
local waterAngularDrag = 0

local Force = Instance.new("BodyForce",Object)


game:GetService("RunService").Heartbeat:Connect(function(DeltaTime)
	if Object.Position.Y < TopWater then
		Force.Force = Vector3.new(0,(game.Workspace.Gravity * DisplacementMultiplier) * (math.abs(TopWater - Object.Position.Y) /Object:GetMass()),0)
print(Force.Force)
	else
		Force.Force = Vector3.new(0,0,0)
		end
end)

Current script/progress
https://gyazo.com/f462f47e3aea3e6bd5a5151fddcfb394
@RatiusRat @Scottifly
it actually clicked for me when i remembered that u have to apply more force the deeper the object goes