Mass Battle Frameworkv1.20.0

四层,各自解决别人解决不了的事 Four Layers, Each Solving What the Others Cannot

SIMULATION LOGIC

移动模型为什么这么分层 · 2026-11-21Why the movement model is layered this way · 2026-11-21

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

顺序:意图 → 交通 → 避障 → 物理 The Order: Intent, Traffic, Avoidance, Physics

移动按四层走,这个顺序是排过的:先想去哪,再看该怎么走,再算一个不撞人的速度,最后由物理收尾。每层只做一件别人做不了的事,做完把结论交给下一层。图解和配置速查在运动模型那一页;这篇只讲顺序为什么长这样。

Movement runs through four layers, and the order was chosen: decide where to go, then how to go, then a speed that avoids collisions, and finally let physics settle it. Each layer does one job the others cannot, then hands its answer down. The diagrams and the configuration reference are on the movement model page; this post is about why the order is what it is.

第一层:意图 Layer One: Intent

最上面一层只回答"我想去哪":目标在哪,寻路给出哪个方向,用多快走。产出是一个期望速度,方向乘上这个单位的移动速度曲线。它不认识别的单位,也不看障碍;它只把"想去"翻译成一个向量。

The top layer answers one question: "where do I want to go". Where the goal is, which direction pathfinding gives, how fast to travel. Out comes a desired velocity — a direction times this unit's speed chain. It knows nothing about other units and nothing about obstacles; it only turns "I want to go there" into a vector.

第二层:交通规则 Layer Two: Traffic Rules

知道往哪走,不等于能直着走过去。前面有人,你可能得先侧一下再继续;左边已经挤着一个人,就别再往左顶他。所有人都朝同一个缝里挤,最后挤成一个蛋。

Knowing where to go does not mean you can walk straight there. Someone ahead and you may step sideways before pushing on; someone already on your left and you should not lean into them. Let everyone aim for the same seam and the crowd becomes one lump.

这一层是一组各自独立、各自能开关的小规则,每条只做一件事:偏置期望速度的方向。速度大小它们不动(绕行时的提速是单独的乘数),目标也不动。目标还是你想去的那个点,规则只决定你怎么走过去。

This layer is a set of small independent rules, each with its own switch, each doing one thing: bias the direction of the desired velocity. They leave magnitude alone (flanking has its own speed multiplier), and they leave the goal alone. The goal stays the point you want to reach; the rules only decide how you get there.

规则Rule它做什么What it does
侧让 SideStepSide-step前方有挡路者时,把期望速度横向偏一点,让出一条窄缝继续走;偏置量有上限,不是无限躲When something blocks the way ahead, bias the desired velocity sideways to open a narrow lane and keep going; the bias is capped, so it never becomes unlimited dodging
车道 LaneShiftLane shift迎面而来的两股人流各自贴自己那一侧走,形成两条车道Two opposing flows each hug their own side, forming two lanes
绕行 FlankFlank前进走廊里挡着东西(不可移动体、更重的单位)时,整体旋转航向绕开;走廊里相邻阻挡者之间的缝够宽才允许钻,不够宽就绕整簇When the forward corridor holds blockers (immovable bodies, heavier units), rotate the heading around them; a gap between adjacent blockers is threaded only if it is wide enough, otherwise the agent goes around the whole cluster
贴墙 WallSlideWall slide贴着障碍物走时沿表面滑过去,不会被顶停Along an obstacle, slide along its surface; the wall does not stop you dead
生死规则 LivingVsDyingLiving vs dying活着的单位直接从正在倒下的单位身上挤过去,不会排队等它躺好Living units push past a unit that is going down; they do not queue for it to finish falling
仲裁 ArbitrationArbitration先判断"谁该让":按档位(静止 / 归位 / 主动移动 / 攻击中)决定谁做机动、谁被绕过,再决定偏多少Decides who yields first — by rank (idle / returning / actively moving / in combat) — then how much to bias

