@esengine/pathfinding 13.2.0 → 13.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -182,6 +182,16 @@ interface IORCASolverConfig {
182
182
  * @en Numerical precision threshold
183
183
  */
184
184
  epsilon?: number;
185
+ /**
186
+ * @zh 是否使用 Y 轴向下的坐标系(如 Canvas/屏幕坐标)
187
+ * @en Whether using Y-axis down coordinate system (like Canvas/screen coords)
188
+ *
189
+ * @zh 这会影响障碍物顶点顺序的判断(CCW 检测)
190
+ * @en This affects obstacle vertex order detection (CCW check)
191
+ *
192
+ * @default false
193
+ */
194
+ yAxisDown?: boolean;
185
195
  }
186
196
  /**
187
197
  * @zh ORCA 求解结果
@@ -194,8 +204,8 @@ interface IORCAResult {
194
204
  */
195
205
  velocity: IVector2;
196
206
  /**
197
- * @zh 是否找到可行解
198
- * @en Whether a feasible solution was found
207
+ * @zh 是否找到可行解(满足所有约束)
208
+ * @en Whether a feasible solution was found (satisfies all constraints)
199
209
  */
200
210
  feasible: boolean;
201
211
  /**
@@ -203,6 +213,11 @@ interface IORCAResult {
203
213
  * @en Number of ORCA lines generated
204
214
  */
205
215
  numLines: number;
216
+ /**
217
+ * @zh 违反的约束数量
218
+ * @en Number of violated constraints
219
+ */
220
+ violatedConstraints: number;
206
221
  }
207
222
  /**
208
223
  * @zh ORCA 求解器接口
@@ -285,123 +300,144 @@ declare const DEFAULT_AGENT_PARAMS: {
285
300
  };
286
301
 
287
302
  /**
288
- * @zh ORCA 避让算法求解器
289
- * @en ORCA Avoidance Algorithm Solver
303
+ * @zh 碰撞解决器
304
+ * @en Collision Resolver
290
305
  *
291
- * @zh 实现最优互惠碰撞避免(ORCA)算法,用于多代理局部避让
292
- * @en Implements Optimal Reciprocal Collision Avoidance (ORCA) algorithm for multi-agent local avoidance
306
+ * @zh 提供位置级别的硬碰撞检测和解决,作为 ORCA 的补充保护层
307
+ * @en Provides position-level hard collision detection and resolution as a supplementary protection layer for ORCA
293
308
  */
294
309
 
295
310
  /**
296
- * @zh ORCA 求解器实现
297
- * @en ORCA Solver implementation
298
- *
299
- * @zh 实现最优互惠碰撞避免算法,计算代理的安全速度
300
- * @en Implements Optimal Reciprocal Collision Avoidance algorithm to compute safe velocities for agents
311
+ * @zh 碰撞检测结果
312
+ * @en Collision detection result
301
313
  */
