载入中...
搜索中...
未找到
CrowdSimulation.cpp
浏览该文件的文档.
1// eve-hot-path
3
4namespace eve::crowd {
5namespace {
6float wrapPi(float a) {
7 const float kTwoPi = 6.28318530717958647692f;
8 return std::remainder(a, kTwoPi);
9}
10
11} // namespace
12
15 const double maxRadius = radii.empty() ? 0.0 : double(*std::max_element(radii.begin(), radii.end()));
16 spatialIndex.rebuild(xs, ys, tieKeys, std::max(2.0 * maxRadius, 1.0),
17 budgetActive ? activeBudget.maxGridCells : std::numeric_limits<std::int64_t>::max(),
19 workReport.gridCellsPeak = std::max(workReport.gridCellsPeak, static_cast<std::int64_t>(spatialIndex.cellCount()));
20 workReport.gridEntriesPeak = std::max(workReport.gridEntriesPeak, static_cast<std::int64_t>(actions.size()));
21}
22
24 const int n = int(actions.size());
25 if (avoidance.enabled) {
26 avoidanceMaxSpeed = n == 0 ? 0.f : *std::max_element(maxSpeeds.begin(), maxSpeeds.end());
27 avoidanceMaxRadius = n == 0 ? 0.f : *std::max_element(radii.begin(), radii.end());
28 }
29 spendPreferredWork(std::int64_t(n) * 5);
30 nextVxs.resize(size_t(n));
31 nextVys.resize(size_t(n));
32 preferredVxs.resize(size_t(n));
33 preferredVys.resize(size_t(n));
34 arriveFactors.resize(size_t(n), 1.f);
35 preparePreferredVelocities(dt);
36 if (avoidance.enabled) prepareAvoidanceNeighbors();
37 dispatchWork(size_t(n), [&](std::size_t first, std::size_t last) {
38 for (int i = int(first); i < int(last); ++i) {
39 if (interactions[size_t(i)].holdPosition) {
40 nextVxs[size_t(i)] = nextVys[size_t(i)] = speeds[size_t(i)] = 0.f;
41 continue;
42 }
43 const float desX = preferredVxs[size_t(i)], desY = preferredVys[size_t(i)];
44 const float maxSpeed = maxSpeeds[size_t(i)];
45 const float arriveK = arriveFactors[size_t(i)];
46 // 加速度限幅。
47 float avx = vxs[size_t(i)];
48 float avy = vys[size_t(i)];
49 float ddx = desX - avx;
50 float ddy = desY - avy;
51 const float ddl = std::sqrt(ddx * ddx + ddy * ddy);
52 const float maxDelta = std::max(maxAccels[size_t(i)] * dt, 0.f);
53 if (ddl > maxDelta && ddl > 1e-6f) {
54 ddx *= maxDelta / ddl;
55 ddy *= maxDelta / ddl;
56 }
57 avx += ddx;
58 avy += ddy;
59
60 if (avoidance.enabled) selectAvoidanceVelocity(size_t(i), dt, avx, avy);
61
62 // 速度限幅。
63 float sp = std::sqrt(avx * avx + avy * avy);
64 if (sp > maxSpeed && sp > 1e-6f) {
65 avx *= maxSpeed / sp;
66 avy *= maxSpeed / sp;
67 sp = maxSpeed;
68 }
69 nextVxs[size_t(i)] = avx;
70 nextVys[size_t(i)] = avy;
71 speeds[size_t(i)] = sp;
72
73 // 接近目标时对速度做阻尼,快速消除目标点附近的来回振荡。
74 if (arriveK < 1.f) {
75 const float damp = 1.f - (1.f - arriveK) * std::min(1.f, dt * 4.f);
76 nextVxs[size_t(i)] *= damp;
77 nextVys[size_t(i)] *= damp;
78 speeds[size_t(i)] *= damp;
79 }
80
81 // 转向:heading 以 turnRate·dt 为上限向速度方向收敛。
82 const float spAfter = speeds[size_t(i)];
83 if (spAfter > 1e-4f) {
84 const float targetAng = std::atan2(nextVys[size_t(i)], nextVxs[size_t(i)]);
85 const float delta = wrapPi(targetAng - headings[size_t(i)]);
86 const float maxTurn = std::max(turnRates[size_t(i)] * dt, 0.f);
87 headings[size_t(i)] += std::clamp(delta, -maxTurn, maxTurn);
88 }
89 }
90 });
91 vxs.swap(nextVxs);
92 vys.swap(nextVys);
93 for (int i = 0; i < n; ++i) {
94 xs[size_t(i)] += vxs[size_t(i)] * dt;
95 ys[size_t(i)] += vys[size_t(i)] * dt;
96 }
97}
98
100 if (!field.valid()) return;
101 const float width = float(field.getWidth()) * field.getCellSize();
102 const float height = float(field.getHeight()) * field.getCellSize();
103 for (size_t i = 0; i < xs.size(); ++i) {
104 for (int pass = 0; pass < 4; ++pass) {
105 const float oldX = xs[i], oldY = ys[i];
106 field.resolvePenetration(xs[i], ys[i], radii[i]);
107 if (clampToField) {
108 // Field origin is the lower cell corner, consistently with sampling.
109 const float rx = std::min(radii[i], width * 0.5f);
110 const float ry = std::min(radii[i], height * 0.5f);
111 xs[i] = std::clamp(xs[i], field.getOriginX() + rx, field.getOriginX() + width - rx);
112 ys[i] = std::clamp(ys[i], field.getOriginY() + ry, field.getOriginY() + height - ry);
113 }
114 const float dx = xs[i] - oldX, dy = ys[i] - oldY;
115 const float length2 = dx * dx + dy * dy;
116 if (length2 == 0.f) break;
117 // Remove velocity into the constraint; retaining it causes permanent
118 // walking-in-place when a requested destination lies outside the field.
119 const float inward = vxs[i] * dx + vys[i] * dy;
120 if (inward < 0.f) {
121 vxs[i] -= dx * inward / length2;
122 vys[i] -= dy * inward / length2;
123 speeds[i] = std::hypot(vxs[i], vys[i]);
124 }
125 }
126 }
127}
128
129} // namespace eve::crowd
glm::vec3 n
Definition Grass.cpp:63
std::int32_t first
std::uint32_t height
std::uint32_t width
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
double dt
std::vector< ActionSpec > actions
Definition PlayHost.cpp:127
float dy
Definition Shadow.cpp:151
std::array< float, 4 > radii
void rebuild(std::span< const float > xs, std::span< const float > ys, std::span< const CrowdTieKey > ties, double cellSize, std::int64_t maxCells, std::int64_t &comparisons)
Rebuild owning directories; failure is handled by the surrounding advance rollback....
std::size_t cellCount() const noexcept
Observe occupied cell count; constant time, owner thread.
std::int64_t maxGridCells
Occupied sparse cells, independent of empty world extent.
Definition Crowd.h:74
std::vector< float > xs
SOA agent storage, indexed by compact slot id. @cost Linear in agent count; mutation keeps all owned ...
std::vector< float > ys
std::vector< TieKey > tieKeys
CrowdSpatialIndex spatialIndex
std::vector< int32_t > actions
std::vector< float > radii
std::int64_t gridCellsPeak
Largest occupied sparse-cell count during this advance.
Definition Crowd.h:104
std::int64_t gridBuilds
Broadphase rebuilds, including final residual inspection.
Definition Crowd.h:103
std::int64_t gridEntriesPeak
Largest indexed agent membership count.
Definition Crowd.h:106
std::int64_t gridSortComparisons
Sorting comparisons across all broadphase rebuilds.
Definition Crowd.h:107