载入中...
搜索中...
未找到
WingAssembly.cpp
浏览该文件的文档.
2#include <Box2D/Box2D.h>
3#include <box3d/box3d.h>
4#include <numbers>
5#include <type_traits>
6#include "physics/Body.h"
7#include "physics/Body3D.h"
8#include "physics/World.h"
9#include "physics/World3D.h"
11
12namespace eve::physics {
13namespace {
14using namespace force_detail;
15bool fits(ForceVector v) {
16 constexpr auto limit = std::numeric_limits<float>::max();
17 return isFinite(v) && std::abs(v.x) <= limit && std::abs(v.y) <= limit && std::abs(v.z) <= limit;
18}
19ForceVector point(const Body& body, ForceVector p) {
20 const auto v = body.raw()->GetWorldPoint({float(p.x), float(p.y)});
21 return {v.x, v.y, 0};
22}
23ForceVector point(const Body3D& body, ForceVector p) {
24 const auto v = b3RotateVector(b3Quat{body.getRotX(), body.getRotY(), body.getRotZ(), body.getRotW()},
25 {float(p.x), float(p.y), float(p.z)});
26 return {v.x + body.getX(), v.y + body.getY(), v.z + body.getZ()};
27}
28ForceVector direction(const Body& body, ForceVector p) {
29 const auto v = body.raw()->GetWorldVector({float(p.x), float(p.y)});
30 return {v.x, v.y, 0};
31}
32ForceVector direction(const Body3D& body, ForceVector p) {
33 const auto v = b3RotateVector(b3Quat{body.getRotX(), body.getRotY(), body.getRotZ(), body.getRotW()},
34 {float(p.x), float(p.y), float(p.z)});
35 return {v.x, v.y, v.z};
36}
37ForceVector velocity(const Body& body, ForceVector p) {
38 const auto v = body.raw()->GetLinearVelocityFromWorldPoint({float(p.x), float(p.y)});
39 return {v.x, v.y, 0};
40}
41ForceVector velocity(const Body3D& body, ForceVector p) {
42 const ForceVector r{p.x - body.getWorldCenterX(), p.y - body.getWorldCenterY(), p.z - body.getWorldCenterZ()};
43 const ForceVector w{body.getAngularVelocityX(), body.getAngularVelocityY(), body.getAngularVelocityZ()};
44 return {body.getLinearVelocityX() + w.y * r.z - w.z * r.y, body.getLinearVelocityY() + w.z * r.x - w.x * r.z,
45 body.getLinearVelocityZ() + w.x * r.y - w.y * r.x};
46}
47template <class W>
48Result<ForceBatch> prepareSurfaces(W& world, PhysicsLink link, std::span<const WingSurface> surfaces,
49 std::span<const double> controls, ForceVector wind) {
50 constexpr bool planar = std::is_same_v<W, World>;
51 if (controls.size() != surfaces.size() || !isFinite(wind) || (planar && wind.z != 0)) return invalid<ForceBatch>();
52 auto resolved = link.resolve(world);
53 if (!resolved) return Result<ForceBatch>::failure(resolved.status());
54 auto& body = *resolved.value();
55 if (!body.isActive() || body.getType() != "dynamic") return invalid<ForceBatch>();
56 std::vector<ForceLoad> loads;
57 loads.reserve(surfaces.size());
58 for (std::size_t i = 0; i < surfaces.size(); ++i) {
59 const auto& surface = surfaces[i];
60 if (!isFinite(controls[i]) ||
61 (planar && (surface.center.z != 0 || surface.model.chord.z != 0 || surface.model.normal.z != 0)))
62 return invalid<ForceBatch>();
63 const double angle = std::remainder(controls[i], 2 * std::numbers::pi);
64 const double c = std::cos(angle), s = std::sin(angle);
65 auto model = surface.model;
66 model.chord = direction(body, add(scale(surface.model.chord, c), scale(surface.model.normal, s)));
67 model.normal = direction(body, add(scale(surface.model.normal, c), scale(surface.model.chord, -s)));
68 const auto center = point(body, surface.center);
69 if (!fits(center)) return invalid<ForceBatch>();
70 const auto relative = add(velocity(body, center), scale(wind, -1));
71 auto sampled = ForceModels::wing(model, relative);
72 if (!sampled) return Result<ForceBatch>::failure(sampled.status());
73 loads.push_back({link, sampled.value().force, center, {}});
74 }
75 return ForceBatch::prepare(world, loads);
76}
77} // namespace
78Result<WingAssembly> WingAssembly::build(std::span<const WingSurface> surfaces) {
79 for (const auto& surface : surfaces) {
80 if (!fits(surface.center)) return invalid<WingAssembly>();
81 auto validated = ForceModels::wing(surface.model, {});
82 if (!validated) return Result<WingAssembly>::failure(validated.status());
83 }
85}
86Result<ForceBatch> WingAssembly::prepare(World& world, PhysicsLink body, std::span<const double> controls,
87 ForceVector wind) const {
88 return prepareSurfaces(world, body, surfaces_, controls, wind);
89}
90Result<ForceBatch> WingAssembly::prepare(World3D& world, PhysicsLink body, std::span<const double> controls,
91 ForceVector wind) const {
92 return prepareSurfaces(world, body, surfaces_, controls, wind);
93}
94} // namespace eve::physics
float w
Definition AnimClip.cpp:738
Vec3 relative
Definition AnimSmr.cpp:164
const std::string & s
glm::vec4 p[6]
ForceVector force
std::vector< WingSurface > surfaces
double r
float v
std::int32_t c
PhysicsJointLink link
std::array< float, 3 > scale
glm::dvec2 center
glm::dvec3 velocity
std::string body
glm::mat4 model
RoadNetwork resolved
RoadLaneDirection direction
int limit
Definition TreeMesh.cpp:166
glm::vec3 point
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
static Result< ForceBatch > prepare(World &world, std::span< const ForceLoad > loads)
Validate and copy planar requests without waking or applying forces.
static Result< WingSample > wing(const WingModel &model, ForceVector relativeVelocity)
Wing force using body-minus-air velocity at the aerodynamic center.
Immutable multi-surface aerodynamics, producing a single external-force batch. @ownership Copies geom...
static Result< WingAssembly > build(std::span< const WingSurface > surfaces)
Validate and copy local surface descriptions in deterministic input order.
Result< ForceBatch > prepare(World &world, PhysicsLink body, std::span< const double > deflections, ForceVector wind) const
Prepare planar surface forces; centers, local axes and wind must have z=0.
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:47
Box2D world wrapper (2D physics) with pixel-space coordinates. Handles stepping, gravity,...
Definition World.h:44
ForceVector add(ForceVector a, ForceVector b)
Definition ForceMath.h:13
bool isFinite(double value)
Definition ForceMath.h:8
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Owning world-space SI vector; 2D uses XY with Z exactly zero.
Definition ForceModels.h:12