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.
碰撞體是另一種必須與剛體一起添加的組件,以允許發生碰撞。如果兩個剛體相互碰撞,物理引擎不會計算碰撞,除非兩個物體都有一個碰撞體組件。在物理模擬過程中,無碰撞體的剛體只是簡單地通過彼此。
在這裏插入圖片描述
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.
混合碰撞器是原始碰撞器的組合,作爲一個剛體共同作用。當您的模型過於複雜或性能昂貴,無法精確地模擬,並且希望使用簡單的近似以最佳方式模擬形狀的碰撞時,它們就派上了用場。要創建一個混合碰撞體,請創建碰撞對象的子對象,然後向每個子對象添加一個碰撞體組件。這允許您輕鬆地、獨立地擺放位置、旋轉和縮放每個碰撞體。您可以使用許多基本碰撞體和/或凸網格碰撞體來構建混合碰撞體。

在這裏插入圖片描述
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.
    你不能僅僅通過將一個物體的角阻力設爲無窮大就使它停止旋轉。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章