游戏里那些看着"很聪明"的场面,基本都是这几条规则叠出来的:

The "smart" moments in game are mostly these rules stacking:

你看到的What you see为什么会这样Why it happens
一个方阵穿过另一个静止的方阵,穿过去之后被冲散的人慢慢回到原位A formation walks through a stationary one; the ones it scatters drift back to their posts穿行靠侧让与绕行;能回到原位是归位(Return)的功劳,归位者在避障上走不被意图劫持的那条路,不会被人潮一路拖走Passing through is side-step and flank; the drift back is Return at work, and every returning agent takes the avoidance path that never hijacks its intent, so the crowd cannot drag it away
两股移动的人流对冲,各自让出半边形成车道,丝滑穿过Two moving groups charge at each other, each yields half the width, and they pass smoothly同向超车归绕行,反向对冲归车道:两条规则各管一段,不打架Overtaking a slower same-direction blocker belongs to flank; opposing traffic belongs to lane shift — the two rules split the job cleanly
一辆坦克从方阵里开过去,旁边的步兵让开,过后再站回去A tank drives through a formation; infantry step aside and later return两个方向同时发生:重单位自己用绕行清走廊,轻单位在质量加权的斥力里被推开;让开之后归位把位置拿回来Both directions at once: the heavy unit flanks its own corridor clear, while the light ones are pushed aside by mass-weighted repulsion — and Return gives them their place back afterwards
一大群人拐过墙角,没有都往内挤、把内侧的人压扁A crowd rounds a corner without everyone squeezing inward and flattening whoever is inside缝宽门只放行真正够宽的缝,不够宽就绕整簇;下一层的压力场还会把过密的区块整体推开The gap-width gate only lets an agent thread when the gap is genuinely wide enough; otherwise it rounds the whole cluster, and the pressure field in the next layer pushes over-dense regions apart
一个单位判断前面那个该不该避,然后往侧面挪半步A unit decides whether the one ahead is worth avoiding, then shifts half a step sideways仲裁先按档位定谁该让,侧让再算偏置的方向和大小。不挡路的就不避,路线不因为一个路人改道Arbitration decides who yields by rank, then side-step computes direction and amount. Something that does not actually block is not avoided, so the path does not change for a passer-by

第三层:避障解算 Layer Three: The Avoidance Solve

到这里才第一次真正看"别的单位"。这一层把周围的邻居和它们的速度放进来,解出一个不碰撞的可行速度,同时尽量贴着上一层给的期望速度。判据是"不撞",以及"尽量不偏离初衷"。躲得越远反而越差。

Only here do other units genuinely enter the picture. This layer takes the neighbours and their velocities, solves for a collision-free feasible velocity, and keeps it as close to the desired one as possible. The criteria are "do not collide" and "do not drift from the intent"; dodging farther than needed is a worse answer.

有件事得说清:避障算出来的速度并不保证真的避开。单位改变速度的能力有限,加速度有上限,速度有上限,惯性还很大,想停未必停得住。所以还需要下一层。

One thing has to be said plainly: an avoidance velocity does not guarantee avoidance. A unit's ability to change velocity is limited: bounded acceleration, bounded speed, plenty of inertia. Wanting to stop is not the same as stopping. Hence the next layer.

两种避障:精确的与不精确的 Two Avoidance Solvers: Exact and Rough

交通规则改的是"想怎么走";"这一帧到底用什么速度"由避障求解器算。求解器有两种,单位上二选一,还有一个按状态自动切换的模式。

The traffic rules edit the intent; the solver computes this frame's actual velocity. There are two solvers, chosen per unit, plus a mode that switches between them by state.

