Rigidbody 刚体 Physics物理系列之二

Rigidbody 刚体

本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.4版本
参考链接:https://docs.unity3d.com/Manual/class-Rigidbody.html

Rigidbodies enable your GameObjects to act under the control of physics. The Rigidbody can receive forces and torque to make your objects move in a realistic way. Any GameObject must contain a Rigidbody to be influenced by gravity, act under added forces via scripting, or interact with other objects through the NVIDIA PhysX physics engine.
刚体可使你的游戏对象在物理的控制下运动。刚体可以接受力和转矩,使您的对象以一种真实的方式移动。GameObject(游戏对象)想受重力影响,或通过脚本在施加的力下工作,或通过NVIDIA PhysX物理引擎与其他对象交互都必须包含刚体组件。
在这里插入图片描述

Property Function
Mass The mass of the object (in kilograms by default).
质量 对象的质量(以kg为单位)
Drag How much air resistance affects the object when moving from forces. 0 means no air resistance, and infinity makes the object stop moving immediately.
拖曳 当物体离开力时,空气阻力对物体的影响有多大。0表示没有空气阻力,无穷大表示物体立即停止运动。
Angular Drag How much air resistance affects the object when rotating from torque. 0 means no air resistance. Note that you cannot make the object stop rotating just by setting its Angular Drag to infinity.
角阻力 从转矩旋转物体时,空气阻力对物体的影响程度。0表示没有空气阻力。注意,您不能仅通过将对象的角阻力设置为无穷大来停止旋转。
Use Gravity If enabled, the object is affected by gravity.
使用重力 开启,受重力影响
Is Kinematic If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached.
是运动学 如果启用,对象将不会由物理引擎驱动,而只能由其Transform操作。这对于移动平台很有用,或者如果你想让一个有铰链连接的刚体动起来。
Interpolate Try one of the options only if you are seeing jerkiness in your Rigidbody’s movement.
插值 只有当你看到你的刚体抖动时,你才可以尝试其中一种。
- Interpolate Transform is smoothed based on the Transform of the previous frame.
-插值 在前一帧Transform 的基础上对Transform 进行平滑处理。
-Extrapolate Transform is smoothed based on the estimated Transform of the next frame.
-推断 根据下一帧Transform 的估计平滑Transform 。
Collision Detection Used to prevent fast moving objects from passing through other objects without detecting collisions.
碰撞检测 用于防止快速移动的物体在没有检测到碰撞的情况下通过其他物体。
-Discrete Use discrete collision detection against all other Colliders in the Scene. Other colliders will use discrete collision detection when testing for collision against it. Used for normal collisions (This is the default value).
-离散 对场景中的所有其他碰撞体使用离散的碰撞检测。其他碰撞体在对其进行碰撞测试时将使用离散碰撞检测。用于一般的碰撞(这是默认值)。
-Continuous Use Discrete collision detection against dynamic Colliders (with a Rigidbody) and sweep-based continuous collision detection against static Colliders (without a Rigidbody). Rigidbodies set to Continuous Dynamic will use continuous collision detection when testing for collision against this rigidbody. Other rigidbodies will use Discrete Collision detection. Used for objects which the Continuous Dynamic detection needs to collide with. (This has a big impact on physics performance, leave it set to Discrete, if you don’t have issues with collisions of fast objects)
-连续 对动态碰撞体(挂刚体)使用离散碰撞检测和对静态碰撞体(没有刚体)使用扫描式的连续碰撞检测。对方设置为连续动态的刚体,在测试与此刚体的碰撞时将使用连续碰撞检测。其他刚体将使用离散碰撞检测。碰撞到一起的时候才用于连续动态检测。(这对物理性能有很大的影响,如果你没有快速物体的碰撞问题,就把它设置成离散的)
-Continuous Dynamic Use sweep-based continuous collision detection against GameOjects set to Continuous and Continuous Dynamic collision. It will also use continuous collision detection against static Colliders (without a Rigidbody). For all other colliders, it uses discrete collision detection. Used for fast moving objects.
-连续动态 对设置为连续和连续动态碰撞的游戏对象使用基于扫描的连续碰撞检测。它还将对静态碰撞器(没有刚体)使用连续碰撞检测。对于所有其他碰撞体,它使用离散碰撞检测。用于快速移动的物体。
- Continuous Speculative Use speculative continuous collision detection against Rigidbodies and Colliders. This is also the only CCD mode that you can set kinematic bodies. This method tends to be less expensive than sweep-based continuous collision detection.
-连续推测式 对刚体和碰撞体使用连续推测式碰撞检测。这也是唯一的你设置为运动学身体的CCD模式。与基于扫描的连续碰撞检测方法相比,该方法的开销更小。
Constraints Restrictions on the Rigidbody’s motion:-
约束条件 刚体运动的限制:
- Freeze Position Stops the Rigidbody moving in the world X, Y and Z axes selectively.
-冻结位置 停止刚体在X轴、Y轴和Z轴上的运动。
- Freeze Rotation Stops the Rigidbody rotating around the local X, Y and Z axes selectively.
-冻结旋转 阻止刚体绕局部X轴、Y轴和Z轴可选性旋转。

