天天看點

通過剛體元件控制物體的運動

一.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:

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.

當你發現剛體運動時抖動,可以嘗試該選項

- None

No Interpolation is applied.

不使用插值

-Interpolate

内插值

Transform is smoothed based on the Transform of the previous frame.

基于上一幀的變換來平滑本幀變換。

-Extrapolate

外插值

Transform is smoothed based on the estimated Transform of the next frame.

基于下一幀的預估變換來平滑本幀變換。

Collision Detection

碰撞檢測

Used to prevent fast moving objects from passing through other objects without detecting collisions.

碰撞檢測模式。用于避免高速物體穿過其他物體,卻未觸發碰撞。

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

不連續碰撞檢測。使用不連續碰撞檢測模式來與場景中其他碰撞器進行碰撞檢測。其他物體與它的碰撞檢測,也會應用這種模式。适用于普通碰撞(這是預設的模式)。

-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

動态連續

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:-

對剛體運動的限制。

- 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 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按鍵實作物體的移動旋轉和停止。

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進行嘗試。

繼續閱讀