连续物理
2.2. Fundamental Concepts
基本概念
The following sections present an in-depth description of the concepts behind continuous physics. It is not necessary to understand this part in order to use the engine at a basic level.
下面的章节会深度描述连续物理背后的知识,简单使用的话没有必要理解这一部分。
Havok Physics has implemented new simulation loops and collision detection algorithms. This section progresses from the basics of motion data representation to the new functionality of the collision detector, and explains our implementation of the continuous physics simulation loop.
Havok物理实习了新的物理模拟循环和碰撞检测算法。这节解释基本的运动数据原理到碰撞检测功能,来解释我们连续物理模拟循环的实现。
2.2.1. Transform and Swept Transform
形变和扫描形变
hkTransform is a representation of a body's state i.e. its position and orientation at a given moment in time. In discrete simlulation that position and orientation are constant for the duration of a frame and are updated at its end.
hkTransform是物体状态的一个表示。比如:在指定时间 它的位置和朝向。它描述了一帧时间内,这个物体的朝向和位置常量!
In continuous physics we need to know the body's precise position and orientation at any moment in-between discrete 'frame ticks'. So we represent the rigid body's state by an hkSweptTransform structure which basically stores the position and orientation at two chosen moments in time.
在连续物理中我们需要知道物体在任何世界的位置和朝向,所以我们使用hkSweptTransform 这个结构体来存贮两个选定时刻的位置和朝向。
Figure 6.16. Illustration of a hkSweptTransform, which stores position and orientation information at two moments in time. Information about mass centre allows for proper interpolation of position in time. In contrast, discrete physics' representation uses position and orientation information from one time moment. Motion State values from consecutive frames are therefore not explicitly linked in any way.
一个hkSweptTransform的插图,存储了两个时刻的位置和朝向信息.质心的信息可以让你通过差值得到任意时刻的位置,相反,离散物理只使用某一时刻的位置和朝向信息,这样,某一时刻的连续帧的运动信息不会很准确。
hkSweptTransform also stores location of the body's mass center allowing for proper interpolation of position in time. In contrast, discrete physics' representation uses position and orientation information from one time moment. A body's state information from consecutive frames is therefore not explicitly linked in any way.
hkSweptTransform 同样存贮了物体的质心位置信息用来插值获得任一时刻的位置。
A hkSweptTransform stores:包含了:
1.Time t0 and inverseDeltaTime, such that t1 == t0 + 1/inverseDeltaTime.
1.间隔时间的倒数
2.The body's mass centre position and the body's orientation at t0 and t1.
2.T0和t1 物体的质心和朝向
3.The center of mass in the body's local reference frame.
3.本地坐标系的质心。
The position of the mass center is required to properly interpolate the body's transform between the two time endpoints.
To get body's transform at arbitrary time tx (where t0 <= tx <= t1) we interpolate the position of body's mass center and interpolate its orientation quaternions. Here's a simplified code snippet:
// Performs linear interpolation of positions
post = pos0 * (1 - t) + pos1 * t
// Performs linear interpolation orientations
qt = lerp(q0, q1, t)
// Calculates position of the body's reference point
// using the mass center stored in local space mcPos
post = post - qt * mcPos
2.2.2. State Integration状态
To integrate a body's state we use simple Euler integration. At each 'frame tick' (also referred to as a PSI) assume that the body has reached its final position at t1. This becomes the initial position of the updated hkSweptTransform. After applying all gravity, constraint forces, reaction forces and impulses we get the body's final velocity that we can use to calculate its end position at t1. This new end position is perceived only as predicted or potential because it may be modified later when in-between-frames collisions are processed (we refer to such collisions as TOI or time-of-impact collisions).欧拉积分
2.2.3. Collision Detector 碰撞检测
The collision detector performs two tasks -- it verifies the motion of all simulated bodies throughout the frame step and it generates contact points between those bodies for the specified end time of the frame step.
碰撞检测有两个任务:1.确认所有模拟物体的运动状态,2.产生这些物体在帧结束时间点的contact points 联系点。
2.2.3.1. Verifying Motion and Generating Time-of-Impact Events.
确认运动信息和产生 TOI事件
The collision detector is given two hkSweptTransforms, which overlap in time. It interpolates the transform of each of the associated bodies along time and checks for collisions.
碰撞检测有两个hkSweptTransforms(两个物体的),他们在时间上重叠,它插值两个物体的位置并检测碰撞。
Figure 6.17. As the positions of the two bodies are interpolated they start moving towards each other. The moment when they touch each other is the time-of-impact and it is reported in a TOI event. If we interpolated their positions further the objects would interpenetrate significantly and eventually pass through completely.
他们向对方移动,当他们碰到对方的时候,产生TOI事件,并存储碰撞时间在事件里,通过插值,我们发现他们碰撞,重叠,并穿过了对方。
When interpenetration of bodies is detected the collision detector generates a collision event. Such events are called time-of-impact (TOI) events. They contain the time of reported collision/impact and references to the two colliding bodies. Collision detection is performed for all pairs of object and ultimately yields a list of collision events.
当渗透被检测的时候,碰撞检测器会产生一个碰撞事件,被称为TOI(TIME of Impact)的事件。他们包含碰撞时间和碰撞双方物体,所有的物体对进行碰撞检测,并产生一系列的碰撞事件。
2.2.3.2. Calculating Contact Points 计算连续点
Contact points generated by the collision detector are always calculated for one given moment in time. This operation is done independently of TOI events. By default, the collision detector calculates contact characteristics at the end of the current frame.
碰撞检测产生的连续点是基于给点时间的,这个操作独立与TOI事件。默认的碰撞检测在帧结束的时候计算特定联系点。
Figure 6.18. Pairs of objects found in proximity or contact at the end of the time step (objects selected within the box) generate contact points (small dot).接近或者重叠的物体对在帧结束的时候产生接触点。
At that time the collision detector does not know whether or how any occurring TOI events are going to be handled. It therefore generates contact information, which may be used by constraint solver in the next integration step (or may be overridden with newer information).
这个时间,碰撞检测器才会处理检测是否或者多少TOI事件发生,它会产生碰撞点,用来给解决器使用。
This part of collision detector functionality corresponds to the whole of the discrete collision detection in Havok. Note that thanks to TOI events there is no longer a need to have a large additional collision tolerance in hkpWorld. 有了TOI就不需要碰撞公差了!
For discrete collision detection, the tolerance layer was used to generate contact points early to avoid object penetration or tunneling in the following frame.
离散物理中,公差层用于早点产生碰撞点。
However, it is still a good idea to have a big (~10cm) collision layer as it can avoid CPU intensive TOI events. However a large collision tolerance can have the side-effect that objects bounce off each other before actually hitting each other. To avoid this side-effect, the collision tolerance can be reduced to a small value (around 1 cm), however more CPU time will be used to handle the additional TOIs.
无论如何,使用10cm的公差还是可以减少cpu秘籍的TOI事件,一个大的碰撞公差会导致物体在没碰到时就弹开,为了避免这个,碰撞公差可以再小点(1CM),但是cpu就要处理更多了TOI事件。
2.2.4. Continuous Simulation
The picture below highlights differences between discrete and continuous collision detection in Havok. Collision detection and Integration are common to both solutions. They are referred to as the Physical Synchronous Instance Step. The final while loop in the continuous collision detection is referred to as the TOI-handling loop. It handles occurring time-of-impact events chronologically, and is intended to replace some of the user's motion verification code usually created outside of the engine.
2.2.4.1. Physical Synchronous Instance Step
Physical Synchronous Instance (PSI) Step corresponds to the discrete simulation pass – it is responsible for performing a synchronized collision detection and integration for all bodies.
The discrete simulation approach is based on processing information from one physics frame only. Given the positions of bodies it performs collision detection and finds any colliding or overlapping object pairs that need to be corrected. The constraints solver uses that information to apply proper forces to the bodies. Finally the correct new state is integrated into the system.
In contrast, the continuous simulation approach assumes that it already has proper contact information for the current state at the beginning of each pass of the simulation loop. (Even if it doesn't, any unacceptable penetrations will be reported via TOI-events before the next frame is reached.) It runs the constraint solver and integrates the bodies' states first. This yields their predicted or potential final states.
Collision detection is responsible for verifying the bodies' movement from their initial to their final state. If there are no interrupting TOI events generated by the collision detector then the predicted state is validated and remains unchanged. Otherwise it may change as TOI events are handled (as is described in the following paragraphs).
2.2.4.2. Time-of-Impact Event Handling Loop
All TOI events for the entire world are kept on one list. The TOI-handling loop processes the events chronologically according to their time-of-impact.
In each TOI-event the motion of some bodies is altered. For those bodies, collision detection is performed again which may result in removal, modification, or addition of some TOI-events.
2.2.4.2.1. Solving Collision Impact Response解决碰撞冲击响应
For each event the two colliding bodies are identified and a simple collision response algorithm is used to process the impact in isolation from the rest of the world. (It will be shown in the Optimizations section below that we may also process a greater number of bodies in one TOI event.)
每个事件中的碰撞物体被指定,并且一个简答的碰撞响应算法被用来处理 隔离这个物体对世界其他物体的的影响。
Once the proper reaction is calculated, the motion states of the bodies must be recalculated. We set time t0 to the moment of impact and use the corresponding interpolated transform as the new initial transform at t0. Then we integrate the state to t1. Note that this time we integrate only by a fraction of the original PSI step.
一旦合适的计算了反应,物体的运动状态必须重新计算,我们设置碰撞的事件为t0,使用新的插值位置为新的t0位置,当到了t1,请注意,这个时候,我们仅由一小部分原始PSI步骤
2.2.4.2.2. Partial Collision Detection局部碰撞检测
After altering the motion of bodies according to the calculated collision response we run collision detection again to verify the newly calculated trajectories of the bodies. We must therefore collide each of the colliding bodies with each other and with the rest of the world's objects.
This generates a list of new TOI events, which are inserted to the current world's list of TOI events to be handled.
Also notice that previously calculated events, which referred to the reintegrated bodies, are now invalid. Such events are either removed or removed-and-replaced by the newly calculated events with new time-of-impact values.
2.2.5. Graphical Examples图例
The following examples illustrate the operation of the collision detector in various situations.
下面例子说明了碰撞检测器在各种情况下的操作。
2.2.5.1. Independent Collisions独立碰撞
Three spheres are moving in parallel, and are stationary relative to one another. In the picture below , the spheres at the bottom represent position at t0, and the uppermost spheres represent position at t1. They are about to hit a fixed wall (gray block).