载入中...
搜索中...
未找到
OceanBoatWake.cpp
浏览该文件的文档.
1#include "fluids/OceanBoat.h"
2#include "fluids/OceanWake.h"
3
4#include <algorithm>
5#include <cmath>
6
7namespace eve::fluids {
9 const auto invalid = [] {
11 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid boat wake receipt", "fluids.ocean.wake"));
12 };
13 if (!std::isfinite(dt) || dt <= 0.0 || dt > 1.0 / 30.0 || receipt.contacts.size() > 64 ||
14 !std::isfinite(receipt.time) || std::abs(receipt.time - wake.time()) > 1e-8)
15 return invalid();
16 const auto settings = wake.settings();
17 const double cell = settings.length / settings.resolution;
18 double displacedVolume = 0.0;
19 // Validate the whole receipt before mutating the field. All contact cells share
20 // one forcing budget: treating every cell as a complete moving source saturated
21 // the velocity/foam clamps on large hulls and erased their divergent wave train.
22 for (const auto& contact : receipt.contacts) {
23 const auto p = contact.position, v = contact.relativeVelocity;
24 if (!std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z) || !std::isfinite(v.x) ||
25 !std::isfinite(v.y) || !std::isfinite(v.z) || !std::isfinite(contact.submergedVolume) ||
26 contact.submergedVolume < 0.0 || contact.submergedVolume > 1e6 || glm::length(v) > 1e4)
27 return invalid();
28 displacedVolume += contact.submergedVolume;
29 }
30 const double supportVolume = std::max(displacedVolume, cell * cell * cell);
31 glm::dvec2 center(0.0), heading(0.0);
32 for (const auto& contact : receipt.contacts) {
33 center += glm::dvec2(contact.position.x, contact.position.z) * contact.submergedVolume;
34 heading += glm::dvec2(contact.relativeVelocity.x, contact.relativeVelocity.z) * contact.submergedVolume;
35 }
36 const bool underway = displacedVolume > 0.0 && glm::length(heading) > 1e-6;
37 double front = 0.0, rear = 0.0;
38 if (underway) {
40 heading = glm::normalize(heading);
41 for (const auto& contact : receipt.contacts) {
42 if (contact.submergedVolume == 0.0) continue;
43 const double along = glm::dot(glm::dvec2(contact.position.x, contact.position.z) - center, heading);
44 front = std::max(front, along);
45 rear = std::min(rear, along);
46 }
47 }
48 // A moving pressure source at the leading end radiates a divergent wave
49 // train. Repeating that source along the hull cancels the outward waves and
50 // instead produces long, nearly uniform ridges against both hull sides.
51 const double bowWidth = std::max(cell, (front - rear) * 0.10);
52 const auto bowWeight = [&](const OceanBoatContact& contact) {
53 if (!underway) return 1.0;
54 const double aft = front - glm::dot(glm::dvec2(contact.position.x, contact.position.z) - center, heading);
55 return std::exp(-0.5 * aft * aft / (bowWidth * bowWidth));
56 };
57 double bowVolume = 0.0;
58 for (const auto& contact : receipt.contacts) bowVolume += contact.submergedVolume * bowWeight(contact);
59 const double bowSupport = std::max(bowVolume, cell * cell * cell);
60 std::vector<OceanWakeImpulse> impulses;
61 impulses.reserve(receipt.contacts.size() * 2);
62 for (const auto& contact : receipt.contacts) {
63 const auto p = contact.position, v = contact.relativeVelocity;
64 if (contact.submergedVolume == 0.0) continue;
65 const double radius = std::clamp(std::cbrt(contact.submergedVolume), cell, settings.length * 0.25);
66 const double scale = dt * contact.submergedVolume / supportVolume;
67 const double bowScale = dt * contact.submergedVolume * bowWeight(contact) / bowSupport;
68 const double speed = std::hypot(v.x, v.z);
69 const double foam = std::clamp(scale * (speed + std::abs(v.y)) * 0.005, 0.0, 1.0);
70 if (speed > 1e-6) impulses.push_back({{p.x, p.z}, radius, std::min(bowScale * speed, 0.5), {v.x, v.z}, foam});
71 if (std::abs(v.y) > 1e-6)
72 impulses.push_back({{p.x, p.z}, radius, std::clamp(-scale * v.y, -0.5, 0.5), {0.0, 0.0}, foam});
73 }
74 return wake.inject(impulses);
75}
76} // namespace eve::fluids
glm::vec4 p[6]
float displacedVolume
float v
std::array< float, 3 > scale
double radius
Definition MeleeHit.cpp:42
OceanWakeSettings settings
ssq::Object wake
glm::dvec2 center
double dt
Cell cell
V3 heading
Definition TreeMesh.cpp:429
Contact contact
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 failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Caller-owned wave field with finite-depth gravity dispersion and absorbing boundaries....
Definition OceanWake.h:53
Status invalid(const char *message)
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
EVENGINE_API_DOMAINS Result< void > injectOceanBoatWake(OceanWake &wake, const OceanBoatReceipt &receipt, double dt)
Convert wet hull motion to bounded bow/stern dipoles and vertical impacts in a local wave field.
Owning wet hull sample for wake/foam consumers; no rigid-body pointer escapes.
Definition OceanBoat.h:30
Owning fixed-step force receipt; consumers may retain it for wake injection.
Definition OceanBoat.h:36
std::vector< OceanBoatContact > contacts
Definition OceanBoat.h:40