Details

Rigidbodies allow your GameObjects to act under control of the physics engine. This opens the gateway to behaviors such as realistic collisions and varied types of joints. Manipulating your GameObjects by adding forces to a Rigidbody creates a very different feel and look than adjusting the Transform Component directly. Generally, you shouldn’t manipulate the Rigidbody and the Transform of the same GameObject - only one or the other.
刚体允许游戏对象在物理引擎的控制下运行。这打开了通往响应动作的大门,比如真实的碰撞和不同关节的类型。通过向刚体添加力来操纵游戏对象,与直接调整Transform组件相比,会创建一种非常不同的感观和效果。一般来说,你不应该同时操纵同一个GameObject(游戏物体)的刚体和Transform -只可选择其一。

The biggest difference between manipulating the Transform versus the Rigidbody is the use of forces. Rigidbodies can receive forces and torque, but Transforms cannot. Transforms can be translated and rotated, but this is not the same as using physics. You’ll notice the distinct difference when you try it for yourself. Adding forces/torque to the Rigidbody will actually change the object’s position and rotation of the Transform component. This is why you should only be using one or the other. Changing the Transform while using physics could cause problems with collisions and other calculations.
操纵Transform 和刚体之间最大的区别是力的使用。刚体可以接受力和力矩,但Transform 不能。Transform 可以平移和旋转,但这与使用物理是不同的。当你自己尝试的时候,你会注意到明显的不同。向刚体添加力/力矩实际上会改变对象的Transform组件的位置和旋转。这就是为什么你应该只使用其中之一。在使用物理时改变Transform可能会导致碰撞和其他计算问题。

Rigidbodies must be explicitly added to your GameObject before they will be affected by the physics engine. You can add a Rigidbody to your selected object from Components->Physics->Rigidbody in the menu. Now your object is physics-ready; it will fall under gravity and can receive forces via scripting, but you may need to add a Collider or a Joint to get it to behave exactly how you want.
刚体必须被显式地添加到GameObject中,才会受到物理引擎的影响。您可以在菜单中从Components->Physics->Rigidbody中向所选对象添加一个刚体。现在你的对象已经准备好接受力的影响了;它将在重力作用下掉落,并可以通过脚本接收力,但您可能需要添加一个碰撞体或一个铰链,以使它完全按照您想要的方式运动。

Parenting

When an object is under physics control, it moves semi-independently of the way its transform parents move. If you move any parents, they will pull the Rigidbody child along with them. However, the Rigidbodies will still fall down due to gravity and react to collision events.
当一个物体处于物理控制之下时,它的运动半独立于它的transform的父结点的运动。如果你移动任何父结点,他们会把刚体的孩子也一起移动。然而,刚体仍然会由于重力而下落,并对碰撞事件作出反应。

