Games [XUI2D] Floating Bodies Simulation

Erel

Administrator
Staff member
Licensed User

This is a small and nice example of simulating bodies thrown into water.
It is inspired by this tutorial: http://www.iforce2d.net/b2dtut/buoyancy
The implementation is different.

The main task is to find the volume and center of mass of the underwater body. The floating force is -gravity * underwater volume. Volume = mass / density.
The force is applied to the underwater center of mass.

If you run it with debug drawing enabled you will see a green dot that marks the center of gravity of the underwater bodies:


I used a "brute force" approach to find the mass and center of mass.
We take the rotated CompressedBC object and count the the number of non-transparent pixels in each row.
There is an assumption that there are no holes in the body (only relevant for the center of mass).

The code to go over the CompressedBC pixels is based on CompressedBC internal structure. It is not part of the public API of BitmapCreator.

This example is included in the example pack.

It is recommended to use BitmapCreator v4.31+ with this example.
 
Top