载入中...
搜索中...
未找到
CrowdInternal.h
浏览该文件的文档.
1#pragma once
2
3#include "crowd/Crowd.h"
4
5#include <algorithm>
6#include <cmath>
7#include <unordered_map>
8#include <vector>
9
10namespace eve::crowd {
11
19
20 float defaultSpeed = 120.f;
21 float defaultRadius = 6.f;
22 float defaultTurnRate = 6.f;
23 float arriveRadius = 64.f;
24 float sepRadius = 28.f;
25 float perceiveRadius = 64.f;
26 float sepWeight = 1.f;
27 float alignWeight = 0.f;
28 float cohesionWeight = 0.f;
29 float wanderWeight = 0.f;
30 float goalWeight = 1.f;
31 bool resolveOverlaps = true;
32 bool clampToField = true;
33 int maxAgents = 100000;
34 float simTime = 0.f;
36 std::int64_t avoidanceChecks = 0;
40 size_t slot;
41 double distance2;
42 };
47 std::vector<AvoidanceNeighbor> avoidanceNeighbors;
48
53 // SOA 单位存储(id = 槽位索引)。
54 std::vector<float> xs, ys, headings, vxs, vys, speeds;
57 std::vector<float> radii, maxSpeeds, maxAccels, turnRates;
58 std::vector<int32_t> actions, datas, avoidancePriorities;
59 std::vector<uint8_t> hasTargets;
60 std::vector<AgentInteraction> interactions;
61 std::vector<float> targetXs, targetYs;
62 std::vector<float> wanderPhases;
63 std::vector<std::string> stableIds;
64 std::unordered_map<std::string, int> namedAgents;
65
70 // 每帧重建的计数排序空间网格。
71 std::vector<int32_t> cellCount, cellStart, cursor, sorted;
72 int gridW = 0;
73 int gridH = 0;
74 float gridOriginX = 0.f;
75 float gridOriginY = 0.f;
76 float gridCell = 1.f;
77
78 bool validId(int id) const { return id >= 0 && id < int(actions.size()); }
79
80 bool canInteract(size_t a, size_t b) const {
81 return (interactions[a].layer & interactions[b].mask) != 0 &&
82 (interactions[b].layer & interactions[a].mask) != 0;
83 }
84
85 void rebuildGrid();
86
87 template <typename Fn>
88 void forEachNeighbor(float qx, float qy, float radius, Fn&& fn) const {
89 if (gridW <= 0 || gridH <= 0 || radius <= 0.f) return;
90 const float cell = gridCell;
91 const auto boundedCell = [&](double coordinate, float origin, int extent) {
92 return int(std::clamp(std::floor((coordinate - origin) / cell), 0.0, double(extent - 1)));
93 };
94 const int x0 = boundedCell(double(qx) - radius, gridOriginX, gridW);
95 const int x1 = boundedCell(double(qx) + radius, gridOriginX, gridW);
96 const int y0 = boundedCell(double(qy) - radius, gridOriginY, gridH);
97 const int y1 = boundedCell(double(qy) + radius, gridOriginY, gridH);
98 const float r2 = radius * radius;
99 for (int cy = y0; cy <= y1; ++cy) {
100 const int rowBase = cy * gridW;
101 for (int cx = x0; cx <= x1; ++cx) {
102 const int c = rowBase + cx;
103 const int begin = cellStart[size_t(c)];
104 const int end = begin + cellCount[size_t(c)];
105 for (int k = begin; k < end; ++k) {
106 const int j = sorted[size_t(k)];
107 const float dx = xs[size_t(j)] - qx;
108 const float dy = ys[size_t(j)] - qy;
109 if (dx * dx + dy * dy <= r2) fn(j);
110 }
111 }
112 }
113 }
114
115 void stepAgents(float dt);
116 void selectAvoidanceVelocity(size_t index, float dt, float& vx, float& vy);
117
118 float resolveOverlapsPass();
119
120 void resolveWalls();
121};
122
123} // namespace eve::crowd
int mask
float cx
Definition CardTypes.cpp:33
float cy
Definition CardTypes.cpp:34
std::int32_t c
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
TileLayer * layer
float radius
float begin
V3 origin
Definition RoadBake.cpp:138
float dy
float dx
Cell cell
uint32_t index
float vy
float qy
float vx
float qx
连续流场(flow field)。
Definition CrowdField.h:22
SettlementPipeline::Stage fn
Optional bounded velocity sampling for RTS local avoidance.
Definition Crowd.h:48
Private crowd runtime storage. @cost Linear in agent count for SOA vectors and transient broadphase/a...
std::vector< int32_t > cellCount
Per-frame counting-sort broadphase workspace. @cost Linear in grid cells plus agent count when rebuil...
std::vector< float > xs
SOA agent storage, indexed by compact slot id. @cost Linear in agent count; mutation keeps all owned ...
std::vector< int32_t > cellStart
std::vector< float > preferredVys
std::vector< float > headings
std::vector< float > maxSpeeds
std::vector< AvoidanceNeighbor > avoidanceNeighbors
Per-step avoidance neighbor scratch space. @cost Linear in checked neighbors and bounded by avoidance...
std::vector< float > correctionXs
std::int64_t avoidanceChecks
std::vector< float > vys
std::vector< float > ys
std::vector< float > turnRates
bool validId(int id) const
std::vector< float > preferredVxs
std::vector< float > targetYs
std::vector< int32_t > sorted
std::vector< int32_t > cursor
std::vector< int32_t > datas
std::vector< float > maxAccels
std::unordered_map< std::string, int > namedAgents
AvoidanceSettings avoidance
std::vector< float > correctionYs
std::vector< float > wanderPhases
std::vector< float > arriveFactors
std::vector< float > nextVys
std::vector< float > nextVxs
std::vector< float > vxs
std::vector< int32_t > avoidancePriorities
void selectAvoidanceVelocity(size_t index, float dt, float &vx, float &vy)
std::vector< int32_t > actions
void forEachNeighbor(float qx, float qy, float radius, Fn &&fn) const
std::vector< uint8_t > hasTargets
std::vector< float > radii
std::vector< float > targetXs
std::vector< std::string > stableIds
std::vector< AgentInteraction > interactions
std::vector< float > speeds
bool canInteract(size_t a, size_t b) const