Scripting

To control your Rigidbodies, you will primarily use scripts to add forces or torque. You do this by calling AddForce() and AddTorque() on the object’s Rigidbody. Remember that you shouldn’t be directly altering the object’s Transform when you are using physics.
为了控制刚体,主要用脚本来添加力或力矩。您可以通过调用对象刚体上的AddForce()和AddTorque()来实现这一点。记住,在使用物理时,不应该直接改变对象的Transform。

Animation

For some situations, mainly creating ragdoll effects, it is neccessary to switch control of the object between animations and physics. For this purpose Rigidbodies can be marked isKinematic. While the Rigidbody is marked isKinematic, it will not be affected by collisions, forces, or any other part of the physics system. This means that you will have to control the object by manipulating the Transform component directly. Kinematic Rigidbodies will affect other objects, but they themselves will not be affected by physics. For example, Joints which are attached to Kinematic objects will constrain any other Rigidbodies attached to them and Kinematic Rigidbodies will affect other Rigidbodies through collisions.
在某些情况下,主要是创建布娃娃效果,在动画和物理之间有必要地切换对象的控制。为此目的,刚体可以标为isKinematic(运动学)。当刚体被标记为运动学,则它不会受到碰撞、力或物理系统的任何其他部分的影响。这意味着您必须通过直接操作Transform 组件来控制对象。运动学刚体会影响其他对象,但它们本身不受物理影响。例如,连接到运动学物体上的铰链会约束任何附加到运动学物体上的刚体,运动学刚体会通过碰撞影响其他刚体。

Colliders

Colliders are another kind of component that must be added alongside the Rigidbody in order to allow collisions to occur. If two Rigidbodies bump into each other, the physics engine will not calculate a collision unless both objects also have a Collider attached. Collider-less Rigidbodies will simply pass through each other during physics simulation.
碰撞体是另一种必须与刚体一起添加的组件,以允许发生碰撞。如果两个刚体相互碰撞,物理引擎不会计算碰撞,除非两个物体都有一个碰撞体组件。在物理模拟过程中,无碰撞体的刚体只是简单地通过彼此。
Rigidbody 刚体 Physics物理系列之二_第1张图片
Colliders define the physical boundaries of a Rigidbody
碰撞体定义了刚体的物理边界

Add a Collider with the Component->Physics menu. View the Component Reference page of any individual Collider for more specific information:

  • Box Collider - primitive shape of a cube 立方体形状
  • Sphere Collider - primitive shape of a sphere 球形
  • Capsule Collider - primitive shape of a capsule 胶囊体
  • Mesh Collider - creates a collider from the object’s mesh, cannot collide with another Mesh Collider 网格碰撞体,不能和其他网格碰撞体碰撞检测
  • Wheel Collider - specifically for creating cars or other moving vehicles 车轮或类似轮子
  • Terrain Collider - handles collision with Unity’s terrain system 地型

Compound Colliders 混合碰撞器

Compound Colliders are combinations of primitive Colliders, collectively acting as a single Rigidbody. They come in handy when you have a model that would be too complex or costly in terms of performance to simulate exactly, and want to simulate the collision of the shape in an optimal way using simple approximations. To create a Compound Collider, create child objects of your colliding object, then add a Collider component to each child object. This allows you to position, rotate, and scale each Collider easily and independently of one another. You can build your compound collider out of a number of primitive colliders and/or convex mesh colliders.
混合碰撞器是原始碰撞器的组合,作为一个刚体共同作用。当您的模型过于复杂或性能昂贵,无法精确地模拟,并且希望使用简单的近似以最佳方式模拟形状的碰撞时,它们就派上了用场。要创建一个混合碰撞体,请创建碰撞对象的子对象,然后向每个子对象添加一个碰撞体组件。这允许您轻松地、独立地摆放位置、旋转和缩放每个碰撞体。您可以使用许多基本碰撞体和/或凸网格碰撞体来构建混合碰撞体。

