载入中...
搜索中...
未找到
Rope3DTopology.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5
6namespace eve::physics {
8 std::vector<std::uint32_t> indices;
9 indices.reserve(elements_.size() * 2);
10 for (const auto& element : elements_) {
11 if (!element.active) continue;
12 indices.push_back(static_cast<std::uint32_t>(element.a));
13 indices.push_back(static_cast<std::uint32_t>(element.b));
14 }
15 return eve::Result<std::vector<std::uint32_t>>::success(std::move(indices));
16}
17
18namespace {
19Rope3D::Vec3 subtract(Rope3D::Vec3 a, Rope3D::Vec3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
20Rope3D::Vec3 addScaled(Rope3D::Vec3 a, Rope3D::Vec3 direction, float scale) {
21 return {a.x + direction.x * scale, a.y + direction.y * scale, a.z + direction.z * scale};
22}
23float magnitude(Rope3D::Vec3 value) { return std::sqrt(value.x * value.x + value.y * value.y + value.z * value.z); }
24Rope3D::Vec3 direction(Rope3D::Vec3 from, Rope3D::Vec3 to) {
25 auto delta = subtract(to, from);
26 const float length = magnitude(delta);
27 if (length <= 1e-6f) return {1.f, 0.f, 0.f};
28 return {delta.x / length, delta.y / length, delta.z / length};
29}
30} // namespace
31
32eve::Result<RopeTopologyChange> Rope3D::changeLength(float requested, float spacing, bool fromEnd) {
33 if (!std::isfinite(requested) || requested <= 0.f || !std::isfinite(spacing) || spacing <= 0.f)
35 eve::DiagnosticCode::InvalidArgument, "Length and particle spacing must be finite and positive",
36 "physics.rope3d.changeLength"));
37 if (std::any_of(elements_.begin(), elements_.end(), [](const Element& element) { return !element.active; }))
39 eve::DiagnosticCode::Conflict, "A torn rope must be repaired before cursor topology changes",
40 "physics.rope3d.changeLength"));
41 const float original = getRestLength();
42 if (std::abs(requested - original) <= 1e-6f)
44
45 bool structuralChange = false;
46 if (requested > original) {
47 float remaining = requested - original;
48 while (remaining > 1e-6f) {
49 const float segmentLength = std::min(spacing, remaining);
50 if (particles_.size() >= 100000)
53 "physics.rope3d.changeLength"));
54 if (fromEnd) {
55 const auto heading = direction(particles_[particles_.size() - 2].position, particles_.back().position);
56 Particle particle;
57 particle.position = particle.previous = addScaled(particles_.back().position, heading, segmentLength);
58 const int previous = getParticleCount() - 1;
59 particles_.push_back(particle);
60 Element element;
61 element.a = previous;
62 element.b = previous + 1;
63 element.restLength = segmentLength;
64 elements_.push_back(element);
65 } else {
66 const auto heading = direction(particles_[1].position, particles_[0].position);
67 Particle particle;
68 particle.position = particle.previous = addScaled(particles_[0].position, heading, segmentLength);
69 particles_.insert(particles_.begin(), particle);
70 for (auto& element : elements_) {
71 ++element.a;
72 ++element.b;
73 }
74 Element element;
75 element.a = 0;
76 element.b = 1;
77 element.restLength = segmentLength;
78 elements_.insert(elements_.begin(), element);
79 }
80 remaining -= segmentLength;
81 structuralChange = true;
82 }
83 } else {
84 float remaining = original - requested;
85 while (elements_.size() > 1) {
86 Element& edge = fromEnd ? elements_.back() : elements_.front();
87 if (remaining + 1e-6f < edge.restLength) break;
88 remaining -= edge.restLength;
89 if (fromEnd) {
90 const Particle removed = particles_.back();
91 particles_.pop_back();
92 elements_.pop_back();
93 if (removed.attached) {
94 auto& endpoint = particles_.back();
95 endpoint.attached = true;
96 endpoint.attachment = endpoint.position = endpoint.previous = removed.attachment;
97 }
98 } else {
99 const Particle removed = particles_.front();
100 particles_.erase(particles_.begin());
101 elements_.erase(elements_.begin());
102 for (auto& element : elements_) {
103 --element.a;
104 --element.b;
105 }
106 if (removed.attached) {
107 auto& endpoint = particles_.front();
108 endpoint.attached = true;
109 endpoint.attachment = endpoint.position = endpoint.previous = removed.attachment;
110 }
111 }
112 structuralChange = true;
113 }
114 Element& edge = fromEnd ? elements_.back() : elements_.front();
115 edge.restLength = std::max(1e-6f, edge.restLength - remaining);
116 }
117 if (structuralChange || std::abs(requested - original) > 1e-6f) ++topologyRevision_;
118 syncBendState();
120}
121
122int Rope3D::getLastTornElement(int eventIndex) const {
123 return eventIndex >= 0 && eventIndex < getLastTornElementCount()
124 ? lastTornElements_[static_cast<std::size_t>(eventIndex)]
125 : -1;
126}
127
128} // namespace eve::physics
std::vector< std::uint32_t > indices
float length
std::array< float, 3 > position
std::array< float, 3 > scale
std::string to
std::string from
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::shared_ptr< const OceanSprayFrame > previous
Object::Ptr value
RoadLaneDirection direction
int removed
std::string element
int spacing
V3 heading
Definition TreeMesh.cpp:429
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
eve::Result< std::vector< std::uint32_t > > captureSegmentIndices() const
Copy currently active segment endpoints into an owning ordered index stream. @ownership Owns returned...
int getLastTornElement(int eventIndex) const
Returns a torn element index, or -1 for an invalid event index.
int getLastTornElementCount() const
Number of elements automatically torn by the most recent update/step.
Definition Rope3D.h:209
int getParticleCount() const
Definition Rope3D.h:213
float getRestLength() const
Definition Rope3D.cpp:223
eve::Result< RopeTopologyChange > changeLength(float length, float particleSpacing, bool fromEnd=true)
Changes material length at one endpoint by adding/removing particles near the requested spacing.
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Compact solver vector exposed only as a value type.
Definition Rope3D.h:41