求解器Solver它的优势Strength它的代价Cost
ORCA(速度障碍)ORCA (velocity obstacles) 解是解析解:在速度空间里直接算出可行速度,因此低帧率下依然稳,避障质量非常好,窄缝也能干净穿过去It is an analytic solution — a feasible velocity computed directly in velocity space — so it stays stable even at low tick rates, avoids cleanly and threads tight gaps 避障效果"太好了":为了保不撞,它能一直压低你的期望速度,几个人互相礼让到谁都不动;移动意图被吃掉,人群卡住It is too good: to guarantee no collision it keeps lowering your desired velocity, and a few mutually polite agents politely stop; the intent is eaten and the crowd stalls
力场(斥力)Force field (repulsion) 不改动你的期望速度,只额外加一份推开的速度;只要有可能通过,通过性更好,人群不会集体僵住It never rewrites the desired velocity; it adds a separation velocity on top. Wherever passage is possible it passes, and the crowd does not freeze together 它是模拟解——斥力是近似,精度不如解析解,密集或几何刁钻的场合不如 ORCA 干净It is a simulated solution: repulsion is an approximation, less precise than an analytic one, and messier than ORCA in dense or awkward geometry

所以还有第三种用法:按状态切换。追击、攻击、正在绕障碍的单位走 ORCA,它们要精确穿过人群,要把障碍当硬约束。移动中、待机,还有所有归位者走力场,它们最怕被人群裹着走偏。归位者尤其如此:力场不会劫持它的期望速度,它总走得回原位。

Hence a third option: switch by state. Chasing, attacking, or currently rounding an obstacle means ORCA — those cases need to thread a crowd precisely and treat geometry as a hard constraint. Moving, idle, and every returning agent use the force field — what they fear is being carried off course. Returners most of all: the field never hijacks their desired velocity, so they always find their way back.

再加一层:压力场 One More Layer: The Pressure Field

力场是一对一的:它只看见附近的邻居,看不见"这一整片已经挤满了"。所以又加了一层压力场。每个单位每帧把自己的占位投进一张世界格网,格网把密度抹匀,再把密集的区块整体往外推。

The force field works one against one: it sees nearby neighbours, not that this whole patch is packed. So there is one more layer, a pressure field. Every unit deposits its footprint into a world grid each frame; the grid smooths the density out and pushes over-dense regions outward as a whole.

它补的就是力场看不见的那块:两两斥力可以互相抵消,你推我、我推你,谁都不动;压力场看的是区块密度,能把"挤成一坨"变成"散开"。它只在力场那条路上生效,ORCA 自己已经有速度空间约束,不用再叠一层。

It covers exactly what the field cannot see: pairwise pushes can cancel out (I push you, you push me, nobody moves), while the pressure field reads density by region and turns "one packed lump" into "spread out". It only runs on the force-field path; ORCA already carries a velocity-space constraint and needs no extra layer.

这几层各管一段:规则管意图的方向,求解器管这一帧的速度,压力场管区块的密度。哪一层做过头,下一层会兜住。它们叠在一起工作,不会互相打架。

Each layer owns a stretch: the rules own the direction of intent, the solver owns this frame's velocity, the pressure field owns density by region. When one overreaches, the next absorbs it. That is how they work together instead of fighting.

第四层:物理 Layer Four: Physics

最后一层不讲道理:两个东西不能重叠,就是不能重叠。物理规则是死的,没有通融的余地。上面几层想让它怎么走都没用,重叠了就得分开。它也是唯一有权力否定上面所有结论的一层。

The last layer does not negotiate: two bodies must not overlap, so they do not overlap. Physics is a dead rule with no exceptions. Whatever the layers above intended, an overlap gets separated. It is also the only layer with the authority to overrule all of them.

它排在最后,因为它最不可让步:最后一句话必须是确定的,前面所有"尽量"都要在这里收敛成一个答案。

It sits last because it concedes least: the final word has to be definite, so every "as far as possible" collapses into one answer here.

为什么这个顺序不能换 Why the Order Cannot Change

这套顺序的关键,是每层只解决自己能解决的那件事。换个顺序,问题马上看得出来:

The order works because each layer solves only what it can. Swap anything and the problem is immediate:

