Boats and Submarines#
A hull floats when the water it pushes aside weighs more than the hull does.
Flight.displacement is the volume of water the vessel pushes aside when it is
fully submerged, and Flight.buoyant_force_at() is the force that volume pushes back
with:
body = vessel.orbit.body
flight = vessel.flight()
displacement = flight.displacement
buoyancy = flight.buoyant_force_at(body.ocean_density, body.surface_gravity)
var body = vessel.Orbit.Body;
var flight = vessel.Flight ();
var displacement = flight.Displacement;
var buoyancy = flight.BuoyantForceAt (
body.OceanDensity, body.SurfaceGravity);
auto body = vessel.orbit().body();
auto flight = vessel.flight();
auto displacement = flight.displacement();
auto buoyancy =
flight.buoyant_force_at(body.ocean_density(), body.surface_gravity());
krpc_SpaceCenter_Orbit_t orbit;
krpc_SpaceCenter_Vessel_Orbit(conn, &orbit, vessel);
krpc_SpaceCenter_CelestialBody_t body;
krpc_SpaceCenter_Orbit_Body(conn, &body, orbit);
krpc_SpaceCenter_Flight_t flight;
krpc_SpaceCenter_Vessel_Flight(conn, &flight, vessel, KRPC_NULL);
double displacement;
krpc_SpaceCenter_Flight_Displacement(conn, &displacement, flight);
double density, gravity, buoyancy;
krpc_SpaceCenter_CelestialBody_OceanDensity(conn, &density, body);
krpc_SpaceCenter_CelestialBody_SurfaceGravity(conn, &gravity, body);
krpc_SpaceCenter_Flight_BuoyantForceAt(conn, &buoyancy, flight, density,
gravity);
CelestialBody body = vessel.getOrbit().getBody();
Flight flight = vessel.flight(null);
double displacement = flight.getDisplacement();
double buoyancy = flight.buoyantForceAt(
body.getOceanDensity(), body.getSurfaceGravity());
local body = vessel.orbit.body
local flight = vessel:flight()
local displacement = flight.displacement
local buoyancy =
flight:buoyant_force_at(body.ocean_density, body.surface_gravity)
const body = await (await vessel.getOrbit()).getBody();
const flight = await vessel.flight();
const displacement = await flight.getDisplacement();
const buoyancy = await flight.buoyantForceAt(
await body.getOceanDensity(),
await body.getSurfaceGravity(),
);
Compare that force with the vessel’s weight. A hull with more buoyancy than its weight
floats. A submarine trims its tanks to move between the two.
Part.displacement and Part.buoyant_force_at() give the same figures for one
part.
Note
Flight.buoyant_force_at() applies SpaceCenter.buoyancy_scalar, which is 1.2
in stock KSP. A buoyant force in the game is a fifth larger than
Archimedes’ principle alone
gives.
Trim#
A hull at rest carries its center of buoyancy on the same vertical line as its center of mass. A horizontal offset between the two turns the weight and the buoyancy into a couple that rolls or pitches the hull. That offset is the trim.
Read both in the vessel’s surface reference frame. Its x axis points up, so the trim is what the other two axes carry:
flight = vessel.flight(vessel.surface_reference_frame)
center_of_buoyancy = flight.center_of_buoyancy
center_of_mass = flight.center_of_mass
var flight = vessel.Flight (vessel.SurfaceReferenceFrame);
var centerOfBuoyancy = flight.CenterOfBuoyancy;
var centerOfMass = flight.CenterOfMass;
auto flight = vessel.flight(vessel.surface_reference_frame());
auto center_of_buoyancy = flight.center_of_buoyancy();
auto center_of_mass = flight.center_of_mass();
krpc_SpaceCenter_ReferenceFrame_t frame;
krpc_SpaceCenter_Vessel_SurfaceReferenceFrame(conn, &frame, vessel);
krpc_SpaceCenter_Flight_t flight;
krpc_SpaceCenter_Vessel_Flight(conn, &flight, vessel, frame);
krpc_tuple_double_double_double_t center_of_buoyancy, center_of_mass;
bool out_of_water;
krpc_SpaceCenter_Flight_CenterOfBuoyancy(conn, ¢er_of_buoyancy,
&out_of_water, flight);
krpc_SpaceCenter_Flight_CenterOfMass(conn, ¢er_of_mass, flight);
Flight flight = vessel.flight(vessel.getSurfaceReferenceFrame());
Triplet<Double, Double, Double> centerOfBuoyancy =
flight.getCenterOfBuoyancy();
Triplet<Double, Double, Double> centerOfMass =
flight.getCenterOfMass();
local flight = vessel:flight(vessel.surface_reference_frame)
local center_of_buoyancy = flight.center_of_buoyancy
local center_of_mass = flight.center_of_mass
const flight = await vessel.flight(await vessel.getSurfaceReferenceFrame());
const centerOfBuoyancy = await flight.getCenterOfBuoyancy();
const centerOfMass = await flight.getCenterOfMass();
Flight.center_of_buoyancy is null when no part of the vessel is in the water, or the body
has no ocean.
The couple with the hull fully submerged is also available as a torque about the center of mass. It is zero when the fully submerged center of buoyancy lies directly above or below the center of mass:
torque = flight.buoyant_torque_at(body.ocean_density, body.surface_gravity)
var torque = flight.BuoyantTorqueAt (
body.OceanDensity, body.SurfaceGravity);
auto torque = flight.buoyant_torque_at(body.ocean_density(),
body.surface_gravity());
double density, gravity;
krpc_SpaceCenter_CelestialBody_OceanDensity(conn, &density, body);
krpc_SpaceCenter_CelestialBody_SurfaceGravity(conn, &gravity, body);
krpc_tuple_double_double_double_t torque;
krpc_SpaceCenter_Flight_BuoyantTorqueAt(conn, &torque, flight, density,
gravity);
Triplet<Double, Double, Double> torque = flight.buoyantTorqueAt(
body.getOceanDensity(), body.getSurfaceGravity());
local torque =
flight:buoyant_torque_at(body.ocean_density, body.surface_gravity)
const torque = await flight.buoyantTorqueAt(
await body.getOceanDensity(),
await body.getSurfaceGravity(),
);
Flight.buoyant_torque_at() is the torque with the hull fully submerged. It holds
steady while the hull rides the waves, where Flight.buoyant_torque follows the
live waterline.
In the Editor#
A hull can be trimmed before it is launched. EditorVessel.flight() returns a
SimulatedFlight, which has the fully submerged members of Flight. Its
SimulatedFlight.surface_reference_frame sets which way is up. Here a hull built
nose-up in the vehicle assembly building is simulated floating on its bottom:
vessel = space_center.editor.vessel
flight = vessel.flight(vessel.reference_frame)
# The vessel frame's z axis points out of the bottom, so up is its -z axis
side = math.sqrt(0.5)
flight.surface_reference_frame = (
space_center.ReferenceFrame.create_relative(
vessel.reference_frame, rotation=(0, side, 0, side)
)
)
torque = flight.buoyant_torque_at(body.ocean_density, body.surface_gravity)
var vessel = spaceCenter.Editor.Vessel;
var flight = vessel.Flight (vessel.ReferenceFrame);
// The vessel frame's z axis points out of the bottom, so up is
// its -z axis
var side = Math.Sqrt (0.5);
flight.SurfaceReferenceFrame = ReferenceFrame.CreateRelative (
connection, vessel.ReferenceFrame,
rotation: Tuple.Create (0.0, side, 0.0, side));
var torque = flight.BuoyantTorqueAt (
body.OceanDensity, body.SurfaceGravity);
auto vessel = space_center.editor().vessel();
auto flight = vessel.flight(vessel.reference_frame());
// The vessel frame's z axis points out of the bottom, so up is its -z
// axis
double side = std::sqrt(0.5);
flight.set_surface_reference_frame(ReferenceFrame::create_relative(
conn, vessel.reference_frame(), std::make_tuple(0.0, 0.0, 0.0),
std::make_tuple(0.0, side, 0.0, side)));
auto torque = flight.buoyant_torque_at(body.ocean_density(),
body.surface_gravity());
double density, gravity;
krpc_SpaceCenter_CelestialBody_OceanDensity(conn, &density, body);
krpc_SpaceCenter_CelestialBody_SurfaceGravity(conn, &gravity, body);
krpc_SpaceCenter_Editor_t editor;
krpc_SpaceCenter_Editor(conn, &editor);
krpc_SpaceCenter_EditorVessel_t vessel;
krpc_SpaceCenter_Editor_Vessel(conn, &vessel, editor);
krpc_SpaceCenter_ReferenceFrame_t vessel_frame;
krpc_SpaceCenter_EditorVessel_ReferenceFrame(conn, &vessel_frame, vessel);
krpc_SpaceCenter_SimulatedFlight_t flight;
krpc_SpaceCenter_EditorVessel_Flight(conn, &flight, vessel, vessel_frame);
// The vessel frame's z axis points out of the bottom, so up is its -z
// axis
double side = sqrt(0.5);
krpc_tuple_double_double_double_t zero = {0, 0, 0};
krpc_tuple_double_double_double_double_t rotation = {0, side, 0, side};
krpc_SpaceCenter_ReferenceFrame_t up_frame;
krpc_SpaceCenter_ReferenceFrame_CreateRelative(
conn, &up_frame, vessel_frame, &zero, &rotation, &zero, &zero);
krpc_SpaceCenter_SimulatedFlight_set_SurfaceReferenceFrame(conn, flight,
up_frame);
krpc_tuple_double_double_double_t torque;
krpc_SpaceCenter_SimulatedFlight_BuoyantTorqueAt(conn, &torque, flight,
density, gravity);
EditorVessel vessel = spaceCenter.getEditor().getVessel();
SimulatedFlight flight = vessel.flight(vessel.getReferenceFrame());
// The vessel frame's z axis points out of the bottom, so up is its
// -z axis
double side = Math.sqrt(0.5);
Triplet<Double, Double, Double> zero = new Triplet<>(0.0, 0.0, 0.0);
flight.setSurfaceReferenceFrame(ReferenceFrame.createRelative(
connection, vessel.getReferenceFrame(), zero,
new Quartet<>(0.0, side, 0.0, side), zero, zero));
Triplet<Double, Double, Double> torque = flight.buoyantTorqueAt(
body.getOceanDensity(), body.getSurfaceGravity());
local vessel = space_center.editor.vessel
local flight = vessel:flight(vessel.reference_frame)
-- The vessel frame's z axis points out of the bottom, so up is its -z axis
local side = math.sqrt(0.5)
flight.surface_reference_frame =
space_center.ReferenceFrame.create_relative(
vessel.reference_frame, {0, 0, 0}, {0, side, 0, side})
local torque =
flight:buoyant_torque_at(body.ocean_density, body.surface_gravity)
const vessel = await (await spaceCenter.getEditor()).getVessel();
const vesselFrame = await vessel.getReferenceFrame();
const flight = await vessel.flight(vesselFrame);
// The vessel frame's z axis points out of the bottom, so up is its -z axis
const side = Math.sqrt(0.5);
await flight.setSurfaceReferenceFrame(
await spaceCenter.ReferenceFrame.createRelative(vesselFrame, {
rotation: [0, side, 0, side],
}),
);
const torque = await flight.buoyantTorqueAt(
await body.getOceanDensity(),
await body.getSurfaceGravity(),
);
Gravity acts along the negative x axis of the surface reference frame. A frame created
relative to EditorVessel.reference_frame follows the hull as it is edited.
Submersion#
Flight.submerged_portion is the fraction of the vessel below the waterline, and
Flight.depth is how far its center of mass sits below the surface. The same pair
is available per part, alongside Part.splashed and the depth of the part’s
shallowest and deepest points.
Pressure#
Below the waterline, Flight.static_pressure and Part.static_pressure
include the weight of the water. With part pressure limits enabled in the difficulty
settings, a part is destroyed when its static plus dynamic pressure exceeds
Part.max_pressure:
for part in vessel.parts.all:
if part.static_pressure + part.dynamic_pressure > part.max_pressure:
print(part.title, "is over its pressure limit")
foreach (var part in vessel.Parts.All)
if (part.StaticPressure + part.DynamicPressure >
part.MaxPressure)
Console.WriteLine (
"{0} is over its pressure limit", part.Title);
for (auto part : vessel.parts().all())
if (part.static_pressure() + part.dynamic_pressure() >
part.max_pressure())
std::cout << part.title() << " is over its pressure limit"
<< std::endl;
krpc_list_object_t all = KRPC_NULL_LIST;
krpc_SpaceCenter_Parts_All(conn, &all, parts);
for (size_t i = 0; i < all.size; i++) {
krpc_SpaceCenter_Part_t part = all.items[i];
float static_pressure, dynamic_pressure;
double max_pressure;
krpc_SpaceCenter_Part_StaticPressure(conn, &static_pressure, part);
krpc_SpaceCenter_Part_DynamicPressure(conn, &dynamic_pressure, part);
krpc_SpaceCenter_Part_MaxPressure(conn, &max_pressure, part);
if (static_pressure + dynamic_pressure > max_pressure) {
char* title = NULL;
krpc_SpaceCenter_Part_Title(conn, &title, part);
printf("%s is over its pressure limit\n", title);
krpc_free(title);
}
}
KRPC_FREE_LIST(all);
for (Part part : vessel.getParts().getAll()) {
if (part.getStaticPressure() + part.getDynamicPressure()
> part.getMaxPressure()) {
System.out.println(
part.getTitle() + " is over its pressure limit");
}
}
for _, part in ipairs(vessel.parts.all) do
if part.static_pressure + part.dynamic_pressure > part.max_pressure then
print(part.title .. ' is over its pressure limit')
end
end
for (const part of await (await vessel.getParts()).getAll()) {
const pressure =
(await part.getStaticPressure()) + (await part.getDynamicPressure());
if (pressure > (await part.getMaxPressure())) {
console.log(await part.getTitle(), "is over its pressure limit");
}
}
The loop reads the parts with separate calls, which can land in different physics ticks. A server side function reads every part in the same tick, with a single call:
over_limit = conn.run(
lambda: [
part.title
for part in vessel.parts.all
if part.static_pressure + part.dynamic_pressure > part.max_pressure
]
)
var overLimit = connection.Run (
() => vessel.Parts.All
.Where (part =>
part.StaticPressure + part.DynamicPressure >
part.MaxPressure)
.Select (part => part.Title)
.ToList ());
auto e = Expr::bind(conn);
auto t = KType::bind(conn);
auto part = e.parameter("part", t.class_type("SpaceCenter", "Part"));
// A property of the part parameter
auto get = [&](const krpc::schema::ProcedureCall& property) {
return e.call_with_arguments(property,
std::map<int32_t, Expr>({{0, part}}));
};
// A function of the part parameter
auto function = [&](const Expr& body) {
return e.lambda(std::vector<Expr>({part}), body);
};
auto over =
e.greater_than(e.add(get(Part::static_pressure_call_template()),
get(Part::dynamic_pressure_call_template())),
get(Part::max_pressure_call_template()));
auto titles =
e.select(e.where(e.call(vessel.parts().all_call()), function(over)),
function(get(Part::title_call_template())));
auto over_limit = krpc::run<std::vector<std::string>>(e.to_list(titles));
krpc_KRPC_Type_t part_type;
krpc_KRPC_Type_ClassTypeById(conn, &part_type,
KRPC_CLASS_SPACECENTER_PART);
krpc_KRPC_Expression_t part;
krpc_KRPC_Expression_Parameter(conn, &part, "part", part_type);
// The parts over their pressure limit
krpc_KRPC_Expression_t static_pressure, dynamic_pressure, max_pressure;
get(conn, &static_pressure, part,
krpc_SpaceCenter_Part_StaticPressure_call_template);
get(conn, &dynamic_pressure, part,
krpc_SpaceCenter_Part_DynamicPressure_call_template);
get(conn, &max_pressure, part,
krpc_SpaceCenter_Part_MaxPressure_call_template);
krpc_KRPC_Expression_t pressure, over, is_over;
krpc_KRPC_Expression_Add(conn, &pressure, static_pressure,
dynamic_pressure);
krpc_KRPC_Expression_GreaterThan(conn, &over, pressure, max_pressure);
function(conn, &is_over, part, over);
krpc_schema_ProcedureCall all_call;
krpc_SpaceCenter_Parts_All_call(&all_call, parts);
krpc_KRPC_Expression_t all_parts, over_parts;
krpc_KRPC_Expression_Call(conn, &all_parts, &all_call);
krpc_free_procedure_call(&all_call);
krpc_KRPC_Expression_Where(conn, &over_parts, all_parts, is_over);
// Their titles
krpc_KRPC_Expression_t title, title_of, titles, expr;
get(conn, &title, part, krpc_SpaceCenter_Part_Title_call_template);
function(conn, &title_of, part, title);
krpc_KRPC_Expression_Select(conn, &titles, over_parts, title_of);
krpc_KRPC_Expression_ToList(conn, &expr, titles);
krpc_bytes_t data = KRPC_BYTES_NULL;
krpc_run_function(conn, &data, NULL, expr, NULL);
krpc_list_string_t over_limit = KRPC_NULL_LIST;
pb_istream_t stream = pb_istream_from_buffer(data.data, data.size);
krpc_decode_list_string(&stream, &over_limit);
KRPC_FREE_BYTES(data);
Expression.Bound e = Expression.bind(connection);
Type.Bound t = Type.bind(connection);
Expression part =
e.parameter("part", t.classType("SpaceCenter", "Part"));
Expression over = e.greaterThan(
e.add(property(connection, e, part, "getStaticPressure"),
property(connection, e, part, "getDynamicPressure")),
property(connection, e, part, "getMaxPressure"));
Expression titles = e.select(
e.where(
e.call(connection.getCall(vessel.getParts(), "getAll")),
e.lambda(Arrays.asList(part), over)),
e.lambda(Arrays.asList(part),
property(connection, e, part, "getTitle")));
List<String> overLimit = connection.run(e.toList(titles));
local Expression = conn.krpc.Expression
local Part = conn.space_center.Part
local part = Expression.parameter(
'part', conn.krpc.Type.class_type('SpaceCenter', 'Part'))
-- A property of the part parameter
local function get(property)
return Expression.call_with_arguments(property, Map{[0] = part})
end
-- A function of the part parameter
local function fn(body)
return Expression.lambda({part}, body)
end
local over = Expression.greater_than(
Expression.add(
get(Part.get_static_pressure), get(Part.get_dynamic_pressure)),
get(Part.get_max_pressure))
local titles = Expression.select(
Expression.where(
Expression.call(conn:get_call(parts.get_all, parts)), fn(over)),
fn(get(Part.get_title)))
local over_limit = conn:run(Expression.to_list(titles))
const overLimit = await conn.run(
({ vessel }) =>
vessel.parts.all
.filter(
(part) =>
part.staticPressure + part.dynamicPressure > part.maxPressure,
)
.map((part) => part.title),
{ vessel },
);
CelestialBody.pressure_at() takes a negative altitude for a point under the sea, to
plan how deep a dive can go.
Example#
This script reports whether the active vessel floats, and how far its center of buoyancy sits from its center of mass:
import math
import krpc
conn = krpc.connect(name="Buoyancy Trim")
vessel = conn.space_center.active_vessel
body = vessel.orbit.body
if not body.has_ocean:
raise RuntimeError(body.name + " has no ocean")
flight = vessel.flight(vessel.surface_reference_frame)
# The fully submerged figures, which do not depend on where the vessel is
buoyancy = flight.buoyant_force_at(body.ocean_density, body.surface_gravity)
torque = flight.buoyant_torque_at(body.ocean_density, body.surface_gravity)
weight = vessel.mass * body.surface_gravity
print("Displacement: %.1f m^3" % flight.displacement)
print("Buoyancy: %.0f N fully submerged" % buoyancy)
print("Weight: %.0f N" % weight)
print("Floats: %s" % str(buoyancy > weight).lower())
print("Torque: %.0f N m fully submerged" % math.hypot(*torque))
# The trim, read in the surface reference frame. Its x axis points up, so
# the y and z axes span the horizontal. A horizontal offset between the two
# centers tips the hull until they line up again.
center_of_buoyancy = flight.center_of_buoyancy
if center_of_buoyancy is None:
print("Trim: vessel is out of the water")
else:
center_of_mass = flight.center_of_mass
north = center_of_buoyancy[1] - center_of_mass[1]
east = center_of_buoyancy[2] - center_of_mass[2]
print("Submerged: %.0f%%" % (100 * flight.submerged_portion))
print("Trim: %.2f m horizontally" % math.hypot(north, east))
print("Trim north: %+.2f m" % north)
print("Trim east: %+.2f m" % east)
using System;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;
class BuoyancyTrim {
public static void Main () {
using (var connection = new Connection ("Buoyancy Trim")) {
var vessel = connection.SpaceCenter ().ActiveVessel;
var body = vessel.Orbit.Body;
if (!body.HasOcean)
throw new InvalidOperationException (
body.Name + " has no ocean");
var flight = vessel.Flight (vessel.SurfaceReferenceFrame);
// The fully submerged figures, which do not depend on where
// the vessel is
var buoyancy = flight.BuoyantForceAt (
body.OceanDensity, body.SurfaceGravity);
var torque = flight.BuoyantTorqueAt (
body.OceanDensity, body.SurfaceGravity);
var weight = vessel.Mass * body.SurfaceGravity;
var torqueSize = Math.Sqrt (torque.Item1 * torque.Item1 +
torque.Item2 * torque.Item2 +
torque.Item3 * torque.Item3);
Console.WriteLine ("Displacement: {0:F1} m^3",
flight.Displacement);
Console.WriteLine ("Buoyancy: {0:F0} N fully submerged",
buoyancy);
Console.WriteLine ("Weight: {0:F0} N", weight);
Console.WriteLine ("Floats: {0}",
buoyancy > weight ? "true" : "false");
Console.WriteLine ("Torque: {0:F0} N m fully submerged",
torqueSize);
// The trim, read in the surface reference frame. Its x axis
// points up, so the y and z axes span the horizontal. A
// horizontal offset between the two centers tips the hull
// until they line up again.
var centerOfBuoyancy = flight.CenterOfBuoyancy;
if (centerOfBuoyancy == null) {
Console.WriteLine (
"Trim: vessel is out of the water");
} else {
var centerOfMass = flight.CenterOfMass;
var north = centerOfBuoyancy.Item2 - centerOfMass.Item2;
var east = centerOfBuoyancy.Item3 - centerOfMass.Item3;
Console.WriteLine ("Submerged: {0:F0}%",
100 * flight.SubmergedPortion);
Console.WriteLine ("Trim: {0:F2} m horizontally",
Math.Sqrt (north * north + east * east));
Console.WriteLine ("Trim north: {0:+0.00;-0.00} m",
north);
Console.WriteLine ("Trim east: {0:+0.00;-0.00} m",
east);
}
}
}
}
#include <cmath>
#include <cstdio>
#include <krpc.hpp>
#include <krpc/services/space_center.hpp>
#include <stdexcept>
#include <tuple>
int main() {
auto conn = krpc::connect("Buoyancy Trim");
krpc::services::SpaceCenter space_center(conn);
auto vessel = space_center.active_vessel().value();
auto body = vessel.orbit().body();
if (!body.has_ocean())
throw std::runtime_error(body.name() + " has no ocean");
auto flight = vessel.flight(vessel.surface_reference_frame());
// The fully submerged figures, which do not depend on where the vessel
// is
auto buoyancy =
flight.buoyant_force_at(body.ocean_density(), body.surface_gravity());
auto torque = flight.buoyant_torque_at(body.ocean_density(),
body.surface_gravity());
auto weight = vessel.mass() * body.surface_gravity();
std::printf("Displacement: %.1f m^3\n", flight.displacement());
std::printf("Buoyancy: %.0f N fully submerged\n", buoyancy);
std::printf("Weight: %.0f N\n", weight);
std::printf("Floats: %s\n", buoyancy > weight ? "true" : "false");
std::printf("Torque: %.0f N m fully submerged\n",
std::hypot(std::get<0>(torque), std::get<1>(torque),
std::get<2>(torque)));
// The trim, read in the surface reference frame. Its x axis points up,
// so the y and z axes span the horizontal. A horizontal offset between
// the two centers tips the hull until they line up again.
auto center_of_buoyancy = flight.center_of_buoyancy();
if (!center_of_buoyancy) {
std::printf("Trim: vessel is out of the water\n");
} else {
auto center_of_mass = flight.center_of_mass();
auto north =
std::get<1>(*center_of_buoyancy) - std::get<1>(center_of_mass);
auto east =
std::get<2>(*center_of_buoyancy) - std::get<2>(center_of_mass);
std::printf("Submerged: %.0f%%\n",
100 * flight.submerged_portion());
std::printf("Trim: %.2f m horizontally\n",
std::hypot(north, east));
std::printf("Trim north: %+.2f m\n", north);
std::printf("Trim east: %+.2f m\n", east);
}
}
#include <krpc_cnano.h>
#include <krpc_cnano/services/space_center.h>
#include <math.h>
#include <stdio.h>
int main() {
krpc_connection_t conn;
if (krpc_open_serial(&conn, "COM0") != KRPC_OK) return 1;
if (krpc_connect(conn, "Buoyancy Trim") != KRPC_OK) return 1;
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);
krpc_SpaceCenter_Orbit_t orbit;
krpc_SpaceCenter_Vessel_Orbit(conn, &orbit, vessel);
krpc_SpaceCenter_CelestialBody_t body;
krpc_SpaceCenter_Orbit_Body(conn, &body, orbit);
bool has_ocean;
krpc_SpaceCenter_CelestialBody_HasOcean(conn, &has_ocean, body);
if (!has_ocean) {
char* name = NULL;
krpc_SpaceCenter_CelestialBody_Name(conn, &name, body);
fprintf(stderr, "%s has no ocean\n", name);
krpc_free(name);
krpc_disconnect(conn);
return 1;
}
krpc_SpaceCenter_ReferenceFrame_t frame;
krpc_SpaceCenter_Vessel_SurfaceReferenceFrame(conn, &frame, vessel);
krpc_SpaceCenter_Flight_t flight;
krpc_SpaceCenter_Vessel_Flight(conn, &flight, vessel, frame);
// The fully submerged figures, which do not depend on where the vessel
// is
double density, gravity, buoyancy, displacement;
krpc_SpaceCenter_CelestialBody_OceanDensity(conn, &density, body);
krpc_SpaceCenter_CelestialBody_SurfaceGravity(conn, &gravity, body);
krpc_SpaceCenter_Flight_BuoyantForceAt(conn, &buoyancy, flight, density,
gravity);
krpc_tuple_double_double_double_t torque;
krpc_SpaceCenter_Flight_BuoyantTorqueAt(conn, &torque, flight, density,
gravity);
float mass;
krpc_SpaceCenter_Vessel_Mass(conn, &mass, vessel);
double weight = mass * gravity;
krpc_SpaceCenter_Flight_Displacement(conn, &displacement, flight);
printf("Displacement: %.1f m^3\n", displacement);
printf("Buoyancy: %.0f N fully submerged\n", buoyancy);
printf("Weight: %.0f N\n", weight);
printf("Floats: %s\n", buoyancy > weight ? "true" : "false");
printf("Torque: %.0f N m fully submerged\n",
sqrt(torque.e0 * torque.e0 + torque.e1 * torque.e1 +
torque.e2 * torque.e2));
// The trim, read in the surface reference frame. Its x axis points up,
// so the y and z axes span the horizontal. A horizontal offset between
// the two centers tips the hull until they line up again.
krpc_tuple_double_double_double_t center_of_buoyancy;
bool out_of_water;
krpc_SpaceCenter_Flight_CenterOfBuoyancy(conn, ¢er_of_buoyancy,
&out_of_water, flight);
if (out_of_water) {
printf("Trim: vessel is out of the water\n");
} else {
krpc_tuple_double_double_double_t center_of_mass;
krpc_SpaceCenter_Flight_CenterOfMass(conn, ¢er_of_mass, flight);
double north = center_of_buoyancy.e1 - center_of_mass.e1;
double east = center_of_buoyancy.e2 - center_of_mass.e2;
double submerged;
krpc_SpaceCenter_Flight_SubmergedPortion(conn, &submerged, flight);
printf("Submerged: %.0f%%\n", 100 * submerged);
printf("Trim: %.2f m horizontally\n",
sqrt(north * north + east * east));
printf("Trim north: %+.2f m\n", north);
printf("Trim east: %+.2f m\n", east);
}
krpc_disconnect(conn);
}
import krpc.client.Connection;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.CelestialBody;
import krpc.client.services.SpaceCenter.Flight;
import krpc.client.services.SpaceCenter.Vessel;
import org.javatuples.Triplet;
public class BuoyancyTrim {
public static void main(String[] args) {
try (Connection connection = Connection.newInstance("Buoyancy Trim")) {
Vessel vessel =
SpaceCenter.newInstance(connection).getActiveVessel();
CelestialBody body = vessel.getOrbit().getBody();
if (!body.getHasOcean()) {
throw new IllegalStateException(body.getName() + " has no ocean");
}
Flight flight = vessel.flight(vessel.getSurfaceReferenceFrame());
// The fully submerged figures, which do not depend on where the
// vessel is
double density = body.getOceanDensity();
double gravity = body.getSurfaceGravity();
double buoyancy = flight.buoyantForceAt(density, gravity);
Triplet<Double, Double, Double> torque =
flight.buoyantTorqueAt(density, gravity);
double weight = vessel.getMass() * gravity;
double torqueSize = Math.sqrt(
torque.getValue0() * torque.getValue0()
+ torque.getValue1() * torque.getValue1()
+ torque.getValue2() * torque.getValue2());
System.out.printf("Displacement: %.1f m^3%n",
flight.getDisplacement());
System.out.printf("Buoyancy: %.0f N fully submerged%n",
buoyancy);
System.out.printf("Weight: %.0f N%n", weight);
System.out.printf("Floats: %s%n", buoyancy > weight);
System.out.printf("Torque: %.0f N m fully submerged%n",
torqueSize);
// The trim, read in the surface reference frame. Its x axis points
// up, so the y and z axes span the horizontal. A horizontal offset
// between the two centers tips the hull until they line up again.
Triplet<Double, Double, Double> centerOfBuoyancy =
flight.getCenterOfBuoyancy();
if (centerOfBuoyancy == null) {
System.out.println("Trim: vessel is out of the water");
} else {
Triplet<Double, Double, Double> centerOfMass =
flight.getCenterOfMass();
double north = centerOfBuoyancy.getValue1()
- centerOfMass.getValue1();
double east = centerOfBuoyancy.getValue2()
- centerOfMass.getValue2();
System.out.printf("Submerged: %.0f%%%n",
100 * flight.getSubmergedPortion());
System.out.printf("Trim: %.2f m horizontally%n",
Math.hypot(north, east));
System.out.printf("Trim north: %+.2f m%n", north);
System.out.printf("Trim east: %+.2f m%n", east);
}
}
}
}
local krpc = require 'krpc'
local conn = krpc.connect('Buoyancy Trim')
local vessel = conn.space_center.active_vessel
local body = vessel.orbit.body
if not body.has_ocean then
error(body.name .. ' has no ocean')
end
local flight = vessel:flight(vessel.surface_reference_frame)
-- The fully submerged figures, which do not depend on where the vessel is
local buoyancy =
flight:buoyant_force_at(body.ocean_density, body.surface_gravity)
local torque =
flight:buoyant_torque_at(body.ocean_density, body.surface_gravity)
local weight = vessel.mass * body.surface_gravity
print(string.format('Displacement: %.1f m^3', flight.displacement))
print(string.format('Buoyancy: %.0f N fully submerged', buoyancy))
print(string.format('Weight: %.0f N', weight))
print(string.format('Floats: %s', tostring(buoyancy > weight)))
print(string.format('Torque: %.0f N m fully submerged',
math.sqrt(torque[1]^2 + torque[2]^2 + torque[3]^2)))
-- The trim, read in the surface reference frame. Its x axis points up, so
-- the y and z axes span the horizontal. A horizontal offset between the two
-- centers tips the hull until they line up again.
local center_of_buoyancy = flight.center_of_buoyancy
if center_of_buoyancy == nil then
print('Trim: vessel is out of the water')
else
local center_of_mass = flight.center_of_mass
local north = center_of_buoyancy[2] - center_of_mass[2]
local east = center_of_buoyancy[3] - center_of_mass[3]
print(string.format('Submerged: %.0f%%',
100 * flight.submerged_portion))
print(string.format('Trim: %.2f m horizontally',
math.sqrt(north^2 + east^2)))
print(string.format('Trim north: %+.2f m', north))
print(string.format('Trim east: %+.2f m', east))
end
import { connect } from "@krpc-org/krpc";
const conn = await connect({ name: "Buoyancy Trim" });
const vessel = await conn.spaceCenter.getActiveVessel();
const body = await (await vessel.getOrbit()).getBody();
if (!(await body.getHasOcean())) {
throw new Error(`${await body.getName()} has no ocean`);
}
const flight = await vessel.flight(await vessel.getSurfaceReferenceFrame());
const density = await body.getOceanDensity();
const gravity = await body.getSurfaceGravity();
// The fully submerged figures, which do not depend on where the vessel is
const buoyancy = await flight.buoyantForceAt(density, gravity);
const torque = await flight.buoyantTorqueAt(density, gravity);
const weight = (await vessel.getMass()) * gravity;
const displacement = await flight.getDisplacement();
console.log(`Displacement: ${displacement.toFixed(1)} m^3`);
console.log(`Buoyancy: ${buoyancy.toFixed(0)} N fully submerged`);
console.log(`Weight: ${weight.toFixed(0)} N`);
console.log(`Floats: ${buoyancy > weight}`);
console.log(
`Torque: ${Math.hypot(...torque).toFixed(0)} N m fully submerged`,
);
// The trim, read in the surface reference frame. Its x axis points up, so
// the y and z axes span the horizontal. A horizontal offset between the two
// centers tips the hull until they line up again.
const centerOfBuoyancy = await flight.getCenterOfBuoyancy();
/** @param {number} x */
const signed = (x) => (x >= 0 ? "+" : "") + x.toFixed(2);
if (centerOfBuoyancy === null) {
console.log("Trim: vessel is out of the water");
} else {
const centerOfMass = await flight.getCenterOfMass();
const north = centerOfBuoyancy[1] - centerOfMass[1];
const east = centerOfBuoyancy[2] - centerOfMass[2];
const submerged = 100 * (await flight.getSubmergedPortion());
console.log(`Submerged: ${submerged.toFixed(0)}%`);
console.log(
`Trim: ${Math.hypot(north, east).toFixed(2)} m horizontally`,
);
console.log(`Trim north: ${signed(north)} m`);
console.log(`Trim east: ${signed(east)} m`);
}
await conn.close();