通過剛體組件控制物體的移動

一.Rigidbody的基本介紹
以下翻譯主要引用自:http://www.ceeger.com/Components/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.

剛體組件使你的遊戲對象的行爲受到物理規律的約束。剛體組件能夠接收外力和扭矩從而使你的對象按照真實情況進行運動。爲了讓物體能夠受到重力的影響,在外力的作用下產生行爲,或者通過NVIDIA物理運算引擎和其他對象交互,任何遊戲對象都需要包含一個剛體組件。

Properties 屬性 ————-Property: Function:

  1. Mass 質量 The mass of the object (in kilograms by default).
    物體的質量(默認單位爲kg)

  2. 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意味着沒有,無窮大代表着使對象立刻停止運動。

  3. 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

  4. Use Gravity 使用重力 If enabled, the object is affected by gravity.
    若開啓,對象就會受重力因素的影響

  5. 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屬性,來控制對象。這對可移動平臺是很有用的,或者當你想要模擬剛體。(適用於模擬運動的平臺或者模擬受鉸鏈關節連接的剛體。)

  6. Interpolate 插值 Try one of the options only if you are seeing
    jerkiness in your Rigidbody’s movement. 當你發現剛體運動時抖動,可以嘗試該選項

    • None No Interpolation is applied. 不使用插值
  7. -Interpolate 內插值 Transform is smoothed based on the Transform of the previous frame. 基於上一幀的變換來平滑本幀變換。

  8. -Extrapolate 外插值 Transform is smoothed based on the estimated Transform of the next frame. 基於下一幀的預估變換來平滑本幀變換。

  9. Collision Detection 碰撞檢測 Used to prevent fast moving objects from
    passing through other objects without

  10. detecting collisions. 碰撞檢測模式。用於避免高速物體穿過其他物體,卻未觸發碰撞。

  11. Discrete 不連續、離散 Use Discreet collision detection against all other
    colliders in the scene. Other colliders will use Discreet collision
    detection when testing for collision against it. Used for normal
    collisions (This is the default value).
    不連續碰撞檢測。使用不連續碰撞檢測模式來與場景中其他碰撞器進行碰撞檢測。其他物體與它的碰撞檢測,也會應用這種模式。適用於普通碰撞(這是默認的模式)。

  12. -Continuous 連續 Use Discrete collision detection against dynamic colliders (with a rigidbody) and continuous collision detection
    against static MeshColliders (without a rigidbody). Rigidbodies set
    to Continuous Dynamic will use continuous collision detection when
    testing for collision against this rigidbody. Other rigidbodies
    will use Discreet 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

  13. 動態連續 Use continuous collision detection against objects set to
    Continuous and Continuous Dynamic Collision. It will also use
    continuous collision detection against static MeshColliders
    (without a rigidbody). For all other colliders it uses discreet
    collision detection. Used for fast moving objects.
    連續動態碰撞檢測。使用連續動態碰撞檢測模式來檢測與連續模式和連續動態模式的物體間的碰撞。也適用於與靜態網格(非剛體)的碰撞檢測。而與之碰撞的其他模式的物體,採用的是不連續動態碰撞檢測模式。適用於高速物體。
    Constraints 約束 Restrictions on the Rigidbody’s motion:- 對剛體運動的約束。

  14. Freeze Position
    凍結位置 Stops the Rigidbody moving in the world X, Y and Z axes selectively.
    剛體在世界中沿所選X,Y,Z軸的移動,將無效。(選擇哪個座標軸,沿那個座標軸的作用將無效)
  15. Freeze Rotation
    凍結旋轉 Stops the Rigidbody rotating around the world 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 realistic collisions, varied types of joints, and other very cool behaviors. 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.

剛體讓物體可以在物理引擎的控制下運動。他打開了通向真實碰撞的大門,實現各種類型的關節及其他很酷的行爲。通過力來操縱物體,與直接通過變換不同,有一種不同的感覺。通常情況下,對同一物體,要麼通過剛體操縱,要麼通過變換操縱。
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和使用RigidBody之間的最大不同是力的使用。剛體可以接收外力和扭矩,但是變換不能。Transforms可以實現平移和旋轉,但這和使用物理方式實現是不同的。當你親自嘗試的時候你會發現明顯的區別。對剛體施加力和扭矩實際上改變的也是對象的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.

剛體在受物理引擎影響之前,必須明確添加給物體。你可以通過選中物體,然後在菜單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.

當一個物體處於物理控制中,他會以半獨立的方式隨着變換的父親的移動而移動。如果你移動父物體,那麼它會將子剛體拖向它。但是,這個剛體仍然會根據重力下落或進行碰撞反應。
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時就不要使用Rigidbody等物理組件控制了)。

二.使用Rigidbody控制物體運動

這裏主要記錄的是通過AddForce()和AddTorque(),以及AddRelativeForce()和AddRelativeTorque()方法控制物體的移動和旋轉。
 通過該組件自帶的方法AddForce()可實現物體沿任意方向的運動,但該種控制方式不會考慮被控制對象的自身狀態,可能會導致物體的運動方向和物體的朝向不一致,比如:控制場景中的汽車運動,增加力使之向前運動時,物體向前運動了,但是汽車頭卻並不是向前(我們想要的是沿着汽車頭的方向向前走)。這時可以使用AddRelativeForce()方法,施加力時它是以自身座標系爲基準,這可以保證力的方向與物體自身方向一致。
通過AddTorque()和AddRelativeTorque()是給物體增加一個旋轉力矩,使之旋轉。
通過設置剛體的velocity屬性爲0,可使物體停止移動

通過設置剛體的angularVelocit屬性爲0,可使物體停止旋轉。
如下面所示:通過I,K,J,L和space按鍵實現物體的移動旋轉和停止。

 [html] view plain copy void Update () {  

        //float moveHorizontal = Input.GetAxis ("Horizontal");  
        //float moveVertical = Input.GetAxis ("Vertical");  

        //Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);  
        //rb.AddForce (movement * speed);  
        if(Input.GetKey(KeyCode.I)){  
          this.GetComponent<Rigidbody> ().AddRelativeForce (new Vector3(-10f,0f,0f);              }  
        if(Input.GetKey(KeyCode.K)){  
          this.GetComponent<Rigidbody> ().AddRelativeForce (new Vector3(10f,0f,0f));  
        }  
        if(Input.GetKey(KeyCode.J)){  
          this.GetComponent<Rigidbody> ().AddTorque (new Vector3(0F,-20F,0f));  
        }  
        if(Input.GetKey(KeyCode.I)){  
          this.GetComponent<Rigidbody> ().AddTorque (new Vector3(0F,20F,2f));  
        }  
        if (Input.GetKey (KeyCode.Space)) {  
          this.GetComponent<Rigidbody> ().velocity = new Vector3 (0f,0f,0f);  
        }   }

注:在學習的過程中發現,有時給一個物體施加力時,物體並不移動,其原因可能是:作用於物體的力太小,無法使物體產生運動,這時可以給物體的碰撞器添加一個物理材質,把摩擦力設爲0進行嘗試。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章