bodyDef := Tb2BodyDef.Create;
bodyDef.bodyType := b2_dynamicBody;
fd := Tb2FixtureDef.Create;
fd.shape := m_polygons[idx];
fd.density := density;
// Override the default friction.
fd.friction := 0.3;
fd.restitution := 0.1;
SetValue(bodyDef.position, RandomFloat(-18, 18), RandomFloat(4, 13));
bodyDef.angle := RandomFloat * Pi;
body := m_world.CreateBody(bodyDef);
body.CreateFixture(fd, True, False);
And here is the sample code from the Step function which shows how the water is handled and drawn:
procedure TBuoyancyController.Step(var settings: TSettings; timeStep: PhysicsFloat);
const
waterlevel: RGBA = (0.0, 0.0, 1.0, 1.0);
waterarea: RGBA = (0.0, 0.0, 1.0, 0.3);
var
theta: PhysicsFloat;
vertices: array[0..3] of TVector2;
begin
time := time + timeStep;
if wave then
begin
theta := 10 * Sin(time / 2) / 180 * Pi; // 10 is the max angle of wave
SetValue(bc.normal, -Sin(theta), Cos(theta));
end
else
theta := 0;
inherited;
vertices[0] := MakeVector(-20, -10);
vertices[1] := MakeVector(-20, -20 * Tan(theta));
vertices[2] := MakeVector(20, 20 * Tan(theta));
vertices[3] := MakeVector(20, -10);
m_world.Draw.DrawSolidPolygon((Pb2PolyVertices(@vertices[0]))^, 4, waterarea);
m_world.Draw.DrawSegment(vertices[1], vertices[2], waterlevel);
end;
Download the Box2d Firemonkey for Delphi XE5 and XE6 Firemonkey with Buoyancy Controller Demo. Or access the Buoyancy Controller Demo unit on Github.