具有增量回滚的多人游戏物理引擎
A Physics Engine with Incremental Rollback for Multiplayer Games

原始链接: https://easel.games/blog/2026-rollback-physics

## Easel 新物理引擎实现更大更复杂的游戏 Easel 传统上受其预测性多人架构限制,世界尺寸受到限制——之前的物理引擎需要每帧进行完整世界快照和回滚。现在,一个**定制物理引擎**改变了这一切,它只对*变化*的元素进行快照和回滚,从而实现《Among Us》规模及更大的游戏。 这得益于几个关键特性:**休眠物体**(忽略静态物体)、优化的**空间索引**以实现高效的碰撞检测,以及一种新的**步进**方法,它消除了不需要的“弹跳”而不会牺牲击退效果。Easel 还集成了**连续碰撞检测**,以实现快速移动物体之间的精确交互,并允许物体即使*没有*碰撞体也能移动——这对于简单的视觉效果很有用。 该引擎建立在开源 Parry 库之上,但专门针对 Easel 的需求进行了定制。结果是一个更高效的系统——将快照数量减少了 30-50 倍——为更大、更详细、更引人入胜的多人游戏体验铺平了道路。本质上,Easel 现在只快照*需要*快照的内容,从而释放了真正广阔的游戏世界的潜力。

一位开发者(BSTRhino)创建了一个新的物理引擎,专为多人游戏设计,旨在解决当前回滚网络代码实现中的局限性。 传统的回滚需要每帧进行完整状态的快照,这使得大型、动态的世界变得不切实际。 这个新引擎使用了**增量回滚**,只快照物理状态的*变化*。 这大大减少了数据开销,有可能实现大型世界——特别是那些大部分元素是静态的世界,例如飞船或建筑的内部。 该引擎是 easel.games 平台的一部分,但用它构建的游戏*可以*导出到 itch.io 等平台,尽管多人游戏/排行榜功能将依赖于通过嵌入式 iframe 通过 easel.games 进行托管。 该开发者分享了一篇帖子,详细介绍了引擎创建过程中获得的发现。
相关文章

原文

We want Easel to be powerful enough to make the kinds of games you would play for hours. Popular multiplayer games like Among Us let you walk around an entire spaceship, completing tasks and evading impostors. Unfortunately, up to this point, games of that scale were out of reach for Easel, because the off-the-shelf physics engine would have to snapshot and roll back the entire world to support Easel's predictive multiplayer architecture. It's too much to do every frame.

Until this point, you were required to keep your world small. But not anymore!

Easel's new custom-built physics engine only snapshots and rolls back the parts of the world that change. That big spaceship might have thousands of objects forming the walls, the control panels, the vents, and so on. However, each frame, a surprisingly few number of objects actually change - perhaps less than 30 per frame as the players walk around and interact with the world. A smart implementation keeps objects sleeping while they are offscreen.

With only 30 objects out of thousands needing to be snapshotted each frame, a factor of 30-50x fewer than before, multiplayer Easel games with large worlds suddenly becomes feasible. Release the feral hogs!

Under the Hood

Easel's new physics engine is custom-built for Easel, which is why it supports everything Easel supports, but better! Here is a tour of some of the features that make it special.

Sleep

