This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
专业(SIM)
UnigineEditor
界面概述
资源工作流程
版本控制
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
VR Development
双精度坐标
应用程序接口
Animations-Related Classes
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
VR-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

物理模拟

At physics simulation, physics calculations are in the multi-threaded mode — some operations are performed in parallel.在物理模拟中,物理计算在多线程模式——一些操作是并行执行的。

Physics performance profiler enables real-time tracking of simulation performance.物理性能分析器可以实时跟踪仿真性能。

注意
To show the physics performance profiler that reports statistics on different physics simulation aspects, press 1 hotkey three times or type show_profiler  3 in the console.展示物理性能剖析器报告不同物理模拟方面的统计信息,按 1 热键三次或在控制台中键入 show_profiler  3

Rate of Physics Simulation物理模拟率#

UNIGINE physics module performs all its calculations. There are few things to point out about physics. UNIGINE 物理模块执行所有计算。关于物理学,有几件事需要指出。

  • It is simulated with its own fixed FPS, which does not depend on the rendering framerate. (The Engine FPS can also be synchronized with the physics one).它是用它模拟的拥有固定的FPS , 哪一个不依赖关于渲染帧率。 (引擎 FPS 也可以是同步的与物理之一)。

    注意
    If physics takes more than the assigned budget, further calculations are delayed. These calculations shall be performed during the subsequent rendering frames, making physics simulation look like in a "slow-motion" mode. The default budget is 50ms, but it can be increased, as necessary.如果物理学比分配的预算,进一步的计算被延迟。这些计算应在随后的渲染帧期间执行,使物理模拟看起来像在“慢动作”模式下。默认预算为 50ms,但可以根据需要增加。
  • During each tick, a number of calculation iterations can be performed. This includes the full cycle of physics simulation:在每个滴答声中,计算数迭代可以执行。这包括物理模拟的完整周期:

    1. updatePhysics() from the world logic is called.updatePhysics() 从世界逻辑被调用。
    2. Collision detection is performed.碰撞检测被执行。
    3. Joints are solved.关节解决了。
    4. Collision response is performed.碰撞响应被执行。
    注意
    There is no point in setting the number of iterations too high. UNIGINE checks whether the next iteration can be performed within physics budget limit, and if not, it is delayed and moved to the next rendering frame.设置没有意义迭代次数太高。 UNIGINE 检查下一次迭代是否可以在物理预算限制,如果不是,则延迟并移动到下一个渲染帧。

Physics Update Modes物理更新模式#