如果换掉If you swap会坏在哪What breaks
交通规则挪到避障之后Traffic after avoidance 避障已经按"直着走"算完了,你再去改写方向,等于否定它的解;重算一遍就是两层互相打架Avoidance already solved for a straight intent; rewriting the direction afterwards negates its answer, and recomputing makes the two layers fight
物理挪到避障之前Physics before avoidance 物理只会在已经重叠时才动手,没有速度层面的预判;等于放弃"主动不撞",只剩"撞了再分"Physics only acts once bodies already overlap; it has no velocity-level foresight. You give up avoiding and keep only separating
把四层压成一层Collapse all four into one 每一层都要在自己的目标上让步给别的层,最后谁都没有明确责任——调一个参数不知道会改坏哪件事Each concern must yield to the others, and nobody owns a clear responsibility — you change one knob and cannot tell what else it breaks

为什么结果看起来"自然" Why the Result Looks Natural

因为每一层都对应一件现实中确实分开发生的事:决定去哪,按当时的规矩调整走法,躲人,身体之间不许穿模。人走路就是这个顺序。你不会故意去"避障",你只是不想撞上;身体自己不允许重叠。

Because each layer matches something that genuinely happens in that order: decide where to go, adjust the way by the local conventions, dodge people, and finally bodies do not interpenetrate. Walking works the same way. You do not consciously "run an avoidance solve"; you simply do not want to collide, and your body refuses to overlap.

顺序做错,症状也很具体:单位绕过了障碍,却贴着人蹭过去;在门口堆成一团;被推一下会抖。根因都是层与层的责任混了。

Get the order wrong and the symptoms are just as concrete: units round an obstacle but slide along people, pile up in a doorway, jitter when pushed. None of it is "a parameter that needs tuning"; the responsibilities have bled between layers.

没有覆盖一切的算法 No Algorithm Covers Everything

先说一个体量上的事实:UE 5.6 的 CharacterMovementComponent.cpp 有 13,658 行,用它的人照样碰得到它解决不了的情况。我们的移动处理器是 9,792 行,定点版本另有一份,9,645 行;加上交通规则、避障参数和物理结算,整套还要更大。它也不可能涵盖所有状况。你可以把我们这套东西当成一个"Mass Movement Component":干一样的活,只是以处理器的形式存在。

Start with a size fact: Unreal 5.6's CharacterMovementComponent.cpp is 13,658 lines, and people still run into situations it cannot solve. Our movement processor is 9,792 lines (the fixed-point variant is another 9,645 lines), and with traffic rules, avoidance parameters and collision resolution on top, the whole thing is bigger still. It also cannot cover everything. You can read what we built as a "Mass Movement Component": the same job, delivered as a processor.

这是问题本身的性质:每个单位只看得见身边的一点点信息,只跑一条简单的规则。局部信息求不出全局最优,本来就不可能。现实里广场上人人都有脑子,照样会踩踏;个体再聪明,群体行为也不会自动变成全局最优。

That is the nature of the problem: every unit sees only a little of what surrounds it and runs one simple rule. A global optimum cannot be derived from local information. In the real world every person in a plaza has a brain and stampedes still happen; individual intelligence does not turn crowd behaviour into a global optimum.

所以目标是用尽量合理的规则,收敛到一个多数情况下看起来都对的状态,而且得跑得动。群体移动里,性能预算和对错一样重。

So the goal is converging, with rules as sensible as we can make them, to a state that looks right most of the time, and it has to run. In crowd movement the performance budget matters as much as being correct.

遇到问题,能做的就三样:加一个参数、调一个参数、加一条规则。判据也朴素:你的场景能跑对就够了,不用纠结它适不适用于所有场景。那样的运动算法不存在;声称它存在,本身就不合逻辑。

So when something misbehaves there are three moves: add a parameter, tune a parameter, or add a rule. The bar is modest: if your scenario behaves, that is enough. Do not agonise over whether it fits every scenario. No movement algorithm does, and claiming one exists is simply not logical.

