物理是引擎算出来的错觉 Physics Is an Illusion the Engine Computes
幻象 · ECS 底座 · 平行世界 · 你的 Agent AI · 2026-12-01The illusion, the ECS substrate, the parallel world, and your agent AI · 2026-12-01
系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →
那种"理所当然"的感觉 The Feeling That Comes Free
刚摸游戏引擎的人,包括当年的我,多半都会有这种直觉:一个物体摆到地图上,它就该有碰撞,就该能移动,就该看得见、查得到。这些性质来得太自然,自然得像东西自带的一样。
Anyone new to a game engine, me included back then, picks up the same instinct: put an object on the map and of course it collides, of course it moves, of course you can see it and query it. The properties come so naturally they feel like part of the object.
底下其实什么也没有。没有运动,没有物理,只有数据和计算。物体"能撞上别人",是因为引擎每一帧用最基础的加减乘除维护一张空间结构,算出谁在谁旁边;"能动",是因为有个函数在积分位置;"看得见",是因为绘制列表里有它。那套符合直觉的真实感,是引擎用算法和判断搭出来的幻象。游戏世界像是天然继承了物理世界的性质,可这份"天然"本身就是写出来的。
At the bottom there is nothing. No motion, no physics, only data and computation. An object "can hit others" because the engine maintains a spatial structure out of plain arithmetic every frame and works out who is next to whom. It "moves" because some function integrates its position. It "is visible" because a draw list contains it. The realism that feels so obvious is an illusion the engine assembles from algorithms and decisions. It looks as if the game world inherited the properties of the physical one; that inheritance is written.
这层底座,OOP 写得出,ECS 写得快 The Substrate: Writable in OOP, Faster in ECS
搭起这个幻象的那一层,我叫它引擎的基础设施:运动解算、避障、物理解算,再加一道配套的空间检测。空间检测严格说不算 ECS,但你一旦用 ECS 写运动、写物理,这道结构就得自己搭。想避障,先得把邻居查出来;想解算碰撞,先得把周围的碰撞体查出来。整套东西的地基就是它。
The layer that puts the illusion together is what I call the engine's infrastructure: movement solving, avoidance, physics, and the spatial detection that comes with them. Strictly speaking spatial detection is not ECS, but the moment you write movement and physics in ECS you build that structure yourself. Avoidance needs neighbours found; collision solving needs the colliders around you found. Everything else stands on it.
这层基础设施,OOP 写得出来,ECS 也写得出来,而且 ECS 快得多。"一大批同类数据做同一件事"本来就是 ECS 吃饭的本事。写完以后在顶上封一层 OOP 接口,用起来和原版没差别。完全可以照 CharacterMovementComponent 造一个 Mass Movement Component:用户调 AddMovementInput,请求先收集、入队,到这一帧固定的结算阶段统一解算,还全是并行。你看到的仍然是"调了函数,物体动了,撞上别人了",底下那趟批处理一点也感觉不到。我们就是这么干的,运动那篇把这条流水线拆成了四层。
OOP can write this layer and ECS can write this layer, but ECS writes it far faster. "One batch of same-shaped data doing one job" is exactly what ECS is for. Once it is written you wrap an OOP interface over the top and it feels like the original. You can build a Mass Movement Component that mirrors CharacterMovementComponent: the user calls AddMovementInput, the request is collected and queued, and a batched, fully parallel pass settles it at a fixed stage of the frame. From outside it still looks like "I called the function, the thing moved, it hit the other thing", and the batching underneath never shows. That is how we built it; the movement post takes that pipeline apart into four layers.
UE 自带的那套是为它自己造的 The Stack UE Ships Was Built for Its Own Game
每种算法都有它适用的场合,这里没有优劣,只有合不合用。UE 自带的那套 Gameplay 框架,我感觉就是照它自己的场景造的:虚幻竞技场那种,十几号人在一张图里大乱斗,子弹和特效满天飞。十个人能打多少发子弹?能放多少特效?量是有数的。在撑住这个量的前提下,能多灵活就做多灵活。这是它的取向,而且它做到了。
Every algorithm has a place it fits. There is no better or worse here, only fit or not. The Gameplay framework UE ships feels built for its own game: Unreal Tournament scale, a dozen figures brawling on one map with bullets and effects everywhere. How many bullets can ten people fire? How many effects can be live? The number is bounded. Inside that budget, be as flexible as you can be. That was the aim, and it hit it.
所以引擎自带的那套如果合你的游戏,直接用,后面讲的事你都不用操心。等它撑不住你的规模,再往下看。
If the stack the engine ships fits your game, use it and stop reading; nothing after this point is your problem. The next section starts where it stops holding.
十万级:写一件没有用,要写就写一套 At 100K, Half a Set Is No Set
可你要模拟上百、上千、上万、十万个单位,那套框架就撑不住了。它本来就是为另一个量级开发的,撑不住很正常。到这一步,你得跟 MassBattle 一样,从零手撸整个世界模拟。
Push to hundreds, thousands, tens of thousands, a hundred thousand units and that framework gives way. It was built for another order of magnitude, so of course it does. At that point you hand-roll the whole world simulation, the way MassBattle does.
只写其中一件没有用。引擎那几个 component 读的都是自己那一套:CharacterMovementComponent 找地面走的是引擎的物理查询,读的是引擎登记的碰撞体,它不会来读你的。所以要写移动和避障,你得有自己的空间检测结构,从里面查邻居;要写碰撞和物理解算,还得从里面查周围的碰撞体。四件事扣在一起,一件都省不掉。
Writing one piece of it buys you nothing. The engine's components read their own kind. CharacterMovementComponent finds floors through the engine's physics queries and reads colliders the engine registered; it will never read yours. So your own spatial structure has to find neighbours before movement and avoidance can exist, and find the colliders around you before collision and physics solving can. Four things chained together, and none of them is optional.
写完之后,你和 UE 那套就是两个平行世界。同一个关卡、同一张地图,屏幕上看不出接缝,底下的东西完全不同,各过各的。
Once it is written you and UE's stack are parallel worlds. Same level, same map, no visible seam on screen, two completely different machines underneath.
五次空间查询对一次 Five Queries Against One
平行世界得拿得出平行世界的成绩。MassBattle 的运动解算、物理碰撞、空间检测都下过很深的性能功夫,先看一组数字。UE 5.6 的 CharacterMovementComponent.cpp 有 13,658 行,一次移动更新要付五次空间查询:扫地板一次,打一条线确认,移动本身再扫一次,撞上东西贴墙滑再扫一次,上台阶还补一次。奢侈。我们每个单位每帧一次空间查询,运动品质到它的九成以上。
A parallel world has to prove itself. MassBattle's movement solving, collision and spatial detection have been through heavy optimization, so here are the numbers. UE 5.6's CharacterMovementComponent.cpp runs 13,658 lines. One movement update there pays five spatial queries: sweep the floor, fire a line trace to confirm, sweep the move itself, sweep again as you slide along a wall, and one more to step up. Luxurious. We pay one query per unit per frame, and the movement quality lands above ninety percent of theirs.
最后一成的品质,成本要翻倍,再往上就是翻倍再翻倍。为这点观感多花几倍的时间,我们觉得不划算。所以运动加物理解算这一块,我们至少是那套的十倍,场景顺的时候能到一百倍,代价是精度上让掉一部分。没有放之四海皆准的方案,要规模,就得在灵活性和品质上让步。每个引擎都在做这种取舍,各自停在自己觉得值的位置上。
The last tenth of quality costs double, and doubling again beyond that. Spending several times as much for that last stretch of feel is a trade we passed on. So our movement plus physics solving runs at least ten times that stack, up to a hundred on scenes that cooperate, with some precision given up along the way. Nothing fits every scenario: scale is bought with flexibility and quality. Every engine makes this trade and parks where it feels right.
UE 也在往这个方向走 UE Is Moving the Same Way
UE 自己的动向已经把这一步的答案摆出来了。UE6 从 Early Access 起不再带那套 Gameplay 框架,Blueprint 和 C++ 的 gameplay 框架都留在 UE5 那条线上;底下的世界模型换成 Scene Graph 这样的实体组件模型,组件按类型成批存放,用户逻辑交给 Verse 写。最后成色如何,我持审慎态度:Epic 那套 Gameplay 框架历来性能一般,这次换的是汤还是药,得看落地。方向本身倒是印证了这篇的判断,底层基础设施在往批处理、往 ECS 这类结构上搬。一个引擎哪怕只把运动、物理、空间检测这三大块换成这种结构,能承载的量都会上一个台阶。数字在上一节,方向不难猜。
UE's own move supplies the answer publicly. From Early Access on, UE6 ships without that Gameplay framework; Blueprints and the C++ gameplay framework stay on the UE5 line, the world model underneath becomes an entity-and-component one like Scene Graph, components are stored by type in batches, and user logic is written in Verse. I am cautious about the final quality: Epic's gameplay stack has never been fast, and whether this change is medicine or just new packaging will show when it lands. The direction, though, backs this post's claim. The infrastructure layer is moving toward batch processing and ECS-shaped structure. Swap even the three big blocks, movement, physics and spatial detection, into that shape and an engine carries a whole step more. The numbers are in the previous section; the direction is not hard to call.
我们的做法:行为和反应也是 ECS Our Call: Behaviour and Reaction Are ECS Too
MassBattle 走得更彻底:所有逻辑全用 ECS 写,包括 Agent 的 Behavior 和 Reaction。这两样东西用 OOP 写也完全可以,说到底就是"每个单位想一想今天干什么",你单写没人拦得住。但用 ECS 写,它们就和移动、物理一样跑多线程,而性能最大化是我们的第一目标。
MassBattle goes further: every piece of logic is ECS, including agents' Behaviour and Reaction. Both could be written in OOP. At heart they are "each unit thinks about what to do today", and nothing stops you writing them on their own. In ECS they run multi-threaded like movement and physics, and maximum performance is our first goal.
还有个原因:我的用户里,挺多要的就是数量和规模——屏幕上真有十万个人在动——对 Agent 行为本身没那么高的要求。内置的 Behavior 处理器和 Reaction 处理器就是给这种场景垫底的:开箱能跑,够用就停。这不等于插件做不了复杂的 AI。行为要写多细都行,三层结构、fragment、处理器都能往下加,真要定制直接改源码。三层各管什么、怎么打断,另一篇讲得很细。
One more reason: plenty of my users are after quantity and scale — a hundred thousand figures actually moving on screen — and ask little of the agents' behaviour. The built-in behaviour and reaction processors are the baseline for that case: they run out of the box and stop where good enough stops. That does not mean the plugin cannot do complex AI. Behaviour can be as deep as you want: the three layers, fragments and processors all extend, and real custom logic means editing the source. What each of the three layers owns, and how interruption works, is covered in detail in another post.
你的 AI 有三条路 Your AI Has Three Roads
没人规定你的 Agent 非得用我们写好的那两个处理器。"想"这件事,路有三条:
Nobody says your agents have to live in the two processors we wrote. For the thinking part there are three roads:
| 路Road | 怎么走How you go | 适合谁Who it fits |
|---|---|---|
| 内置的 Behavior / Reaction 处理器The built-in behaviour / reaction processors | 在 AgentConfig 上配好,开箱可用,跟模拟一起多线程跑Configure on AgentConfig, works out of the box, multi-threaded with the simulation | 大多数"够用就行"的玩法;十万级照跑Most "good enough" gameplay; still fine at 100K |
| Coding agent 改源码A coding agent edits the source | 把想要的行为或反应直接写进处理器,照仓库里已有的形状来(配方在这篇)Write the behaviour or reaction you want straight into the processors, in the shape the repo already uses (recipe in this post) | 要定制行为,同时要保住十万级Custom behaviour while keeping the 100K scale |
| OOP,Actor 当大脑OOP, an Actor as the brain | Actor 挂 MassBattleAgentComponent,实体世界把模拟结果同步到 Actor;移动调 AddAgentMovementInput,检测用 Trace Entity 节点Put MassBattleAgentComponent on the Actor, the entity world syncs the simulation to it; move with AddAgentMovementInput, detect with the Trace Entity node |
要完全自由的逻辑,规模在几百到几千Completely free logic, at a scale of hundreds to a few thousand |
第三条路值得多写几句,它最像你熟悉的 UE 开发。给 Actor 挂上 MassBattleAgentComponent,这个组件就在 Actor 和一个 Mass entity 之间搭好了桥:entity 世界每帧把运动、避障、物理解算的结果同步到 Actor 的 transform 上。Actor 当大脑,感知、决策、反馈全由你写。想让它动,调 AddAgentMovementInput,它对标的就是 AddMovementInput:请求先攒着,到这一帧的结算阶段由移动处理器统一消费。想检测周围,用 Trace Entity 节点,球、扇形、盒子、胶囊都行,一次查询拿回候选表。等于把"调 UE 那套"换成"调 MassBattle 这套",写代码的思路一点不用改。
The third road is worth the space because it feels like ordinary UE work. Put MassBattleAgentComponent on an Actor and it bridges the Actor to a Mass entity: every frame the entity world syncs movement, avoidance and physics results onto the Actor's transform. The Actor is the brain; perception, decisions and feedback are yours. To move it, call AddAgentMovementInput. It is the counterpart of AddMovementInput: the request accumulates first and the movement processor consumes it at the frame's settlement stage. To sense around it, use the Trace Entity node with a sphere, sector, box or capsule, and one query gives you the candidate list back. So you swap "calling UE's set" for "calling MassBattle's set" and nothing about how you write code changes.
第二节里设想的那个 Mass Movement Component,在这条路上是真的:纯数学的一半,物理、碰撞、运动、避障,交给底层基础设施;"想"的一半留在你手里。灵活归你,性能也归你。
The Mass Movement Component imagined in the second section is real here. The pure-math half, physics, collision, movement and avoidance, goes to the infrastructure underneath; the thinking half stays with you. You get the flexibility, and you keep the performance.
规模尺:三条路各撑多大 The Ruler: What Each Road Carries
三条路的差别最后落在规模上。前两条住在模拟里,跑的是同一套多线程处理器,十万级是它们的家。第三条一个大脑一个 Actor,Actor 的账一笔都躲不掉:几百到一千个很宽裕,几千要看你 Actor 里写了什么,上万就别指望,十万想都不用想。
Where the roads differ is scale. The first two live inside the simulation and share the same multi-threaded processors, and 100K is home. The third carries one brain per Actor and pays every Actor cost: a few hundred to a thousand is comfortable, a few thousand depends on what your Actors do, tens of thousands is out of reach, and a hundred thousand is not worth a thought.
按规模选。要十万级,走前两条;要几百个,但要非常个性的逻辑,第三条写着最舒服。三条路不互斥:主力部队走模拟,几十个剧情单位各挂一个 Actor 大脑,同一场战斗里混着用,很正常。
Choose by scale. 100K means the first two. A few hundred units with very particular logic is where the third is most comfortable. The roads do not exclude each other: the main force in the simulation and a few dozen story units each wearing an Actor brain, mixed in one battle, is a perfectly normal shape.
一句话 In One Line
引擎里的"物理"本来就是算出来的错觉:要么用别人算好的那一套,要么自己把算的这一套写齐。写齐了换规模;"想"的部分三条路都给你留着,哪条顺手走哪条。
The "physics" in an engine is an illusion computed into existence. Take the set someone else computed, or write the whole set yourself; writing it buys you scale. For the thinking, three roads stay open, and you take whichever one your hand reaches for.