I need to get the details of the portion of a part that intersects the region. I’m not sure if it’s possible though.
This is to make a volume-based buoyancy system (with a set water level), so other alternatives are welcome.
I’ve drawn a very helpful diagram of what I’m asking for:
Use workspace:GetPartBounesInBox(RegionCframe,RegionSize,OverlapParams(You can ignore this))
you can get a table include all parts which bounds in the region.
This method returns not the volume of each part intersecting, but the part itself. Maybe there’s any other way (pls clarify me if I’m wrong since I deducing from the docs)
This really depends on how in depth you want to go with this. If you use parts that are not rotated, you can compared the y position of the cylinder to the surface of the water to get how much of the part is submerged and then find the volume using math (there formulas for cut cylinders, rectangular prisms, and spheres). If you include rotated parts, then I’m sure its still possible, but the math would increase in difficulty. (I have a few ideas on how this might be tackled.) I can try and provide these if you want them, but I am not going to spend time on a solution that I don’t know if you want.
The main object is a submarine with a mostly union-based structure (but one assembly).
You don’t have to write all the code, just provide your ideas if you have any (thanks).
In the best case scenario I want to factor in rotation, but in case it’s not possible I won’t.
(Getting the volume of a cuboid below a certain Y level is useful too)
You could use SubtractAsync() or better yet IntersectAsync() on a simplified version of the submarine and based on the result union’s mass you can calculate what percentage of the submarine is underwater.
You’d have to do the union operations on server side though and I’m not sure how real time it can be made.
I think the best thing you can do here is to keep the structure ununioned. Then, iterate through each part to determine whether they are submerged or not. For the parts who are at water level, you could 1) ignore them if your parts are small enough or 2) calculate their submerged volume. For a nonrotated cubiod, finding volume is easy: the ratio of the height the cuboid is submerged to total height multiplied by the cuboid’s volume.
For a rotated cuboid I found that it was already answered on the mathematics stack exchange: calculus - Calculating Volume of the Submerged Portion of a Rotated Rectangular Prism - Mathematics Stack Exchange. Its quite a simple and elegant equation, with the only kind of difficult variable to find being the dy variable. You can find this by taking the position of y of your cuboid then finding how to get to the water level with the upVector of the part (or whichever vector is facing upwards). Take difference of the water-level’s y position and the part’s y position and divide that with the upVector’s y coordinate. This will give you how far the water level is from the part’s center position to where the central axis of the part touches the water which is what dy is. It works because the upVector has a length of 1, and the math above basically just describes how many upVector we need to reach the water level.
For other shapes, I am sure there are more equations out there. Spheres, for example, would be easy since they are the same with any rotation. Overall, a pure math-based solution here seems very possible as long as you use parts you know the shape of.
I’ve found a way to get an approximate volume (that is usable in this case, mass/density exists) of the union (by creating a grid of cubes and checking each one if it intersects the union), so that’s why I need the cuboid volume formula.
This is very helpful, thank you!