冲突检测
The body movement without constraints and obstacles rarely happens in real life, the same is true for a virtual world. To correctly describe the situation when the body meets an obstacle the collision detection is used.在现实生活中很少发生没有约束和障碍的身体运动,对于虚拟世界也是如此。为了正确描述身体遇到障碍物的情况,请使用碰撞检测。
Collision detection algorithms, regardless of their implementation, usually operate with invisible simplified shapes that approximate the meshes of colliding objects. Such approximations are called colliders (or collision shapes). There are several types of colliders, which can be combined: boxes, spheres, cylinders, capsules, convex hulls.碰撞检测算法,无论其实现方式如何,通常以不可见的简化形状运行,这些形状近似于碰撞对象的网格。这种近似称为碰撞器(或碰撞形状)。对撞机有几种类型,可以组合使用:盒子,球体,圆柱体,胶囊,凸包。
There are two types of collisions implemented in Unigine depending on the types of colliding objects: 根据碰撞对象的类型,Unigine中实现了两种碰撞类型:
- Shape - Shape collision: between two objects with physical properties assigned (i.e. having a body and at least one shape, both must be enabled). In this case contact points between the shapes are found. 形状-形状碰撞:在分配了物理属性(即具有 body 和至少一个 shape ,都必须启用)。在这种情况下,可以找到形状之间的接触点。
- Shape - Surface collision: between an object with physical properties assigned and a non-physical object (i.e. without physical representation). If the surface has the Collision flag set, it can also passively participate in physical interaction and prevent the physical object from going through. In this case, contact points between the shape and surface polygons are computed. 形状-表面碰撞:已分配了物理属性的对象与非物理对象(即没有物理表示形式)之间。如果 surface 具有Collision标志set,它也可以被动地参与物理交互并阻止物理对象通过。在这种情况下,将计算形状和表面多边形之间的接触点。
In order for an object to participate in collision detection (i.e. be a collider), it must be added to a specific BSP-tree representing the physics scene. Collision detection is automatically enabled for an object if it has a body assigned or at least one of its surfaces has the Collision flag set. The algorithm of enabling/disabling collision detection is illustrated below. 为了使对象参与碰撞检测(即为collider),必须将其添加到表示物理场景的特定BSP树中。如果为对象分配了 body 或对象的至少一个表面设置了Collision标志,则自动为对象启用碰撞检测。启用/禁用碰撞检测的算法如下所示。
- Disabling physics simulation globally does not turn off Collision detection.全局禁用物理模拟不会关闭碰撞检测。
- In case if an object has a body and a shape assigned and enabled, the collision detection algorithms will use only shape's parameters, while the parameters of its surfaces will be ignored.如果对象具有已分配并启用的身体和形状,则碰撞检测算法将仅使用形状的参数,而将忽略其表面的参数。
See Also也可以看看#
- getCollision() methods getCollision() methods
- The video tutorial on physics 物理视频教程
Process Pipeline工艺管道#
The whole process is divided into the following stages and phases:整个过程分为以下几个阶段:
- Collision Detection - during this phase we find all collisions with all contact points and collect all necessary information. 冲突检测 -在此阶段,我们发现与所有接触点的所有冲突并收集所有必要的信息。
Collision Response - it is the result of collision (e.g. two balls bounce off of each other). Without the response there would be no difference between collision and intersection of two objects. Friction, restitution and other parameters are taken into account in calculation of collision response. 碰撞响应 -这是碰撞的结果(例如,两个球相互弹起)。没有响应,两个对象的碰撞和相交就不会有任何区别。计算碰撞响应时会考虑摩擦力,恢复力和其他参数。
Callbacks Execution - custom user-defined actions to be performed on certain physics-related events. 回调执行 -对某些与物理相关的事件执行的自定义用户定义操作。
Detecting Collisions检测碰撞#
Checking all pairs of objects for collision is too time consuming, especially if the scene is large. Before doing more precise and costly calculations, we can filter out pairs of objects that are positioned too far to collide. Thus, all objects that have physical bodies are found within the Physical distance using the scene tree.检查所有成对的对象是否碰撞非常耗时,尤其是在场景较大的情况下。在进行更精确,更昂贵的计算之前,我们可以过滤掉放置太远而无法碰撞的对象对。因此,可以使用场景树在物理距离内找到所有具有物理物体的对象。
Actually, there are no broad and narrow phases per se, so on the next step all collisions (shape-shape and shape-surface) along with contact points are found for all colliding bodies, i.e., if they are 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.实际上,本身没有宽窄相,所以在下一步,所有碰撞( shape-shape 和 shape-surface )以及所有碰撞体的接触点,即它们是否相交或它们之间的距离小于穿透公差的值。接触点由它们的坐标,法线,形状穿透的深度,相对速度(两个物体之间),相对摩擦和恢复来表示。因此,这里我们收集了以后解决冲突所需的所有数据。
In a constrained physics simulation some objects influence the motion of others, while others don’t. Thus, these objects can be grouped into islands, which are self-contained groups of bodies that can influence the motion of each other in the group through constraint forces/impulses, but do not affect objects belonging to other islands. So, all contacting bodies as well as the ones having joints that connect them are combined in a island. 在受限的物理模拟中,某些对象会影响其他对象的运动,而其他对象则不会。因此,可以将这些对象分为岛,它们是独立的一组身体,可以通过约束力/冲力影响彼此在该组中的运动,但不影响属于其他对象的对象岛屿。因此,所有接触体以及具有连接它们的关节的接触体都合并在一个岛中。
In case Stable mode of physics simulation is enabled bodies, shapes, and joints are sorted inside islands to ensur that contacts are solved in the predefined order and visualization of physics in the world is repetitive (on one computer).如果启用了物理模拟的 Stable 模式,则在岛内对物体,形状和关节进行分类以确保以预定的顺序解决了接触问题,并且世界上的物理可视化是重复的(在一台计算机)。
Collision detection uses certain optimization approaches to decrease computational load and improve efficiency (e.g., space partitioning, islands, freezing etc.).碰撞检测使用某些优化方法来减少计算负荷并提高效率(例如,空间划分,岛屿,冻结等)。
Collision Response碰撞反应#
So, we've got all necessary information about collisions, now something has to be done with this information to provide realistic reaction. Collision response stands for simulation of the changes in the motion of two solid bodies after collision. On collision, the kinetic properties of two bodies change instantaneously. Typically the bodies rebound away from each other, slide, or settle into a static contact, depending on their elasticity and the configuration of the collision. Unigine uses an impulse-based reaction model. During the collision, the first body applies a collision impulse to the second one at a contact point equal in magnitude but opposite in direction to the impulse applied by the second body, as per the Newtonian principle of action and reaction.因此,我们已经获得了有关碰撞的所有必要信息,现在必须对这些信息进行某些操作以提供切合实际的反应。碰撞响应代表模拟两个固体在碰撞后运动的变化。发生碰撞时,两个物体的动力学特性会瞬间变化。典型地,取决于它们的弹性和碰撞的构型,它们彼此反弹,滑动或沉降成静态接触。 Unigine使用基于基于脉冲的反应模型。在碰撞过程中,根据牛顿的作用和反作用原理,第一个物体在与第二个物体施加的冲击大小相等但方向相反的接触点上向第二个物体施加碰撞冲击。
#At this stage all found contacts are cached together with contacts from the previous frame — to ensure proper interaction. On the basis of gathered contact points data, Unigine computes the impulse a shape gets by collision. Contact points are solved in a pseudo-random order to achieve simulation stability and reproducibility.在此阶段,所有找到的联系人都与前一帧的联系人一起缓存-以确保正确的交互。根据收集的接触点数据,Unigine计算形状通过碰撞得到的脉冲。接触点以伪随机顺序求解,以实现仿真稳定性和可重复性。
Joints are solved in the process of contact response calculation. The impulses that joints give the bodies attached to them are computed: how according to the current state of the joint, the bodies should respond to keep the joint unbroken (i.e. based on their masses, linear and angular velocities, change their movement direction and orientation), and how that response affects the joint (a joint can be broken by a too large impulse). Joints are also solved in the pseudo-random order.关节在接触响应计算过程中得到解决。计算关节给予与之相连的物体的冲动:根据关节的当前状态,物体应如何响应以使关节保持不破裂(即,基于其质量,线速度和角速度,更改其运动方向和方向),以及该响应如何影响关节(过大的脉冲可能会破坏关节)。关节也以伪随机顺序求解。
The results of contact and joint solving are accumulated and applied to bodies. The coordinates of the bodies change according to their new linear and angular velocities.累积了接触和联合求解的结果并将其应用于实体。物体的坐标根据其新的线性和角速度而变化。
There are two material parameters taken into account in the process of collison response calculation, which can be set for a shape and for a surface as well: Collison响应计算过程中考虑了两个材料参数,它们可以为 shape 和 surface 设置:
- Restitution - the degree of relative kinetic energy retained after a collision. It depends on the elasticity of the materials of colliding bodies.
- The minimum value of 0 indicates inelastic collisions (a piece of soft clay hitting the floor)The minimum value of 0 indicates inelastic collisions (a piece of soft clay hitting the floor)
- The maximum value of 1 represents highly elastic collisions (a rubber ball bouncing off a wall)The maximum value of 1 represents highly elastic collisions (a rubber ball bouncing off a wall)
- The minimum value of 0 indicates inelastic collisions (a piece of soft clay hitting the floor)最小值 0 表示无弹性碰撞(一块软粘土撞击地板)
- The maximum value of 1 represents highly elastic collisions (a rubber ball bouncing off a wall) 1 的最大值表示高度弹性的碰撞(橡胶球从墙壁上弹起)
- Friction - the force that impedes the relative motion of two surfaces in contact. The higher the value, the less tendency the body has to slide. Friction-阻止两个接触表面相对运动的力。值越高,身体滑动的趋势越少。
Callbacks Execution回调执行#
After the simulation stage all physics callbacks are called. These callbacks are mainly used for creation, destruction or modification of other objects, such operations can only be performed in the main thread.在仿真阶段之后,将调用所有物理回调。这些回调主要用于创建,销毁或修改其他对象,此类操作只能在主线程中执行。
Discrete and Continuous Collision Detection离散和连续碰撞检测#
Regarding the way the time scale is considered, two basic approaches to finding contact points in case of collision are used. Both of these approaches are 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 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). In cases when something gets into this volume and a collision is detected, the body is taken back in time to correct the collision reaction.
Continuous collision detection is available for sphere and capsule shapes only. 连续碰撞检测 不会遇到此问题,因为移动体沿其轨迹(在两个相邻帧之间)被挤压。如果有物体进入该体积并检测到碰撞,则及时收回车身以纠正碰撞反应。
The primary advantage of discrete detection is that it tends to be much faster and simpler for objects with complex shape or large size. Continuous collision detection requires mathematical descriptions of all objects and their motions and the solving systems of equations, which can be difficult, slow, or even impossible in many circumstances.离散检测的主要优点是,对于形状复杂或尺寸较大的对象,它往往会更快,更简单。连续碰撞检测需要对所有对象及其运动以及方程的求解系统进行数学描述,这在许多情况下可能是困难,缓慢甚至是不可能的。
Collision Mask防撞面罩#
To make collision detection flexible and selective, and to reduce calculation costs, the mechanism of bit masking is used. For example, we have an object, which 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 checks as only one bit in the mask is required to match for a pair of objects.为了使冲突检测灵活而有选择性,并降低计算成本,使用了位掩码机制。例如,我们有一个对象,它不参与与其他对象的交互,但是我们希望它位于地面上。使该对象的碰撞位掩码与地面匹配(掩码中的至少一位应匹配)可提供必要的效果。一个对象可以参加多个碰撞检查,因为只需要掩码中的一位即可匹配一对对象。
All scene objects, if not configured, are created with the default collision mask i.e. everything collides with everything. This may reduce performance in case if the number of objects in the world is large. The best practice here would be to establish what should collide with what.所有场景对象(如果未配置)都是使用默认的碰撞蒙版创建的,即所有物体都与所有物体碰撞。如果世界上的对象数量很多,这可能会降低性能。此处的最佳做法是确定应该与哪些碰撞。
Static and Dynamic Contacts静态和动态联系人#
Basically there are two types of contacts:基本上有两种类型的联系人:
- Static, when the body rests on another body. In this case, bodies are considered "frozen" until an external force affects them. This makes it possible to avoid unnecessary calculations. 静态,当尸体放在另一个尸体上时。在这种情况下,除非有外力影响物体,否则将其视为“ frozen ”。这样可以避免不必要的计算。
- Dynamic, when two bodies, at least one of which is moving, collide. In this case, we have to calculate instantaneous change of velocities for both colliding objects. 动态,当至少有一个正在移动的两个物体碰撞时。在这种情况下,我们必须计算两个碰撞对象的瞬时速度变化。
Collision Examples碰撞实例#
See the following example illustrating several aspects of collision detection:请参阅以下示例,说明冲突检测的几个方面: