物理
Unlike many other engines using third-party solutions, UNIGINE features its own built-in physics module.与许多其他使用第三方解决方案的引擎不同,UNIGINE具有其自己的内置物理模块。
The advantages of using a built-in physics module:使用内置的物理模块的优势:
- Memory efficiency — a single instance of the world is used (if an external solution is used, one more copy of the world has to be created and stored).内存效率:使用一个世界的实例(如果使用外部解决方案,则必须再创建和存储一个世界的副本)。
- Performance — calls to external functions, excessive data conversion and transmission between integrated systems are avoided.性能:避免了对外部函数的调用,过多的数据转换以及集成系统之间的传输。
It should be clear that it is not a high-precision simulation of real world physics! The simulation is just an approximation based on simplified calculations and decreased accuracy providing a realistic look within the strict limits of the real-time mode. Achieving a true-to-life result always involves a trade-off between accuracy and performance.应该清楚的是,这不是对现实世界物理的高精度模拟!模拟只是基于简化的计算和降低的精度的近似值,可以在实时模式的严格限制内提供逼真的外观。要获得真实的结果,总是需要在准确性和性能之间进行权衡。
Though in general UNIGINE uses a simplified Newtonian physics with an impulse-based approach, the integration of collision detection, gravity, friction, buoyancy, joints, and external physical forces provides realistic simulation of physical bodies and complex interactions between them.尽管UNIGINE通常使用基于脉冲的方法简化的牛顿物理学,但将碰撞检测,重力,摩擦,浮力进行了集成>,关节和外部物理力提供了物理体以及它们之间复杂相互作用的逼真的模拟。
See also也可以看看#
Use Cases and Limitations of Built-in Physics内置物理的用例和局限性#
UNIGINE's physics module is quite limited; however, in a number of cases it helps programmers and designers to make their work easier. Here is the list of the use cases where it is better to use built-in physics rather than hard-coding or bone-based animation:UNIGINE的物理模块非常有限;但是,在许多情况下,它可以帮助程序员和设计师简化工作。以下是一些使用案例列表,在这些使用案例中,最好使用内置物理而不是硬编码或基于骨骼的动画:
- Collision detection (prevention of moving through walls, interpenetration of solid bodies, etc.)碰撞检测(防止穿过壁移动,固体互穿等)
- Simulation of perfectly elastic collisions (redistribution of kinetic energy)完全弹性碰撞的模拟(动能的分布)
- Simulation of various joints, motors, and springs各种关节,电机和弹簧的仿真
- Simulation of basic physical phenomena: gravity, friction (static and sliding), buoyancy (for relatively calm water without big waves)基本物理现象的模拟:重力,摩擦(静态和滑动),浮力(用于相对平静且没有大浪的水)
- Simulation of external forces (wind, force field)外力模拟(风,力场)
- Procedural destruction of meshes程序网格破坏
- Simulation of deformable cloth and ropes可变形的衣服和绳索的仿真
- Rag doll simulationRagDoll模拟
It should be understood that the scope of simulation using built-in physics is limited to the cases listed above. Thus, there is a large number of cases that cannot be solved this way, including the following:应该理解,使用内置物理方法进行模拟的范围仅限于上述情况。因此,有很多情况无法通过这种方式解决,包括以下情况:
- High-precision physics simulation高精度物理模拟
- Flight dynamics simulation飞行动力学模拟
- Fluid dynamics simulationFluid dynamics simulation
- Simulation of gravitational fields引力场的模拟
- Simulation of inelastic collisions非弹性碰撞的模拟
- Digging of the ground挖地面
- Physical destruction of complex objects复杂对象的物理破坏
Though some tasks involving a large number of objects can be solved using built-in physics, it may significantly reduce performance. In this case, as in the cases listed above, it is recommended to use alternative ways described in the next chapter.尽管可以使用内置物理方法解决某些涉及大量对象的任务,但它可能会大大降低性能。在这种情况下,与上面列出的情况一样,使用替代方法下面描述。
Watch a fragment of our video tutorial on physics illustrating this chapter.观看我们的物理视频教程的片段,说明本章。
Alternatives to Built-in Physics内置物理的替代方法#
When a problem cannot be solved by means of the built-in physics module or takes too much time, several other approaches can be used: bone-based animation, hard coding, or using an external physics engine.当无法通过内置物理模块解决问题或花费过多时间时,可以使用其他几种方法:基于骨骼的动画,硬编码或使用外部物理引擎。
Bone-based Animation基于骨骼的动画#
In some cases, when an object's behavior cannot be implemented by using physics simulation or requires too many calculations, the bone-based animation can be used instead.在某些情况下,如果无法通过物理模拟来实现对象的行为或需要进行过多的计算,则可以使用基于骨骼的动画。
Bone-based animation is a technique in which animated object is represented by two components:基于骨骼的动画是一种将动画对象由两个组件表示的技术:
- Surface representation used to draw the object (called skin or mesh) 用于绘制对象的表面表示(称为皮肤或mesh)
- Hierarchical set of interconnected bones (called the skeleton or rig) used to animate (pose and keyframe) the mesh用于对网格进行动画处理(姿势和关键帧)的互连骨骼的层次结构集(称为骨架或装备)
This technique serves to make the animation process more convenient. Each bone of the rig is associated with some part of the object's mesh. Skinning is the process of creating this association. Changing bone transformations modifies the mesh.此技术用于使动画处理更加方便。装备的每个骨骼都与对象网格的某些部分相关联。外观是创建此关联的过程。更改骨骼变形会修改网格。
There are two ways of using bone animation:有两种使用骨骼动画的方法:
- Baked bone animation using pre-baked keyframes stored in .anim file使用保存在 .anim file
- Procedural bone animation using ObjectMeshSkinned class to change bone transformations in real time使用ObjectMeshSkinned类实时修改骨骼变换的程序骨骼动画
Meshes and animation can be created using third-party graphics software (3ds Max, Maya, etc.), then imported to UNIGINE in their native format (.mesh and .anim) and used in the world.网格和动画可以使用第三方图形软件(3ds Max, Maya等)创建,然后导入到UNIGINE的原生格式(.mesh 和 .mesh),并在世界上使用。
- The first use case is creation of a complex mechanism with a large number of various joints working in the background of a factory workshop. Simulation of this mechanism using physics will significantly reduce performance. You can use a baked bone-based animation instead.第一个用例是在工厂车间的背景下,创建一个复杂的机制,其中包含大量的各种关节。使用物理学对该机制进行仿真将大大降低性能。您可以改为使用基于骨骼的烘焙动画。
- The second use case is determined by the fact that bones cannot be affected physically. Imagine that we have an antenna, which is represented by a single mesh containing a number of bones, attached to a vehicle. To make it move realistically we have to write a script (in C++ or C#) calculating necessary bone transformations and providing desired behavior of the antenna.第二个用例由以下事实决定:骨骼不会受到物理影响。想象一下,我们有一个天线,它由连接到车辆的单个mesh表示,其中包含许多骨头。为了使其逼真地移动,我们必须编写一个脚本(在C++或C#中),以计算必要的骨骼转换并提供所需的天线行为。
Code-based Approach基于代码的方法#
Another way to replace physics simulation achieving a realistic result without performance drops is code-based approach, which involves writing some code to programmatically change transformations of objects and their behavior.替代物理模拟以达到不降低性能的真实结果的另一种方法是基于代码的方法,该方法涉及编写代码以编程方式更改对象及其行为的转换。
-
The first use case is simulation of the object deformation as a result of an inelastic collision. The built-in physics does not implement inelastic collision simulation. However, we can detect a collision and then change the deformed object's mesh in the script (in UnigineScript, C++ or C#) providing the desired deformation.第一个用例是模拟由于非弹性碰撞导致的物体变形。内置物理方法不执行非弹性碰撞模拟。但是,我们可以检测到碰撞,然后在脚本(在UnigineScript,C++或C#中)中更改变形对象的网格。
Deformation (i.e. changing positions of mesh vertices) is available for dynamic meshes only!变形(即更改网格顶点的位置)仅适用于动态网格! - The second use case is when there is a large number of interacting elements, e.g. tank tracks and suspension. If we have many tanks, the calculation of physics will drop FPS down. Moreover, due to approximation errors the whole mechanism may blow up or look unnatural. We write a script, which calculates the movement of suspension and track plates, providing stability and a desired degree of realism.第二个用例是当存在大量交互元素时,例如坦克履带和悬架。如果我们有很多坦克,那么物理计算将使FPS下降。此外,由于近似误差,整个机构可能会破裂或看起来不自然。我们编写了一个脚本,该脚本计算悬架和履带板的运动,从而提供稳定性和所需的逼真度。
Using code-based tricks makes it possible to maintain acceptable FPS and tweak the behavior of objects.使用基于代码的技巧可以维持可接受的FPS并调整对象的行为。
External Physics Engines外部物理引擎#
In cases where a precise physical simulation is required (e.g. flight dynamics, fluid dynamics, etc.) it is recommended to use a specialized third-party solution that provides required functionality. UNIGINE does not currently offer a solution for third-party physics integration. Therefore, all necessary product-specific guidelines on how to integrate a certain third-party solution into your application are to be obtained from the party offering this solution.在需要精确的物理模拟(例如飞行动力学,流体动力学等)的情况下,建议使用提供所需功能的专业第三方解决方案。 UNIGINE当前不提供用于第三方物理集成的解决方案。因此,有关如何将某个第三方解决方案集成到您的应用程序中的所有必要的产品特定准则,都应从提供该解决方案的一方那里获得。
Combined Approach组合法#
Very often, the best results can be achieved using a combination of different methods or techniques. So, the approaches described above can be successfully combined to provide a convincing look and acceptable performance.通常,结合使用不同的方法或技术可获得最佳结果。因此,上述方法可以成功组合以提供令人信服的外观和可接受的性能。
The use case illustrating a successful combination of several approaches is associated with simulation of physical destruction of complex objects, e.g. collapsing bridges, exploding buildings, etc. Realistic simulation in this case is impossible in real time. However, it can be pre-computed using third-party offline physics simulation software and then converted to a baked bone-based animation to be used in a real-time application.用例说明了几种方法的成功结合,与模拟复杂对象的物理破坏有关,例如倒塌的桥梁,爆炸的建筑物等。在这种情况下,无法实时进行逼真的模拟。但是,可以使用第三方脱机物理模拟软件对其进行预先计算,然后将其转换为基于烘焙的骨骼动画,以用于实时应用程序。
Physical Interaction of Objects. Basic Entities对象的物理交互。基本实体#
While the majority of node types in the scene serve as auxiliary means or decorations, the Object type can have physical properties and interact with other objects and the environment. For these properties to be assigned, the object should have a "body" (a physical approximation of the object), and the body should have at least one "shape " (a volume in space occupied by a body). To connect bodies and restrict their movement relative to each other, the "joints" are used. The picture below illustrates relationships between these entities.尽管场景中的大多数节点类型用作辅助手段或装饰,但Object类型可以具有物理属性并与其他对象和环境交互。为了分配这些属性,对象应具有“ body”(对象的物理近似值),并且本体应至少具有一个“ shape”(物体所占据的空间体积)。为了连接实体并限制它们之间的相对运动,使用了“ joint”(关节)。下图说明了这些实体之间的关系。
Body物体(Body)#
To interact with other objects as well as external physical forces, an object must have a body. The body can be considered a physical approximation of the object, it describes its behavior and represents a set of its dynamic parameters, such as mass, velocity, etc. There are several types of bodies: dummy, rigid, ragdoll, fracture, rope, cloth, water, path. Each type of body is used for simulation of a specific type of object.要与其他对象以及外部物理力相互作用,一个对象必须具有物体。可以将物体视为对象的物理近似,它描述物体的行为并代表其动态参数集,例如质量,速度等。物体有几种类型:dummy, rigid, ragdoll, fracture, rope, cloth, water, path。每种类型的物体都用于模拟特定类型的对象。
Mass parameters of the body can be set up manually or determined automatically using the shape-based parameters. It is convenient when a body has several shapes.物体的质量参数可以手动设置,也可以使用基于形状的参数自动确定。人体具有多种形状时很方便。
Body mass and density are used for buoyancy simulation in accordance with the Archimedes' principle. 根据阿基米德原理,将体重和密度用于浮力模拟。
Shape形状(Shape)#
While the body determines the object’s behavior, the shape represents the volume of space occupied by a physical body. The shape is invisible and doesn’t have to be the same as the object’s mesh. Actually a rough approximation (sphere, capsule, cylinder, box, convex hull) is often more efficient and indistinguishable. A physical body has one or several collision shapes allowing objects to collide with each other.物体决定对象的行为时,形状表示物体所占据的空间量。形状是不可见的,不必与对象的网格相同。实际上,粗略近似(sphere, capsule, cylinder, box, convex hull)通常更有效且难以区分。物理物体具有一个或多个碰撞形状,允许对象彼此碰撞。
Objects with shapes also fall down under gravity, bounce off static surfaces, or slide along them. Sliding and bouncing are determined by restitution and friction coefficients included in the list of shape parameters. A body without a single shape assigned behaves as a dummy body that can be connected to other bodies using joints, but does not collide and is immune to gravity.具有形状的对象也会在重力作用下掉落,从静态表面反弹或沿其滑动。滑动和弹跳由形状参数列表中包括的恢复力和摩擦系数确定。没有分配单一形状的物体的行为类似于虚拟物体,可以使用关节将其连接到其他物体,但不会发生碰撞并且不受重力的影响。
Joint联合(Joint)#
In virtual worlds, like in the real world, we would like to have complex objects consisting of several interconnected parts (for example, a robotic arm or a car). That's exactly what the joints are used for.在虚拟世界中,就像在现实世界中一样,我们希望具有由多个相互连接的部分组成的复杂对象(例如,机械臂或汽车)。这正是关节的用途。
A joint connects two rigid bodies and represents certain constraints i.e. it restricts the movement of connected bodies relative to each other. When the force applied to a joint is too much, the joint breaks. The implementation of destructible joints includes the limits of linear and angular motion, so if a force exceeds these limits, the joint is broken. There are several types of joints: fixed, hinge, ball, prismatic, cylindrical, suspension, wheel, path.关节连接两个刚体,并表示某些约束,即,它限制了相连体彼此之间的运动。当施加在关节上的力太大时,关节会断裂。可破坏的关节的实现包括线性运动和角运动的限制,因此,如果力超过这些限制,则关节会断裂。关节有几种类型:fixed, hinge, ball, prismatic, cylindrical, suspension, wheel, path。
When using joints, it is very important to ensure mass balance — avoid connection of too heavy bodies to light ones, otherwise the system may become unstable. As mentioned before, physics simulation uses approximate calculations, therefore, if the difference of mass between two connected bodies is significant, accumulation of errors and precision issues lead to instability of the result.使用关节时,确保质量平衡非常重要:避免将过重的物体连接到轻的物体,否则系统可能会变得不稳定。如前所述,物理模拟使用近似计算,因此,如果两个连接的物体之间的质量差异很大,则误差的累积和精度问题将导致结果不稳定。
Thus, when making a car model, do not set the mass of the car body equal to 2000 kg and the wheels — to 10 kg, it might be better to use 5 kg for the body and 1 kg for each wheel to provide realistic behavior.因此,在制作汽车模型时,不要将车身的质量设置为等于2000 kg,将车轮的质量设置为等于10 kg,最好使用5 kg的车身和每个车轮1 kg的重量。提供逼真的行为。
Collision and Intersection Detection碰撞和相交检测#
To restrict interpenetration of solid bodies, prevent moving through walls, and make things look more natural and familiar to the eye, the collision detection is used.为了限制固体的互穿,防止穿过壁移动并使事物看起来更自然和眼睛熟悉,使用了碰撞检测。
There are two types of collision detection implemented in UNIGINE:UNIGINE中实现了两种冲突检测:
- Discrete collision detection is performed in certain intervals of time and each frame is treated separately from others. In general, discretization improves performance. However, when a project framerate is already low, a small fast-moving object is likely to teleport from one point to another instead of moving there smoothly, and a collision will not be detected.离散冲突检测是在一定时间间隔内执行的,每个帧都与其他帧分开处理。通常,离散化可以提高性能。但是,当项目帧速已经很低时,一个小的快速移动物体很可能会从一个点移动到另一个点,而不是在那里平稳移动,并且不会检测到碰撞。
-
Continuous collision detection does not suffer this problem as the moving body is extruded along its trajectory (between two adjacent frames). If something gets into this volume and a collision is detected, the body is taken back in time to correct the collision reaction. 连续碰撞检测不会遇到此问题,因为移动体沿其轨迹(在两个相邻帧之间)被挤压。如果有物体进入该容积并检测到碰撞,则及时收回车身以纠正碰撞反应。
Collision detection is a very expensive operation, and since our scene may have hundreds of objects, a lot of effort is put into optimization.碰撞检测是一项非常昂贵的操作,并且由于我们的场景中可能有数百个对象,因此需要花费大量精力进行优化。
-
The first parameter contributing to the cost is collision shape complexity. As a rule, most 3D objects are represented by a complex and detailed visible mesh and an invisible simplified shape used by a physics engine for collision detection. Types of shapes available are listed above.导致成本增加的第一个参数是碰撞形状复杂度。通常,大多数3D对象由物理引擎用于碰撞检测的复杂且详细的可见网格和不可见的简化的shape表示。 上方列出了可用的形状类型。
To reduce computational load it is strongly recommended to use simple collision shapes instead of complex ones wherever possible.为减少计算量,强烈建议尽可能使用简单的碰撞形状而不是复杂的碰撞形状。 - The second parameter is a number of checks to be performed — the worst case would be all-against-all. To exclude unnecessary checks (e.g. two objects hardly collide if they are far from each other) the scene is to be split into sections.第二个参数是要执行的检查次数,最坏的情况是全部反对。为了排除不必要的检查(例如,两个对象彼此相距很远,几乎不会发生碰撞),将场景分成多个部分。
There is an important step which follows collision detection — collision response, or the result of collision (e.g. two balls bounce off each other). Friction, restitution, and other parameters are taken into account in calculation of the collision response.碰撞检测之后有一个重要步骤:碰撞响应或碰撞的结果(例如,两个球相互弹开)。在计算碰撞响应时会考虑摩擦,恢复和其他参数。
If some surfaces of an object participate in collisions, and others don't, collision detection can be enabled on a per-surface basis. For example, you can disable collision detection for a dashboard inside a cockpit, as it is covered by other surfaces.如果对象的某些曲面参与碰撞,而其他不参与,则可以基于每个曲面启用碰撞检测。例如,您可以为座舱内的仪表板禁用碰撞检测,因为该仪表板被其他表面覆盖。
Intersection detection lies at the heart of collision detection, but is a particular case of ray intersection (ray casting) that serves slightly different purposes. Calculation of the intersection between a ray cast from a certain point in a certain direction and a surface is fast and inexpensive and therefore sometimes can be used instead of computing a collision. For example, calculation of collisions of car wheels with the ground takes time and decreases framerate. In this case we can use intersection of rays cast from the bottom of the car with the ground instead of precise collision detection using collision shapes, thus reducing computational costs and increasing performance. 相交检测是碰撞检测的核心,但是射线相交(ray casting)的特殊情况,其用途略有不同。从某个方向的某个点投射的光线与表面之间的交点的计算是快速且廉价的,因此有时可以用来代替计算碰撞。例如,计算车轮与地面的碰撞会花费时间并降低帧速率。在这种情况下,我们可以使用从汽车底部投射的光线与地面的交点,而不是使用碰撞形状进行精确的碰撞检测,从而降低了计算成本并提高了性能。
To make collision and intersection detection flexible and selective, and to reduce calculation costs, the mechanism of bit masking is used. For example, we have an object that does not participate in interactions with others, but we want it to lie on the ground. Matching the collision bit masks of this object and the ground (at least one bit in the masks should match) provides the necessary effect. One object can participate in several collision and intersection checks as only one bit in the mask is required to match for a pair of objects. 为了使冲突和相交检测灵活而有选择性,并降低了计算成本,使用了位掩码的机制。例如,我们有一个不参与与他人互动的对象,但我们希望它位于地面上。使该对象的碰撞位掩码与地面匹配(掩码中的至少一位应匹配)可提供必要的效果。一个对象可以参与多次碰撞和相交检查,因为只需要掩码中的一位即可匹配一对对象。
Simulation of Physics物理模拟#
Simulation of physics is performed in a looped sequence of the following stages on a per-frame basis:物理模拟是按照以下阶段的循环顺序在每帧的基础上执行的:
Physics in UNIGINE is simulated in the multi-threaded mode, when some operations are performed in parallel.当并行执行某些操作时, UNIGINE中的物理是在多线程模式中模拟的。
Physics is simulated with its own fixed FPS, which does not depend on the rendering framerate: a variable FPS cannot be used here because it makes calculation results unstable.物理使用其自己的固定的 FPS进行仿真,该FPS不取决于渲染帧频:此处无法使用变量FPS,因为它会使计算结果不稳定。
Calculation of physics for all dynamic objects on the scene takes too much time. To ensure a consistent framerate and exclude unnecessary calculations, some optimization approaches are used:为场景中所有动态对象进行物理计算需要太多时间。为了确保一致的帧速率并排除不必要的计算,使用了一些优化方法:
-
When a body is moving slower than a specified minimum linear or angular velocity for a specified period (freeze frames), it is assumed that it has come to a halt. During this period of inactivity, there is actually no need to simulate it. If this happens, the object remains in a resting state until it is affected by another object or force. Therefore, such object is excluded from all steps of the simulation except collision detection. This state is called freezing and it allows to save a great deal of computational resources.当物体在指定时间段内以低于指定的最小线性或角速度的速度运动(冻结帧)时,假定它具有打住。在这段非活动期间,实际上没有必要对其进行仿真。如果发生这种情况,则该对象将保持静止状态,直到受到另一个对象或力量的影响。因此,除了碰撞检测之外,该对象已从仿真的所有步骤中排除。此状态称为冻结,它可以节省大量计算资源。
Frozen blue and unfrozen red boxes: the impulse applied to the pyramid of boxes unfroze all but one冻结的蓝色立方和未冻结的红色立方:应用于立方的金字塔的脉冲将冻结除一个立方之外的所有立方 - If interacting objects are far away from the viewer, they do not make a noticeable contribution to the whole image and thus can be neglected. The Simulation distance setting specifies the distance from the viewer when physical interactions are no longer calculated.如果交互对象距离查看者较远,则它们对整个图像的贡献不大,因此可以忽略。 模拟距离设置指定不再计算物理交互时与查看者的距离。
Global Physics Settings全局物理设置#
Global physics settings (FPS, gravity, simulation distance, freeze frames and velocities, penetration factor, penetration tolerance) can be accessed via the Physics settings panel in the Editor.可以通过Editor中的Physics settings面板访问全局物理设置(FPS,重力,模拟距离,冻结帧和速度,穿透因子,穿透公差)。