Rigidbody 刚体 Physics物理系列之二_第2张图片
A real-world Compound Collider setup
一个真实的混合碰撞体设置

In the above picture, the Gun Model GameObject has a Rigidbody attached, and multiple primitive Colliders as child GameObjects. When the Rigidbody parent is moved around by forces, the child Colliders move along with it. The primitive Colliders will collide with the environment’s Mesh Collider, and the parent Rigidbody will alter the way it moves based on forces being applied to it and how its child Colliders interact with other Colliders in the Scene.
在上面的图片中,Gun Model GameObject有一个刚体,多个基本碰撞体作为子GameObjects。当刚体父体被力移动时,子碰撞体也随之移动。基本碰撞体将会与环境中的网格碰撞体发生碰撞,父结点刚体将根据施加于其上的力以及子结点碰撞体与场景中其他碰撞体的交互改变其移动轨迹。

Mesh Colliders can’t normally collide with each other. If a Mesh Collider is marked as Convex, then it can collide with another Mesh Collider. The typical solution is to use primitive Colliders for any objects that move, and Mesh Colliders for static background objects.
网格碰撞体通常不会相互碰撞。如果一个网格碰撞器被标记为凸,那么它就会与另一个网格碰撞器发生碰撞。典型的解决方案是对任何移动的对象使用基本碰撞体,对静态背景对象使用网格碰撞体。

Note: Compound colliders return individual callbacks for each collider collision pair when using Collision Callbacks.
注意:使用碰撞回调函数时,混合碰撞体会为每个碰撞体的碰撞对返回单独的回调。

Continuous Collision Detection 连续式碰撞检测

Continuous collision detection is a feature to prevent fast-moving colliders from passing each other. This may happen when using normal (Discrete) collision detection, when an object is one side of a collider in one frame, and already passed the collider in the next frame. To solve this, you can enable continuous collision detection on the rigidbody of the fast-moving object. Set the collision detection mode to Continuous to prevent the rigidbody from passing through any static (ie, non-rigidbody) MeshColliders. Set it to Continuous Dynamic to also prevent the rigidbody from passing through any other supported rigidbodies with collision detection mode set to Continuous or Continuous Dynamic. Continuous collision detection is supported for Box-, Sphere- and CapsuleColliders. Note that continuous collision detection is intended as a safety net to catch collisions in cases where objects would otherwise pass through each other, but will not deliver physically accurate collision results, so you might still consider decreasing the fixed Time step value in the TimeManager inspector to make the simulation more precise, if you run into problems with fast moving objects.
连续碰撞检测是一种防止快速移动的碰撞体互相穿透的特性。这可能发生在使用常规的(离散)碰撞检测时,当一个对象在一帧中是碰撞体的这一侧,并且在下一帧中穿过了碰撞体到那一侧时。为了解决这个问题,您可以在快速移动的对象的刚体上启用连续碰撞检测。将碰撞检测模式设置为连续模式,以防止刚体通过任何静态(即非刚体)网格碰撞器。将其设置为连续动态,也会防止刚体穿过其他任一支持刚体并将碰撞检测模式设置为连续或连续动态。连续碰撞检测支持的立方体,球和胶囊体的碰撞体。注意,连续碰撞检测的目的是作为一个安全网, 以捕捉物体碰撞时会穿过对方的情况, 但不会提供准确的物理碰撞结果, 如果您遇到快速移动对象的问题,您可能仍然会考虑在TimeManager检查器中减少fixed Time step(固定时间步长)值,以使模拟更加精确。

Use the right size 用对大小

The size of the your GameObject’s mesh is much more important than the mass of the Rigidbody. If you find that your Rigidbody is not behaving exactly how you expect - it moves slowly, floats, or doesn’t collide correctly - consider adjusting the scale of your mesh asset. Unity’s default unit scale is 1 unit = 1 meter, so the scale of your imported mesh is maintained, and applied to physics calculations. For example, a crumbling skyscraper is going to fall apart very differently than a tower made of toy blocks, so objects of different sizes should be modeled to accurate scale.
游戏对象网格的大小比刚体的质量重要得多。如果你发现你的刚体没有完全按照你所期望的那样运动——它移动缓慢,漂浮,或者没有正确碰撞——考虑调整网格资产的缩放比例。Unity的默认单位比例尺是1单位= 1米,所以你导入的网格的比例尺会被保持,并应用到物理计算中。例如,一栋摇摇欲坠的摩天大楼和一座由玩具积木组成的大楼将会以非常不同的方式倒塌,所以不同大小的物体应该按照精确的比例建模。

If you are modeling a human make sure the model is around 2 meters tall in Unity. To check if your object has the right size compare it to the default cube. You can create a cube using GameObject > 3D Object > Cube. The cube’s height will be exactly 1 meter, so your human should be twice as tall.
如果你要为一个人建模,确保模型在Unity中大约有2米高。要检查对象是否具有正确的大小,请将其与默认立方体进行比较。您可以使用GameObject > 3D Object > cube创建一个立方体。立方体的高度正好是1米,所以你的人应该是两倍高。

If you aren’t able to adjust the mesh itself, you can change the uniform scale of a particular mesh asset by selecting it in Project View and choosing Assets->Import Settings… from the menu. Here, you can change the scale and re-import your mesh.
如果您不能调整网格本身,您可以通过在项目视图中选择它并从菜单中选择Assets->Import Settings…来更改特定网格资产的统一缩放比例。在这里,你可以改变比例和重新导入你的网格。

If your game requires that your GameObject needs to be instantiated at different scales, it is okay to adjust the values of your Transform’s scale axes. The downside is that the physics simulation must do more work at the time the object is instantiated, and could cause a performance drop in your game. This isn’t a terrible loss, but it is not as efficient as finalizing your scale with the other two options. Also keep in mind that non-uniform scales can create undesirable behaviors when Parenting is used. For these reasons it is always optimal to create your object at the correct scale in your modeling application.
如果你的游戏需要你的GameObject在不同的尺度上被实例化,你可以调整Transform的缩放轴的值。缺点是物理模拟必须在对象实例化时做更多的工作,并可能导致游戏性能下降。这并不是一个可怕的损失,但它不能和最后确定你的缩放值的其他两个选项的效率对比。同时也要记住,当使用未统一的缩放值时,当作父结点使用时会产生不可估计的结果。由于这些原因,在建模应用程序中以正确的统一缩放比例创建对象总是最优的。

Hints 提示

  • The relative Mass of two Rigidbodies determines how they react when they collide with each other.
    两个刚体的相对质量决定了它们相互碰撞时的反应。
  • Making one Rigidbody have greater Mass than another does not make it fall faster in free fall. Use Drag for that.
    希望让一个刚体比另一个刚体有更大的质量,并且不会使它在自由落体中下降得更快。可以考虑使用拖曳值。
  • A low Drag value makes an object seem heavy. A high one makes it seem light. Typical values for Drag are between .001 (solid block of metal) and 10 (feather).
    较低的拖曳值使对象看起来很重。一个高的使它看起来很轻。阻力的典型值介于.001(金属块)和10(羽毛)之间。
  • If you are directly manipulating the Transform component of your object but still want physics, attach a Rigidbody and make it Kinematic.
    如果你直接操纵你的物体的Transform组件,但仍然想要物理,挂载一个刚体,使它运动学开启。
  • If you are moving a GameObject through its Transform component but you want to receive Collision/Trigger messages, you must attach a Rigidbody to the object that is moving.
    如果你正在通过它的Transform组件移动GameObject,但你想接收碰撞/触发消息,则必须将一个刚体挂载到正在移动的对象上。
  • You cannot make an object stop rotating just by setting its Angular Drag to infinity.
    你不能仅仅通过将一个物体的角阻力设为无穷大就使它停止旋转。

你可能感兴趣的:(Physics)