把边界写在最后,是因为知道边界在哪,才知道下一个问题该往哪加:意图不对、规则不够、求解器选错,还是这一档预算本来就撑不住这种密度。

The limit goes last because knowing where the boundary is tells you where the next fix belongs: the intent is wrong, a rule is missing, the solver is the wrong one, or the budget cannot hold this density at all.

AI 来了,还要不要抠这些算法? Now That AI Is Here, Is Hand-Tuning Still Worth It?

现在有两类模型在一起往前走。一类是生成文字的 LLM;另一类是专门为动作训练的模型。它不产出文本,直接输出速度、方向或动作参数,学的是"这个单位下一步该怎么动"。早期的运动合成中间件就是这个思路,今天的学习式运动策略走得更远。于是有个很自然的问题:模型都能自己学会走路了,move processor 里这些手写规则还值不值得抠?

Two kinds of model are advancing side by side. One generates text. The other is trained for motion: it produces no prose, it outputs a velocity, a direction, or motion parameters, and what it learns is "how this unit should move next". Early motion-synthesis middleware took this route; today's learned locomotion policies go further. So the fair question: if a model can learn to walk on its own, is it still worth hand-tuning the rules inside a move processor?

答案是要,因为生物体天生就是分层的,而且一层都不能省:

The answer is yes, because a living body is layered by construction, and no layer may be dropped:

层Layer干什么What it does特点Its character
脊椎反射Spinal reflex 碰到烫的东西先缩手,脚下打滑先稳住Pull the hand back from something hot, steady the footing when it slips 最快,也最无脑Fastest, and the least clever
小脑Cerebellum 平衡、协调、把动作接顺Balance, coordination, making the motion continuous 有一点智能,但不做高级决策Some intelligence, no high-level decisions
大脑皮层Cerebral cortex 宏观决策:去哪、打谁、撤不撤Macro decisions: where to go, whom to fight, whether to withdraw 最慢,但做的是别层做不了的事Slowest, and it decides what no other layer can
群体层Group layer 一个班里有队长下命令,大家执行In a squad the leader issues orders and everyone executes "更大范围的一层"A wider layer

关键在最后一条:有了宏观命令,微观的条件反射不会消失,也不该消失。一个班收到"前进",每个人的侧身、保持间距、别踩到前面的人,还是当场在自己身上发生。把这几层换成一条"前进"的指令,队伍会挤成一团。层数越全,效果越好:底层把这些事接住,顶层才只需要管顶层的事。

The point is in the last row: a macro order does not remove the micro reflexes, and must not. When a squad is told to advance, each soldier's side-step, spacing and not-treading-on-the-man-ahead still happen locally and immediately. Replace those layers with the single order "advance" and the formation collapses into a lump. More layers gives a better result: the bottom catches all of this, so the top can stay on the top.

我们这些算法打的就是脊椎和小脑那一档。规则写死在代码里,做过性能优化,每单位每 tick 只跑几条判断,效率高,延迟低。模型决策层给不了的是确定性,和一个有上界的延迟。底层就该便宜、可靠、反应快;这是地基,谈不上退让。

What our algorithms target is exactly the spinal-reflex and cerebellum tier. The rules are written out in code and tuned for performance: a few comparisons per unit per tick, which is where the throughput and the very low latency come from. A model-driven decision layer cannot supply determinism, or a latency with a bound on it. The bottom layer should be cheap, reliable and quick to react; that is the foundation, not a concession.

把账算清楚会更直观。一次局部避障,每个单位要做的是:查身边有谁,算几个方向和距离,做几次加减乘除和比较。几十次浮点运算,定长,基本无分支,可以向量化。一次神经网络推理,哪怕是很小的策略网络,也是至少上千次乘加;模型稍大就是几十万到上百万。每单位每次的运算量差好几个数量级,这差距靠优化抹不平:差在算法的形状上,跟实现得好不好关系不大。