The fastest way to do something is to not do it at all. When a body is asleep, it does not require any snapshotting, rollback, or any physics calculations until it wakes up again. Unlike other engines which wait for a few seconds of inactivity, Easel puts bodies to sleep immediately when their velocity reaches zero (within a small epsilon threshold, of course, we're not slow).

One tricky case here is gravity, which has the potential to keep your entire world awake with its constant nagging force. Easel tracks the forces and reaction forces on every object and can see whether they are balanced or unbalanced. Regardless of whether it is at zero velocity, as long as one body in a stack has unbalanced forces, the stack has not settled into equilibrium and so the whole stack stays awake.

Spatial Indexing

Like many other physics engines, Easel's broad phase uses a Bounding Volume Hierarchy (BVH) to quickly find potential collisions. Easel's BVH algorithms are optimized to minimize unnecessary snapshotting and rollback, only performing incremental rebalancing when the tree is already undergoing change. Do you only do the vacuuming just before your friends come over? Easel's BVH is lazy efficient just like you.

One additional trick is Easel's BVH also tracks the Categories of each collider, which speeds up common game queries dramatically. It is very common, for example, for a bot to target the nearest player, and if the player is on the other side of the map, it would have to traverse through every single collider in the world to find the nearest player. Finding the nearest Category:Needle amongst a sea of Category:Haystack colliders is a lot faster with a metal detector!

Stepping

Making a character take a step is something so common but surprisingly complex in physics engines. A common method is to add velocity for the step, then subtract it after the physics simulation is complete, but a problem arises when the character collides with an obstacle mid-step.

Even if the physics engine does its job correctly and zeroes out the velocity, later on when the previously-added step velocity gets subtracted, it reintroduces the bounce back that the physics engine just zeroed out. It makes your character feel really bouncy.

  • Some games fix this by damping out all velocity, which removes bounce back but also removes knockback too, removing some of the fun and feel of the game. Walk into a wall? No bounce, good. Get hit by a fireball? No knockback. Seems wrong.
  • Other games fix this by creating a kinematic character controller based on raycasting. In physics engine terms, kinematic (as opposed to static or dynamic) means that the character is not affected by forces and collisions. In other words, the physics engine has failed to produce the desired results, and so it is now being bypassed entirely. You had one job physics engine. Physics. Do it. Please. Not not like that. Okay fine I'll do it myself.

Non-bouncy stepping has been built directly into the solver of the Easel physics engine. Simply use ForcefulStep with the new restitution=0 parameter, and Easel's new physics engine will make sure the step does not bounce back.

How does it work?

The trick is, Easel treats stepping in a similar way to how it corrects for position overlap.

Have you ever played a game where your character gets stuck in a wall, and the physics engine tries to eject you, sending you flying across the map? It is a common physics engine glitch. Super Mario 64 speedrunners famously love to make him slide backwards up the stairs on his backside using a glitch like this. Ow.

Modern physics engines try to avoid this problem by solving for position correction separately. The force that would eject your character out of the wall is applied immediately to the position without changing your velocity, which means the ejection velocity does not last beyond the current frame.

ForcefulStep in Easel is implemented as part of position correction, which is why it does not cause bounce back, unless you want it to. We're killing two (angry) birds with one stone!

In reality, it is a bit more complicated for numerous reasons. Easel first solves for both ejection+stepping and velocity at the same time, then we store that ejection velocity, then we remove the ejection+stepping constraints and solve again, storing the stabilized velocity. At this point, nothing has moved yet, which is why we are now free to sweep for the next time-of-impact using the correct velocities. When it comes time to take a step, we use the ejection velocity, but at the end of the frame we only commit the stabilized velocity and so the bounce disappears.

In other words, Easel collects all the data it needs up front without making changes, so that when it does make a move, it can make the correct one.

Continuous Collision Detection

We love it when two fireballs collide with each other in mid-air.

Finding the collision between two fast-moving objects like this requires continous collision detection, because if we just checked for collisions once every frame, we might miss the precise moment when the two fireballs overlap.

Continuous collision detection is Easel is performed by sweeping and then shape casting like other physics engines, but in doing this we noticed a few differences between Easel other physics engines which may be interesting to some nerds game developers:

  • The Rapier physics engine can produce an incorrect result when the fireball begins the frame touching a mirror. In Easel, collisions are resolved first, causing the fireball to bounce and change direction, and only then does it search for the time-of-impact of those two fireballs. In Rapier, the time-of-impact is searched for first using the original velocities, meaning that in this case, it starts by sweeping that fireball in the wrong direction. This difference happens because Rapier does the position integration early, committing to a substep length before it knows what the time of impact is going to be. Easel stores enough information up front that it can do the position integration after it knows the time of impact, avoiding this problem.

  • Box2D 2.4 does sweep using the correct velocities but takes a different approach of doing the full normal physics simulation, then backtracking, which is why all collisions are already resolved before continuous collision detection. Interestingly, the new Box2D 3.0 does not support dynamic-to-dynamic continuous collision detection at all, meaning those two fireballs would have no choice but to miss each other like ships in the night. Perhaps this is a future direction for Box2D as, besides this, it seems they have achieved the holy grail of avoiding substepping at all by using speculative contacts.

  • Photon Quantum, a professional multiplayer rollback netcode engine, does not support continuous collision detection at all, citing that their physics engine is stateless and so it would be too expensive. It seems incremental snapshotting and rollback of our physics state has enabled Easel to support this feature performantly.

Bodies can move themselves

One inconvenient edge case with the previous physics engine is that a body with a velocity or turnRate and no colliders would not move at all.

It could be argued that this is correct. If there is no mass to hold momentum, there's no movement, but we are not just making a physics engine, we are making a game engine. Sometimes bodies are just a way to group sprites together, and the physics is not important.

Now if you give a body a velocity or turnRate, it will move itself even if it has no colliders. Attach a TextSprite and you could have a simple billboard floating up the screen, scrolling away to a galaxy far, far away.

Sidenote: Photons don't have mass but they still have velocity, in fact there are 10^17 of them hitting your eyes right now every second, so maybe some physics engines need a reality check.

Thanks to

Easel's physics engine is built upon the collision detection algorithms of Parry, an excellent open source library created by Dimforge that powers the Rapier physics engine.

Making a physics engine has been a huge endeavor. Many little decisions have been made to make implement it in a way that is neat, efficient and works well with Easel's multiplayer architecture. Now everything, not just the physics engine, but all parts of Easel only snapshot and rollback the parts that change, meaning you can make much bigger worlds.

It's time think bigger!

联系我们 contact @ memedata.com