302
- declare class ORCASolver implements IORCASolver {
303
- private readonly config;
304
- constructor(config?: IORCASolverConfig);
314
+ interface ICollisionResult {
305
315
  /**
306
- * @zh 计算代理的新速度
307
- * @en Compute new velocity for agent
308
- *
309
- * @param agent - @zh 当前代理 @en Current agent
310
- * @param neighbors - @zh 邻近代理列表 @en List of neighboring agents
311
- * @param obstacles - @zh 障碍物列表 @en List of obstacles
312
- * @param deltaTime - @zh 时间步长 @en Time step
313
- * @returns @zh 计算得到的新速度 @en Computed new velocity
316
+ * @zh 是否发生碰撞
317
+ * @en Whether collision occurred
314
318
  */
315
- computeNewVelocity(agent: IAvoidanceAgent, neighbors: readonly IAvoidanceAgent[], obstacles: readonly IObstacle[], deltaTime: number): IVector2;
319
+ collided: boolean;
316
320
  /**
317
- * @zh 创建代理间的 ORCA 约束线
318
- * @en Create ORCA constraint lines for agent-agent avoidance
321
+ * @zh 穿透深度
322
+ * @en Penetration depth
319
323
  */
320
- private createAgentORCALines;
324
+ penetration: number;
321
325
  /**
322
- * @zh 创建障碍物的 ORCA 约束线
323
- * @en Create ORCA constraint lines for obstacle avoidance
326
+ * @zh 碰撞法线(从障碍物指向代理)
327
+ * @en Collision normal (pointing from obstacle to agent)
324
328
  */
325
- private createObstacleORCALines;
329
+ normal: IVector2;
330
+ /**
331
+ * @zh 最近点
332
+ * @en Closest point on obstacle
333
+ */
334
+ closestPoint: IVector2;
326
335
  }
327
336
  /**
328
- * @zh 创建 ORCA 求解器
329
- * @en Create ORCA solver
330
- *
331
- * @param config - @zh 可选配置参数 @en Optional configuration parameters
332
- * @returns @zh ORCA 求解器实例 @en ORCA solver instance
333
- */
334
- declare function createORCASolver(config?: IORCASolverConfig): ORCASolver;
335
-
336
- /**
337
- * @zh KD-Tree 空间索引
338
- * @en KD-Tree Spatial Index
339
- *
340
- * @zh 用于快速查询指定范围内的邻近代理
341
- * @en Used for fast neighbor queries within specified range
337
+ * @zh 空碰撞结果
338
+ * @en Empty collision result
342
339
  */
343
-
340
+ declare const EMPTY_COLLISION: ICollisionResult;
344
341
  /**
345
- * @zh KD-Tree 空间索引
346
- * @en KD-Tree spatial index
347
- *
348
- * @zh 每帧重建,支持高效的范围查询
349
- * @en Rebuilt every frame, supports efficient range queries
342
+ * @zh 碰撞解决器配置
343
+ * @en Collision resolver configuration
350
344
  */
351
- declare class KDTree implements ISpatialIndex {
352
- private agents;
353
- private agentIndices;
354
- private nodes;
345
+ interface ICollisionResolverConfig {
355
346
  /**
356
- * @zh 最大叶节点大小
357
- * @en Maximum leaf size
347
+ * @zh 碰撞响应系数(0-1,1 表示完全推出)
348
+ * @en Collision response factor (0-1, 1 means fully push out)
358
349
  */
359
- private readonly maxLeafSize;
350
+ responseFactor?: number;
360
351
  /**
361
- * @zh 构建 KD-Tree
362
- * @en Build KD-Tree
352
+ * @zh 安全边距(额外距离)
353
+ * @en Safety margin (extra distance)
363
354
  */
364
- build(agents: readonly IAvoidanceAgent[]): void;
365
- /**
366
- * @zh 递归构建 KD-Tree
367
- * @en Recursively build KD-Tree
368
- */
369
- private buildRecursive;
355
+ safetyMargin?: number;
356
+ }
357
+ /**
358
+ * @zh 默认配置
359
+ * @en Default configuration
360
+ */
361
+ declare const DEFAULT_COLLISION_CONFIG: Required<ICollisionResolverConfig>;
362
+ /**
363
+ * @zh 碰撞解决器
364
+ * @en Collision Resolver
365
+ *
366
+ * @zh 提供位置级别的硬碰撞检测和解决
367
+ * @en Provides position-level hard collision detection and resolution
368
+ */
369
+ declare class CollisionResolver {
370
+ private readonly config;
371
+ constructor(config?: ICollisionResolverConfig);
370
372
  /**
371
- * @zh 按 X 坐标排序
372
- * @en Sort by X coordinate
373
+ * @zh 检测圆与单个障碍物的碰撞
374
+ * @en Detect collision between circle and single obstacle
375
+ *
376
+ * @param position - @zh 圆心位置 @en Circle center position
377
+ * @param radius - @zh 圆半径 @en Circle radius
378
+ * @param obstacle - @zh 障碍物 @en Obstacle
379
+ * @returns @zh 碰撞结果 @en Collision result
373
380
  */
374
- private sortByX;
381
+ detectCollision(position: IVector2, radius: number, obstacle: IObstacle): ICollisionResult;
375
382
  /**
376
- * @zh 按 Y 坐标排序
377
- * @en Sort by Y coordinate
383
+ * @zh 检测圆与所有障碍物的碰撞
384
+ * @en Detect collision between circle and all obstacles
385
+ *
386
+ * @param position - @zh 圆心位置 @en Circle center position
387
+ * @param radius - @zh 圆半径 @en Circle radius
388
+ * @param obstacles - @zh 障碍物列表 @en List of obstacles
389
+ * @returns @zh 最严重的碰撞结果 @en Most severe collision result
378
390
  */
379
- private sortByY;
391
+ detectCollisions(position: IVector2, radius: number, obstacles: readonly IObstacle[]): ICollisionResult;
380
392
  /**
381
- * @zh 查询邻居
382
- * @en Query neighbors
393
+ * @zh 解决碰撞,返回修正后的位置
394
+ * @en Resolve collision, return corrected position
395
+ *
396
+ * @param position - @zh 当前位置 @en Current position
397
+ * @param radius - @zh 半径 @en Radius
398
+ * @param obstacles - @zh 障碍物列表 @en List of obstacles
399
+ * @returns @zh 修正后的位置 @en Corrected position
383
400
  */
384
- queryNeighbors(position: IVector2, radius: number, maxResults: number, excludeId?: number): INeighborResult[];
401
+ resolveCollision(position: IVector2, radius: number, obstacles: readonly IObstacle[]): IVector2;
385
402
  /**
386
- * @zh 递归查询
387
- * @en Recursive query
403
+ * @zh 验证速度是否会导致碰撞,返回安全速度
404
+ * @en Validate velocity won't cause collision, return safe velocity
405
+ *
406
+ * @param position - @zh 当前位置 @en Current position
407
+ * @param velocity - @zh 目标速度 @en Target velocity
408
+ * @param radius - @zh 半径 @en Radius
409
+ * @param obstacles - @zh 障碍物列表 @en List of obstacles
410
+ * @param deltaTime - @zh 时间步长 @en Time step
411
+ * @returns @zh 安全速度 @en Safe velocity
388
412
  */
389
- private queryRecursive;
413
+ validateVelocity(position: IVector2, velocity: IVector2, radius: number, obstacles: readonly IObstacle[], deltaTime: number): IVector2;
390
414
  /**
391
- * @zh 清空索引
392
- * @en Clear the index
415
+ * @zh 检测两个代理之间的碰撞
416
+ * @en Detect collision between two agents
417
+ *
418
+ * @param posA - @zh 代理 A 位置 @en Agent A position
419
+ * @param radiusA - @zh 代理 A 半径 @en Agent A radius
420
+ * @param posB - @zh 代理 B 位置 @en Agent B position
421
+ * @param radiusB - @zh 代理 B 半径 @en Agent B radius
422
+ * @returns @zh 碰撞结果 @en Collision result
393
423
  */
394
- clear(): void;
424
+ detectAgentCollision(posA: IVector2, radiusA: number, posB: IVector2, radiusB: number): ICollisionResult;
395
425
  /**
396
- * @zh 获取代理数量
397
- * @en Get agent count
426
+ * @zh 解决代理之间的碰撞
427
+ * @en Resolve collision between agents
428
+ *
429
+ * @param posA - @zh 代理 A 位置 @en Agent A position
430
+ * @param radiusA - @zh 代理 A 半径 @en Agent A radius
431
+ * @param posB - @zh 代理 B 位置 @en Agent B position
432
+ * @param radiusB - @zh 代理 B 半径 @en Agent B radius
433
+ * @returns @zh 修正后的位置 [A, B] @en Corrected positions [A, B]
398
434
  */
399
- get agentCount(): number;
435
+ resolveAgentCollision(posA: IVector2, radiusA: number, posB: IVector2, radiusB: number): [IVector2, IVector2];
400
436
  }
401
437
  /**
402
- * @zh 创建 KD-Tree
403
- * @en Create KD-Tree
438
+ * @zh 创建碰撞解决器
439
+ * @en Create collision resolver
404
440
  */
405
- declare function createKDTree(): KDTree;
441
+ declare function createCollisionResolver(config?: ICollisionResolverConfig): CollisionResolver;
406
442
 
407
- export { DEFAULT_ORCA_CONFIG as D, type IORCALine as I, KDTree as K, ORCASolver as O, type IAvoidanceAgent as a, type IObstacle as b, type IORCASolverConfig as c, type IORCAResult as d, type IORCASolver as e, type INeighborResult as f, type ISpatialIndex as g, DEFAULT_AGENT_PARAMS as h, createORCASolver as i, createKDTree as j, type IObstacleVertex as k };
443
+ export { CollisionResolver as C, DEFAULT_ORCA_CONFIG as D, EMPTY_COLLISION as E, type IORCALine as I, type IAvoidanceAgent as a, type IObstacle as b, type IORCASolverConfig as c, type IORCAResult as d, type IORCASolver as e, type INeighborResult as f, type ISpatialIndex as g, type ICollisionResult as h, type ICollisionResolverConfig as i, DEFAULT_AGENT_PARAMS as j, DEFAULT_COLLISION_CONFIG as k, createCollisionResolver as l, type IObstacleVertex as m };