载入中...
搜索中...
未找到
CrowdField.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5// 连续流场:Dijkstra 积分场 + 梯度方向场 + 双线性采样。
6// 与 map/FlowField 的区别:map 的流场是格级 next 指针,本类是连续方向向量,
7// 供单位在格子内平滑移动、平滑转向。纯数学,零模块依赖。
8
9#include <cstdint>
10#include <vector>
11
12namespace eve::crowd {
13
23public:
25 static constexpr float kUnreachable = 3.4e38f;
26
28 CrowdField() = default;
29
38 void resize(int width, int height, float cellSize, float originX, float originY);
39
41 void clear();
42
44 bool valid() const { return width_ > 0 && height_ > 0 && cellSize_ > 0.f; }
45
47 int getWidth() const { return width_; }
49 int getHeight() const { return height_; }
51 float getCellSize() const { return cellSize_; }
53 float getOriginX() const { return originX_; }
55 float getOriginY() const { return originY_; }
56
63 void setBlocked(int cx, int cy, bool blocked);
64
66 bool isBlocked(int cx, int cy) const;
67
72 void setCellCost(int cx, int cy, float cost);
73
75 float getCellCost(int cx, int cy) const;
76
78 void setAllCellCost(float cost);
79
85 void setGoal(int gx, int gy);
86
88 void addGoal(int gx, int gy);
90 void clearGoals();
92 int goalCount() const { return int(goals_.size() / 2); }
93
95 bool isBuilt() const { return built_; }
96
98 void build();
99
101 float costAtCell(int cx, int cy) const;
102
104 bool isReachable(int cx, int cy) const;
105
113 void flowAtCell(int cx, int cy, float &dx, float &dy) const;
114
121 float costAtWorld(float wx, float wy) const;
122
130 void flowAtWorld(float wx, float wy, float &dx, float &dy) const;
131
143 bool resolvePenetration(float &wx, float &wy, float radius) const;
144
146 const std::vector<float> &integration() const { return integ_; }
148 const std::vector<float> &flowX() const { return flowX_; }
150 const std::vector<float> &flowY() const { return flowY_; }
151
152private:
153 bool inBounds(int cx, int cy) const { return cx >= 0 && cy >= 0 && cx < width_ && cy < height_; }
154 int index(int cx, int cy) const { return cy * width_ + cx; }
155 void worldToCell(float wx, float wy, float &fx, float &fy) const;
156 void rebuildFlow();
157
158 int width_ = 0;
159 int height_ = 0;
160 float cellSize_ = 0.f;
161 float originX_ = 0.f;
162 float originY_ = 0.f;
163 std::vector<float> cost_; // 地形代价(0=阻挡,>=1 可走)
164 std::vector<float> integ_; // Dijkstra 积分场
165 std::vector<float> flowX_; // 每格方向 X
166 std::vector<float> flowY_; // 每格方向 Y
167 std::vector<int> goals_; // 目标格 (x,y) 对
168 bool built_ = false;
169};
170
171} // namespace eve::crowd
float cx
Definition CardTypes.cpp:33
float cy
Definition CardTypes.cpp:34
eve::resource::CostSpec cost
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::uint32_t height
std::uint32_t width
float radius
float dy
float dx
uint32_t index
float wx
float wy
连续流场(flow field)。
Definition CrowdField.h:22
const std::vector< float > & flowX() const
只读访问方向场 X 分量(调试渲染用)。
Definition CrowdField.h:148
float getOriginX() const
Returns the origin x.
Definition CrowdField.h:53
int getWidth() const
网格尺寸访问器。
Definition CrowdField.h:47
float getOriginY() const
Returns the origin y.
Definition CrowdField.h:55
const std::vector< float > & integration() const
只读访问积分场(调试渲染用)。
Definition CrowdField.h:146
int goalCount() const
目标个数。
Definition CrowdField.h:92
bool isBuilt() const
是否已 build 过(build 后或 resize 后为 true,改动地形后仍为 true)。
Definition CrowdField.h:95
int getHeight() const
Returns the height.
Definition CrowdField.h:49
CrowdField()=default
Crowd field.
float getCellSize() const
Returns the cell size.
Definition CrowdField.h:51
const std::vector< float > & flowY() const
只读访问方向场 Y 分量(调试渲染用)。
Definition CrowdField.h:150
bool valid() const
是否配置了有效网格。
Definition CrowdField.h:44