载入中...
搜索中...
未找到
Rope3DSampling.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5
6namespace eve::physics {
7namespace {
8Rope3D::Vec3 subtract(Rope3D::Vec3 a, Rope3D::Vec3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
9Rope3D::Vec3 interpolate(Rope3D::Vec3 a, Rope3D::Vec3 b, float t) {
10 return {a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t};
11}
12Rope3D::Vec3 normalized(Rope3D::Vec3 value) {
13 const float length = std::sqrt(value.x * value.x + value.y * value.y + value.z * value.z);
14 return length > 1e-6f ? Rope3D::Vec3{value.x / length, value.y / length, value.z / length}
15 : Rope3D::Vec3{1.f, 0.f, 0.f};
16}
17} // namespace
18
19Rope3D::Vec3 Rope3D::samplePosition(float mu) const {
20 if (particles_.empty()) return {};
21 const float total = getRestLength();
22 if (total <= 1e-6f) return particles_.front().position;
23 float distance = std::clamp(std::isfinite(mu) ? mu : 0.f, 0.f, 1.f) * total;
24 for (const auto& element : elements_) {
25 if (!element.active) continue;
26 if (distance <= element.restLength)
27 return interpolate(particles_[element.a].position, particles_[element.b].position,
28 element.restLength > 1e-6f ? distance / element.restLength : 0.f);
29 distance -= element.restLength;
30 }
31 for (auto it = elements_.rbegin(); it != elements_.rend(); ++it)
32 if (it->active) return particles_[it->b].position;
33 return particles_.front().position;
34}
35
36Rope3D::Vec3 Rope3D::sampleTangent(float mu) const {
37 const float total = getRestLength();
38 if (total <= 1e-6f) return {1.f, 0.f, 0.f};
39 float distance = std::clamp(std::isfinite(mu) ? mu : 0.f, 0.f, 1.f) * total;
40 for (const auto& element : elements_) {
41 if (!element.active) continue;
42 if (distance <= element.restLength)
43 return normalized(subtract(particles_[element.b].position, particles_[element.a].position));
44 distance -= element.restLength;
45 }
46 for (auto it = elements_.rbegin(); it != elements_.rend(); ++it)
47 if (it->active) return normalized(subtract(particles_[it->b].position, particles_[it->a].position));
48 return {1.f, 0.f, 0.f};
49}
50
51float Rope3D::getSampleX(float mu) const { return samplePosition(mu).x; }
52float Rope3D::getSampleY(float mu) const { return samplePosition(mu).y; }
53float Rope3D::getSampleZ(float mu) const { return samplePosition(mu).z; }
54float Rope3D::getSampleTangentX(float mu) const { return sampleTangent(mu).x; }
55float Rope3D::getSampleTangentY(float mu) const { return sampleTangent(mu).y; }
56float Rope3D::getSampleTangentZ(float mu) const { return sampleTangent(mu).z; }
57
58} // namespace eve::physics
double value
float length
Definition CaveMesh.cpp:94
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
float t
std::string element
float getSampleTangentX(float mu) const
Samples a normalized current tangent by active material length.
float getSampleTangentZ(float mu) const
float getSampleY(float mu) const
float getSampleTangentY(float mu) const
float getSampleX(float mu) const
Samples current rope position by normalized active material length.
float getRestLength() const
Definition Rope3D.cpp:223
float getSampleZ(float mu) const
Vec3 interpolate(Vec3 start, Vec3 end, float amount) noexcept
Interpolate.
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
std::mutex mu
Definition Graphics.cpp:181