载入中...
搜索中...
未找到
OceanBoat.cpp
浏览该文件的文档.
1#include "fluids/OceanBoat.h"
2
3#include "physics/Body3D.h"
5#include "physics/World3D.h"
6
7#include <algorithm>
8#include <cmath>
9#include <glm/gtc/quaternion.hpp>
10#include <limits>
11#include <numbers>
12
13namespace eve::fluids {
17 double lastTime = -std::numeric_limits<double>::infinity();
18};
19namespace {
20bool finite(glm::dvec3 v) { return std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z); }
21template <class T>
22Result<T> failure(DiagnosticCode code, const char* message) {
23 return Result<T>::failure(Diagnostic::error(code, message, "fluids.ocean.boat"));
24}
25glm::dvec3 vector(physics::PhysicsVector3D v) { return {v.x, v.y, v.z}; }
26} // namespace
27
28OceanBoat::OceanBoat(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
29OceanBoat::~OceanBoat() = default;
30
32 if (s.cells.empty() || s.cells.size() > 64 || !std::isfinite(s.waterDensity) || s.waterDensity <= 0.0 ||
33 s.waterDensity > 1e5 || !std::isfinite(s.gravity) || s.gravity <= 0.0 || s.gravity > 100.0 || !finite(s.drag) ||
34 glm::any(glm::lessThan(s.drag, glm::dvec3(0.0))) || glm::length(s.drag) > 100.0 || !finite(s.propeller) ||
35 glm::length(s.propeller) > 1000.0 || !std::isfinite(s.thrust) || s.thrust < 0.0 || s.thrust > 1e8 ||
36 !std::isfinite(s.rudderCoefficient) || s.rudderCoefficient < 0.0 || s.rudderCoefficient > 1e6)
38 for (const auto& c : s.cells) {
39 if (!finite(c.center) || glm::length(c.center) > 1000.0 || !std::isfinite(c.radius) || c.radius <= 0.0 ||
40 c.radius > 100.0 || !std::isfinite(c.volume) || c.volume <= 0.0 || c.volume > 1e6)
41 return failure<std::unique_ptr<OceanBoat>>(DiagnosticCode::InvalidArgument, "Invalid hull cell");
42 }
44 if (!link) return Result<std::unique_ptr<OceanBoat>>::failure(link.status());
45 if (body.getType() != "dynamic" || !std::isfinite(body.getMass()) || body.getMass() <= 0.0F)
47 "Boat requires a dynamic body with positive mass");
48 auto impl = std::make_unique<Impl>();
49 impl->link = link.value();
50 impl->hull = s;
51 return Result<std::unique_ptr<OceanBoat>>::success(std::unique_ptr<OceanBoat>(new OceanBoat(std::move(impl))));
52}
53
54Result<OceanBoatReceipt> OceanBoat::apply(const OceanWaterView& frame, double dt, double throttle, double rudder) {
55 if (!std::isfinite(dt) || dt <= 0.0 || dt > 1.0 / 30.0 || !std::isfinite(throttle) || std::abs(throttle) > 1.0 ||
56 !std::isfinite(rudder) || std::abs(rudder) > 1.0 || frame.time() <= impl_->lastTime)
57 return failure<OceanBoatReceipt>(DiagnosticCode::InvalidArgument, "Invalid or repeated boat step");
58 auto* world = physics::World3D::findWorld(impl_->link.world);
59 if (!world) return failure<OceanBoatReceipt>(DiagnosticCode::StaleHandle, "Boat world no longer exists");
60 auto resolved = impl_->link.resolve(*world);
62 auto& body = *resolved.value();
63 const auto& h = impl_->hull;
64 const glm::dquat rotation{body.getRotW(), body.getRotX(), body.getRotY(), body.getRotZ()};
65 const auto inverse = glm::conjugate(rotation);
66 const double mass = body.getMass();
67 if (!std::isfinite(mass) || mass <= 0.0 || body.getType() != "dynamic")
68 return failure<OceanBoatReceipt>(DiagnosticCode::InvalidArgument, "Boat body is no longer dynamic");
69 OceanBoatReceipt receipt;
70 receipt.time = frame.time();
71 std::vector<OceanCellForce> forces;
72 forces.reserve(h.cells.size() + 1);
73 receipt.contacts.reserve(h.cells.size());
74 for (const auto& cell : h.cells) {
75 auto p = body.localToWorldPointOwned(float(cell.center.x), float(cell.center.y), float(cell.center.z));
76 if (!p) return Result<OceanBoatReceipt>::failure(p.status());
77 auto v = body.getLocalPointVelocityOwned(float(cell.center.x), float(cell.center.y), float(cell.center.z));
78 if (!v) return Result<OceanBoatReceipt>::failure(v.status());
79 const auto position = vector(p.value()), velocity = vector(v.value());
80 auto water = frame.sample({position.x, position.z});
81 if (!water) return Result<OceanBoatReceipt>::failure(water.status());
82 OceanBuoyancyCell probe{position, velocity, cell.radius, cell.volume, 0.0};
83 auto force = evaluateOceanCellForce(probe, water.value(), h.waterDensity, h.gravity);
84 if (!force) return Result<OceanBoatReceipt>::failure(force.status());
85 const auto relative = velocity - water.value().velocity;
86 const auto local = inverse * relative;
87 const double fraction = force.value().submergedVolume / cell.volume;
88 const double area = std::numbers::pi * cell.radius * cell.radius * fraction;
89 auto drag = -0.5 * h.waterDensity * area * h.drag * glm::abs(local) * local;
90 // Cap each cell's dissipative impulse to its share of body momentum.
91 for (int axis = 0; axis < 3; ++axis) {
92 const double bound = mass * std::abs(local[axis]) / (dt * double(h.cells.size()));
93 drag[axis] = std::clamp(drag[axis], -bound, bound);
94 }
95 force.value().force += rotation * drag;
96 receipt.submergedVolume += force.value().submergedVolume;
97 receipt.contacts.push_back({position, relative, force.value().submergedVolume});
98 forces.push_back(force.value());
99 }
100 auto propeller = body.localToWorldPointOwned(float(h.propeller.x), float(h.propeller.y), float(h.propeller.z));
101 if (!propeller) return Result<OceanBoatReceipt>::failure(propeller.status());
102 const auto p = vector(propeller.value());
103 auto water = frame.sample({p.x, p.z});
104 if (!water) return Result<OceanBoatReceipt>::failure(water.status());
105 auto propVelocity =
106 body.getLocalPointVelocityOwned(float(h.propeller.x), float(h.propeller.y), float(h.propeller.z));
108 const auto relative = inverse * (vector(propVelocity.value()) - water.value().velocity);
109 const double wet = std::clamp((water.value().height - p.y) / 0.3, 0.0, 1.0);
110 const double rudderForce =
111 std::clamp(-rudder * h.rudderCoefficient * relative.z * std::abs(relative.z), -mass * 10.0, mass * 10.0);
112 forces.push_back({p, rotation * (glm::dvec3(rudderForce, 0.0, throttle * h.thrust) * wet), 0.0});
113 for (const auto& force : forces) {
114 if (!finite(force.force) || glm::length(force.force) > 1e12)
115 return failure<OceanBoatReceipt>(DiagnosticCode::InvalidArgument, "Hull force exceeds supported range");
116 receipt.totalForce += force.force;
117 }
118 // No fallible operations or external callbacks after the first force is applied.
119 for (const auto& f : forces)
120 body.applyForceAt(float(f.force.x), float(f.force.y), float(f.force.z), float(f.position.x),
121 float(f.position.y), float(f.position.z));
122 impl_->lastTime = frame.time();
123 return Result<OceanBoatReceipt>::success(std::move(receipt));
124}
125} // namespace eve::fluids
Vec3 relative
Definition AnimSmr.cpp:164
const std::string & s
void * impl
glm::vec4 p[6]
ForceVector force
std::string message
DiagnosticCode code
float area
Definition Grass.cpp:62
float v
std::int32_t c
int h
JointPoint2D axis
PhysicsJointLink link
std::string local
std::array< float, 4 > rotation
std::array< float, 3 > position
ssq::Object frame
glm::dvec3 velocity
double dt
ssq::Object water
bool finite
float f
std::string body
RoadNetwork resolved
Cell cell
float fraction
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Water-force adapter over a World3D-owned rigid body, storing generation-qualified links only....
Definition OceanBoat.h:59
EVENGINE_API_DOMAINS Result< OceanBoatReceipt > apply(const OceanWaterView &frame, double dt, double throttle, double rudder)
Submit buoyancy, body-local directional drag, submerged thrust and rudder forces before physics.
Definition OceanBoat.cpp:54
OceanBoat(const OceanBoat &)=delete
static EVENGINE_API_DOMAINS Result< std::unique_ptr< OceanBoat > > create(physics::Body3D &body, const OceanHullSettings &settings)
Validate/copy a bounded hull and attach to a live dynamic body.
Definition OceanBoat.cpp:31
EVENGINE_API_DOMAINS ~OceanBoat()
Synchronous borrowed composition of the spectral sea and an optional local wake. Both owners must out...
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
static World3D * findWorld(PhysicsWorldHandle handle) noexcept
Resolve a live process-local world handle, or null after teardown. @thread Owning physics thread only...
Definition World3D.cpp:296
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
Result< OceanCellForce > evaluateOceanCellForce(const OceanBuoyancyCell &cell, const OceanSurfaceSample &water, double density, double gravity)
Integrate spherical-cap buoyancy and isotropic quadratic water drag.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Owning fixed-step force receipt; consumers may retain it for wake injection.
Definition OceanBoat.h:36
std::vector< OceanBoatContact > contacts
Definition OceanBoat.h:40
physics::PhysicsLink link
Definition OceanBoat.cpp:15
Sphere with assigned displacement volume; several disjoint volumes approximate a hull.
Hull water response; local +Z forward, +X starboard, +Y up.
Definition OceanBoat.h:20
Allocation-free owning three-dimensional physics value.
Definition RigidTypes3D.h:4