# Custom Buoyancy

Hello, I was wondering if there would be a way to create an actor component that simulates buoyancy. If someone could share some ideas (and or pictures) of how to make realistic or close to realistic buoyancy. I really don’t want to purchase anything, and I have tried many ways but still can’t get it right, any suggestions will be greatly appreciated.
Thanks!

the way it’s done is either by physics “push” at the location, based on whatever the buyancy settings you choose, or outright overwrite of the world Z value of the object to match the value of a simulated wave.

You have math happening on the shader - that generates the wave.
And you have a very similar function solving for Z at X and Y world location to handle the position of water in C++.

From the position of where the water is at the location, you can calculate the amount of force to apply to keep the object at the correct position.

The phys calc portion can be as simple or as complicated as you desire it to be.
I personally rather override the Z entierly / complely forgetting buoyancy is even a thing - because it literally runs way faster.

You can probably do a bastardized Archimedes principle (remember to scream eureka after it works).
(amount of displaced water / number of pushing components in object ) * density of liquid * gravity

The idea is that applying a constant force will eventually counter the physics received from other sources (say a character jumping on and applying a downward push to the object).

1 Like

How would I get the amount of displaced water? That’s what I’ve be having trouble doing.

That’s the size of the object in practice.
Water displacement is based on the volume of an object being introduced into the water by whatever amount it’s submerged.

You can probably calculate this on the fly by using the object bounding box as a source to calculate its volume.

Obviously, it’s not going to be 100% accurate to reality. Just assume the whole object is always completely submerged.
And that when it floats, it’s 50% of the object that’s submerged.
That is by no means accurate, but it should allow you to create interesting effects once the physics push start happening.

If you want to learn off of others work, the community ocean project has a decent breakdown of this.
It’s community made, so it isn’t necessarily the best way to do it, but it does do it…

1 Like

I got it to work!! The only problem I have is getting it inn sync with the waves. How would I get the position of the wave. I tried creating a dynamic material instance and doing the wave calculation in the actor component like I did in the material, but that broke it. Is there a way to grab the material position? I want the waves to be random so it can be as little as 0 and as many as 200 but that would hard to get right by recalculating and could be bad fore performance .

In short. You can’t.

First of a wave is a known pattern, not randomness.

Usually grstner looks the best. It’s also complex in terms of computation, since it’s the avaragrd sum of 4 to 8 different result.

Other not as pleasing alternatives involve adding sines and deciding by how many you added to average out.

Regardless, the whole point of it is that any wave is driven at a specific location by the Time variable.

You have to always be able to solve the Z value given X, Y, and game time.

It’s the only way you can do this.

There is no “oh the material does it. Let’s sample it”.
The material is completely separate and on the GPU.
The physics are completely sepaparate and on the CPU.
There is really no in-between across the 2 that allows you to easily read a memory value or even just pass data from the gfx to the the cpu in a way that would help liquid simulations.

Stick to known formulas like gerstner, so you can have an easier time debugging.
Unless you need something specific for a game effect of some sort, in which case I suggest you set up gerstner first to understand how stuff works (as you’ll find tutorials on it).
And then you can look at Ryan Brucks talks and functions, which are different and give you a decent idea of how to tackle custom solutions in unreal.

and to be extra clear
There’s really no way to sample ripples off a material either.
This makes it particularly hard, since ripples are usually graphics, not math.

To account for ripples you’d have to use math on both ends, making the material code portion a real pita when all you’d need otherwise is drawing to a render target…

1 Like