There are two update modes available for physics simulation (each of them has its advantages and use cases):物理模拟有两种可用的更新模式(每种都有其优点和用例):

  • Before Rendering — physics update (along with the spatial tree update and user callbacks) is executed in the Main thread just before rendering is performed (render). The number of physics ticks executed before the rendering frame here is defined by the physics and the Engine framerates. This mode is the most clear and straightforward (everything is executed safely in a strictly determined order) with no frame lag (results of physics calculations are applied in the current frame). But, on the other hand, this mode is the slowest as there are no asynchronous parallel calculations (everything's in the Main thread). Use this mode in case the time lag is unacceptabe for your application (you need all physics calculations to be applied in the current frame) and you want maximum simplicity and strictly determined order of execution for user code (physicsUpdate and physics callbacks). Before Rendering — 物理更新(连同空间树更新和用户回调)在渲染执行之前在主线程中执行(render)。此处渲染帧之前执行的物理滴答数由物理和引擎帧率. 此模式最清晰直接(一切都以严格确定的顺序安全执行),没有帧滞后(物理计算的结果应用于当前帧)。但是,另一方面,这种模式是最慢的,因为没有异步并行计算(一切都在主线程中)。 如果您的应用程序无法接受时间延迟(您需要在当前帧中应用所有物理计算)并且您希望最大程度地简化并严格确定用户的执行顺序,请使用此模式代码(physicsUpdate 和物理回调)。
  • Async Rendering — physics update is performed asynchronously to rendering. In case of several physics ticks per one rendering frame (when the Engine framerate is lower, or catching up is performed), only the first one is executed in parallel, then the physics module waits for the completion of the rendering process, returns to the Main thread and executes the rest of the physics ticks. There is a frame lag (results of physics calculations are applied in the next frame) and there is some ambiguity regarding the time, when user code (physicsUpdate and physics callbacks) is to be executed in case of several physics ticks per one rendering frame (some part is executed before rendering while the other just after it). This mode is the fastest one and is used by default.Async Rendering — 物理更新与渲染异步执行。如果每个渲染帧有多个物理滴答(当引擎帧速率较低时,或赶上执行),仅并行执行第一个,然后物理模块等待渲染过程完成,返回主线程并执行其余的物理滴答。存在帧延迟(物理计算的结果在下一帧中应用),并且在每个渲染帧有多个物理滴答的情况下执行用户代码(physicsUpdate 和物理回调)的时间存在一些歧义(某些部分在渲染之前执行,而另一部分则在渲染之后执行)。此模式是最快的,默认使用。

The modes are toggled in Global Physics Settings in UnigineEditor.模式在全局物理设置在 UnigineEditor 中。

Catching Up迎头赶上#

As the Engine has a variable framerate while the physics framerate is fixed, execution of physics calculations always adapts to the current situation.由于引擎具有可变帧率,而物理帧率是固定的,因此物理计算的执行始终适应当前情况。

In case the Engine framerate drops below the physics framerate (which is a very rare case), some of the physics frames have no enough time for execution and become missed. The physics module keeps such missed frames in memory for a certain period (called Missed Frame Lifetime) and tries to execute them when the situation gets better (Engine FPS grows) or when the CPU is idle while waiting for GPU to complete rendering (if there is enough time). This is called catching up and helps avoiding "slow-motion" effect that occurs when physics frames are dropped off. In case of insufficient hardware capabilities missed frames are removed from the buffer as their lifetime expires and become lost forever.如果引擎帧率下降到物理帧率以下(这是一种非常罕见的情况),一些物理帧没有足够的时间来执行并被错过。物理模块将这些 丢失帧在内存中保留一段时间(称为 Missed Frame Lifetime),并在情况好转(引擎 FPS 增长)或 CPU 空闲时尝试执行它们等待 GPU完成渲染(如果有足够的时间)。这称为追赶,有助于避免物理帧丢失时出现的“慢动作”效果。在硬件功能不足的情况下,丢失的帧将在其生命周期到期时从缓冲区中删除并永远丢失。

Actual time of physics calcuations can go beyond the current budget as the ones performed while waiting for GPU are not taken into account. So the budget is not absolutely strict in this respect and can be exceeded if the CPU is idle.物理计算的实际时间可以超越当前预算因为没有考虑在等待 GPU 时执行的那些。所以这方面的预算并不是绝对严格的,如果CPU空闲的话可以超出预算。

Deterministic Mode确定性模式#

Deterministic mode ensures that all contacts are solved in the predefined order and visualization of physics in the world is repetitive (on one computer). When this mode is enabled the Engine performs additional sorting of bodies, shapes and joints inside islands after building them.确定性模式确保所有接触都以预定义的顺序解决,并且世界上的物理可视化是重复的(在一台计算机上)。启用此模式后,引擎会在构建岛屿后对岛屿内的物体、形状和关节进行额外的排序。

Deterministic mode is unavailable in case there are missed frames — it is simply impossible. Moreover, there may be differences between visualization of physics on different hardware (e.g., AMD and Intel).确定性模式在丢失帧的情况下不可用——这根本不可能。此外,不同硬件(例如 AMD 和 Intel)上的物理可视化可能存在差异。

Determinism is guaranteed if there are no missed frames, the same Engine version is used, and the CPUs perform SSE operations similarly.如果没有丢失帧,使用相同的引擎版本,并且 CPU 执行类似的 SSE 操作,则可以保证确定性。

Please note that deterministic mode does not come for free, it may eat up to 10-20% of the frame rate, and it also depends on the scene a lot.请注意,确定性模式可能会影响性能,它可能会吃掉高达 10-20% 的帧速率,而且它也很大程度上取决于场景。

The mode can be enabled in Global Physics Settings in UnigineEditor.该模式可以在全局物理设置在 UnigineEditor 中。

Stages of Physics Simulation物理模拟阶段#

Simulation of physics goes through a number of stages when it is updated each iteration. They are as follows.物理模拟在每次迭代更新时都会经历多个阶段。它们如下。

  1. Physics update物理更新
  2. Сollision detection碰撞检测
  3. Simulation模拟
  4. Synchronization of physics物理同步

In the performance profiler, the total time of physics simulation is displayed by the Physics counter.在表演中剖析器,物理模拟的总时间由Physics计数器显示。

1. Physics Update1.物理更新#

  1. Before anything else, a spatial tree is updated, this is performed in the Main thread regardless of the current update mode. After we have the up-to-date data regarding how all objects with physical bodies are positioned, it would be safe to transform them or calculate collisions.在其他任何事情之前,更新空间树,这是在主线程中执行的,而不管当前更新模式.在我们获得有关所有具有物体的对象如何定位的最新数据之后,可以安全地转换它们或计算碰撞。
  2. C++ API Plugin updatePhysics() is called, if this function is implemented for the plugin.C++ API 插件如果为插件实现了此功能,则调用 updatePhysics()
  3. Physics module calls the updatePhysics() of the world logic. Here you can call all functions that handle physics simulation and interactions (and not only that, see the details on updatePhysics() usage).物理模块调用 world logicupdatePhysics()。在这里,您可以调用所有处理物理模拟和交互的函数(不仅如此,请参阅 updatePhysics() 用法的详细信息)。

2. Collision Detection2. 碰撞检测#

After the script-based changes have been made (bodies with their shapes and joints were transformed according to game logic, physics-based callbacks were set, etc.), physics can be simulated.在进行了基于脚本的更改之后(带有它们的物体形状关节根据游戏逻辑进行了转换,设置了基于物理的回调等),可以模拟物理。

  1. All objects that have physical bodies are found within the Physical distance. They will be simulated during the current physics tick. Make sure that the physical distance in your UNIGINE-based application is not too small, because physical interactions beyond this limit are not simulated, so objects become frozen. However, even if at least one body belonging to an island is found within the physical distance, the whole island shall be simulated.所有具有物理性质的物体物体内发现物理距离.它们将在当前物理滴答期间被模拟。确保基于 UNIGINE 的应用程序中的物理距离不会太小,因为超出此限制的物理交互不会被模拟,因此对象会冻结。然而,即使至少有一个物体属于在物理距离内发现,则应模拟整个岛屿。

    注意
    You can force to update nodes that are outside the Physical distance using addUpdateNode() function.您可以强制更新在物理距离使用 addUpdateNode() 函数。
  2. All collisions (shape-shape and shape-surface) along with contact points are found for all colliding bodies among the ones found at the previous step (i.e., intersecting or have the distance between them less than the value of penetration tolerance). Contact points are represented by their coordinates, normals, depth of shapes penetration, relative velocity (between two bodies), relative friction and restitution. So, here we collect all the data that is required to resolve collisions later.所有碰撞(形状形状形面) 以及在上一步中找到的所有碰撞体中的接触点(即相交或它们之间的距离小于穿透公差)。 接触点由它们的坐标、法线、形状穿透深度、相对速度(两个物体之间)、相对摩擦和恢复力来表示。因此,我们在这里收集以后解决冲突所需的所有数据。

    注意
    If a body is frozen and no contacts are found that would push it out of its frozen state with strong enough impulse, such body is not simulated in the current tick.如果一个物体是冷冻并且没有发现可以用足够强的脉冲将其从冻结状态中推出的接触,因此在当前的滴答声中没有模拟这种物体。
  3. Islands are built using the contacts obtained at the previos step.岛屿是使用在上一步获得的联系人构建的。
  4. Bodies, shapes and joints are sorted inside islands. By that, we ensure that contacts are solved in the predefined order and visualization of physics in the world is repetitive (on one computer).物体、形状和关节在岛屿内进行分类。这样,我们确保以预定义的顺序解决接触问题,并且世界物理的可视化是重复的(在一台计算机上)。

In the performance profiler you can find:在表演中剖析器你可以找到:

  • The total time of collision detection is displayed by the PCollision counter.碰撞检测的总时间由 PCollision 计数器显示。
  • The number of contacts is displayed by the PContacts counter.联系人数量由 PContacts 计数器显示。
  • The number of islands is shown by the PIslands counter.岛屿的数量由 PIslands 计数器显示。
  • The number of bodies is shown by the PBodies counter.物体的数量由 PBodies 计数器显示。
  • The total number of joints is shown by the PJoints counter.关节总数由 PJoints 计数器显示。

Continuous Collision Detection连续碰撞检测#

If a sphere or a capsule participates in the contact with any other shape or surface, continuous collision detection (CCD) is performed. UNIGINE takes velocities of the body, radius of its shape and calculates what contacts this body will have (during the current physics tick), assuming it continues its current trajectory. So, unlike the simple collision detection, contacts are analyzed not discretely, once per physics tick, but rather found for the whole frame.如果球体胶囊参与与任何其他形状或表面的接触,则执行连续碰撞检测 (CCD)。 UNIGINE 获取物体的速度、其形状的半径并计算该物体将有哪些接触(在当前物理滴答期间),假设它继续其当前轨迹。因此,与简单的碰撞检测不同,接触不是离散分析的,每个物理刻度一次,而是针对整个帧进行分析。

3. Simulation3. 模拟#

When a collision has been detected, collision response is calculated, so that the colliding bodies would gain new velocities.当检测到碰撞时,碰撞反应被计算出来,这样碰撞的物体就会获得新的速度。

  1. Here bodies are prepared to participate in collisions: contacts found for them are cached together with contacts from the previous frame — to ensure that they interact with each other properly. Here 物体准备参与碰撞:为它们找到的联系人与前一帧的联系人一起缓存 - 以确保它们正确地相互交互。
  2. Collision response for each body is calculated. Based on the gathered contact points data, the Engine computes the impulse each body gets after collision. Contact points are solved in a pseudo-random order to achieve simulation stability and reproducibility.计算每个物体的 Collision 响应。根据收集的接触点数据,引擎计算每个物体在碰撞后获得的冲量。接触点以伪随机顺序求解,以实现模拟稳定性和再现性。
  3. When contact responses are calculated, joints constraining relative motion of bodies are solved. Joints are solved in the pseudo-random order just like contact points.计算联系人响应时,关节解决了约束物体的相对运动。关节像接触点一样以伪随机顺序求解。

    注意
    Within one physics iteration, joints can be solved several times. The high number of joint iterations increase the precision of calculations, as well as computational load.在一次物理迭代中,可以多次求解关节。大量的联合迭代增加了计算的精度,以及计算负荷。

    In the performance profiler, the total time of both collision response and joint solving stages is displayed by the PResponse counter.在表演中剖析器,碰撞响应和联合求解阶段的总时间由 PResponse 计数器显示。

  4. The results of contact and joint solving are accumulated and, finally, are applied to bodies. The coordinates of the bodies change according to their new linear and angular velocities.接触和关节求解的结果被累积起来,最后应用到物体上。物体的坐标根据它们新的线速度和角速度而变化。

    In the performance profiler, the time of this stage is displayed by the PIntegrate counter.在表演中剖析器,这个阶段的时间由PIntegrate计数器显示。

In the performance profiler, the total time of simulation stage is displayed by the PSimulation counter.在表演中剖析器,仿真阶段的总时间由PSimulation计数器显示。

4. Synchronization of Physics4.物理同步#

Synchronization is the final stage of physics simulation. During the swap() in the UNIGINE main loop, physics module calls its internal updatePhysics() function. Bodies set their calculated transformations to objects. In the next frame, objects will be rendered in their new physics-based positions.同步是物理模拟的最后阶段。在 UNIGINE 主循环中的 swap() 期间,物理模块调用其内部的 updatePhysics() 函数。物体将其计算的变换设置为对象。在下一帧中,对象将在其新的基于物理的位置进行渲染。

注意
In the Async Rendering update mode physics simulation goes in parallel to the actual rendering process. This is the reason calculated physics is visible only in the next frame.Async Rendering 更新模式下物理模拟并行进行到实际的渲染过程。这就是计算物理仅在下一帧可见的原因。

If visualizer options are enabled, shapes, joints or contacts of non-frozen bodies will be rendered.如果启用了可视化选项,将渲染非冻结体的形状、关节或接触。

Multi-Threaded Physics Simulation多线程物理模拟#

Multi-threaded simulation of physics is run when the Async Rendering update mode is selected and there are two or more physics threads.Multi-threaded simulation of physics is run when the Async Rendering update mode is selected and there are two or more physics threads.

  1. updatePhysics() from the world script is always performed in the main physics thread.世界脚本中的 updatePhysics() 始终在主物理线程中执行。
  2. After that, the Engine takes advantage of multiple CPUs during the collision detection and simulation stage.之后,引擎在运行期间利用多个 CPU碰撞检测模拟阶段.

    • Contacts for all physical bodies within the physical distance are found along with all necessary information.内所有物理物体的接触物理距离连同所有必要的信息一起找到。
    • Islands are created based on the data on object collisions.岛屿是根据对象碰撞数据创建的。
    • As islands have been created, they can be safely handled in separate threads, because they are isolated.随着孤岛的创建,它们可以在单独的线程中安全地处理,因为它们是隔离的。
    • Then threaded islands are synchronized in the main physics thread to exchange data about the current contacts and ones from the previous frames. It will ensure proper physical behavior of bodies.然后线程岛在主物理线程中同步,以交换有关当前联系人和前一帧联系人的数据。它将确保物体的适当物体行为。
    • From there on, collision response and joints solving are again calculated per island in separate threads.从那里开始,碰撞响应和关节求解再次在单独的线程中按岛计算。
  3. Before physics is synchronized with the world, the engine waits for all threads to finish their calculations. When thread synchronization happens (during the swap stage of the UNIGINE main loop), all assigned physics callbacks are executed in the main thread (the order is as follows: bodies, joints, and triggers), and then physics is applied to nodes.在物理与世界同步之前,引擎会等待所有线程完成它们的计算。当线程同步发生时(在 UNIGINE 主循环的 swap 阶段),所有分配的物理回调都在主线程中执行(顺序如下:物体、关节和触发器),然后将物理应用于节点。

    注意
    Physics callbacks (mainly used for creation, destruction or modification of other objects) are called in the main thread, as this is the only place where such operations can be performed safely.物理回调(主要用于创建、销毁或修改其他对象)在主线程中调用,因为这是唯一可以安全执行此类操作的地方。
最新更新: 2023-12-19
Build: ()