Run the arithmetic and it is concrete. One local-avoidance pass per unit: find the neighbours, compute a few directions and distances, do a few adds, multiplies and comparisons — tens of floating-point operations, fixed-size, nearly branchless, vectorisable. One neural-network inference, even a small policy net, costs at least thousands of multiply-accumulates; a slightly larger model lands in the hundreds of thousands to millions. The work per unit per tick differs by several orders of magnitude, and tuning does not close that gap: the difference is in the shape of the algorithm, not in how well it is implemented.

搬到 GPU 上,这个比例也不变。GPU 给的是并行度,而我们的求解本来就是并行的:Mass 的 chunk 遍历,全内联,无分支。换一个执行器,只是把同一份工作量挪个地方跑,一点没少。更别说每 tick 把模拟状态搬进显存、再把结果搬回来,这份带宽和延迟开销正好打在"反应要快"上。

Moving to the GPU does not change the ratio. A GPU supplies parallelism, and this solve is already parallel: Mass chunk iteration, fully inlined, branchless. A different executor runs the same work somewhere else, not less of it. Then add the per-tick round trip of simulation state into VRAM and results back out; that bandwidth and latency lands straight on the "react fast" requirement.

所以完全不谈确定性,只算算力,用模型替底层求解也赢不了:它每次要算的东西,比这件事本身需要的多几个数量级。模型该待的地方,是那些算得少、想得多的决定:去哪,打谁,什么时候撤。

So even leaving determinism aside, on compute alone a model cannot win the low-level solve: what it computes each time is several orders of magnitude more than this job needs. A model belongs where decisions compute little and think a lot: where to go, whom to fight, when to withdraw.

有一个反驳肯定会来,先答掉:把学到的避障策略蒸馏成两三层的小网络,每次推理也就几百个乘加,"算不起"就不成立了。这条反驳不伤结论,反而把话说得更准:便宜到能进"每帧每单位"的模型,行为和手写规则会落在同一个复杂度档位。真聪明的那一类,代价差着几个数量级。所以问题只是在同一根轴上选位置:一端是几十次运算的条件反射,另一端是上百万次运算的判断力。底层站这一端,这是分工。

One objection will certainly come, so answer it first: distil the learned avoidance policy into a two- or three-layer net and each inference is a few hundred multiply-accumulates, so "cannot afford it" stops being true. That objection leaves the conclusion intact and sharpens it: a model cheap enough to run per unit per frame lands in the same complexity band as hand-written rules, while the genuinely clever ones cost orders of magnitude more. So the real question is where on that axis you sit: tens of operations for a reflex at one end, a million for judgement at the other. Standing at this end is division of labour.

上面那层恰好留好了插口:意图层吃的就是一个期望速度,方向乘上速度曲线。这个向量由谁产生,是上层的事。行为状态机可以,队长下命令可以,LLM 给的宏观安排可以,专门训练的动作模型也可以。规则层不认识上面是谁,也不在乎;它只负责把这个向量安全、稳定、便宜地落到地上。

The layer above already has its socket: the intent layer consumes a desired velocity, a direction times a speed curve. Who produces that vector is the upper layer's business: a behaviour state machine, a squad leader's order, a macro plan from an LLM, or a purpose-trained motion model. The rule layers neither know nor care which one; they only have to land that vector safely, stably and cheaply.

所以这条路是把层数叠全:底层越便宜、越确定、延迟越低,上层能放心交出去的事就越多。反过来,把底层求解交给一个又慢又不确定的模型,等于把整个群体的行为押在一处。上一节说没有覆盖一切的算法,这里补上后半句:也不需要有一个,层是可以叠的。

So the road is to stack every layer: the cheaper, more deterministic and lower-latency the bottom is, the more the top can safely be handed. The reverse, putting the low-level solve on a slow, non-deterministic model, stakes the whole crowd's behaviour on one place. The previous section said no algorithm covers everything. The other half: none has to, because layers stack.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels