close

Physics Joints 物理節點


Joints節點的

Joints are used to assemble more complex game objects from multiple rigid bodes. For example, joints would be used to join the limbs of a ragdoll figure, or the wheels of a car.
接頭是用來組裝更複雜的遊戲對象從多個剛性好兆頭。例如,將用於關節四肢加入一個布娃娃形象,或者是輪子的汽車。


Box2D includes a number of different joint types, each with different parameters and functions. Some joint types can be powered by motors, and others are subject to various physical restrictions in their ranges of motion.
Box2D中包括了一些不同類型的聯合,每一個不同的參數和功能。一些合資類型可以由電動機驅動,和其他人受到各種物理限制其活動範圍的議案。


The simplest joint type is the “pivot” joint, which joins two objects at a single pivot point, and is illustrated in the “Bridge” and “Chains” sample projects.
最簡單的聯合,一種是“支點“的聯合,它連接兩個對象在一個單一的支點,並體現在“Bridge“和“Chains“樣本項目。


To construct a joint, first construct the bodies that will be joined, and then submit those bodies to the desired joint constructor function.
要構造一個關節,首先構造的機構將被加入,然後提交這些機構所需的關節構造函數。


Pivot joint樞軸接頭


A pivot joint (also known as a “revolute joint” in Box2D terms) joins two bodies at an overlapping point. The initial arguments are the two bodies joined, followed by an anchor point in the global coordinates of the world.
一個支點聯合(也稱為“旋轉關節“在Box2D計算)連接兩個機構在一個重疊點。最初的參數是兩個機構的加盟,其次是錨點在全球坐標世界。


Pivot joints can optionally be constrained in their range of rotation. For example, if constructing a “ragdoll” figure, the head/neck joint would have a limited range of angular motion.
樞軸接頭可以選擇受限制的範圍內旋轉。例如,如果構建一個“布娃娃“的人物,頭部/頸部的聯合將有一個有限的範圍內的角運動。
----------------------
myJoint = physics.newJoint( "pivot", crateA, crateB, 200,300 )
-----------------------
Joint motors 聯合馬達


myJoint.isMotorEnabled -- (boolean)
myJoint.motorSpeed
myJoint.motorTorque  -- (get-only)
myJoint.maxMotorTorque  -- (set-only)
By default, joint motors have a fairly weak maximum torque, and therefore setting motorSpeed may appear to have little visible effect. Therefore, you should generally set maxMotorTorque to a high value (such as 100000) if you are trying to move any significant mass, such as rotating a wheel to power a car.
默認情況下,聯合電機有一個比較脆弱的最大扭矩,因此設置 motorSpeed​​可能似乎沒有什麼明顯的影響。因此,你應該設置 maxMotorTorque一般為較高的值(如100000),如果你試圖移動任何重大質量,如旋轉的輪子來驅動汽車。


Rotation limits旋轉限制


Pivot joints can optionally be constrained in their range of rotation. For example, if constructing a "ragdoll" figure, the head/neck joint would have a limited range of angular motion. These rotation limits are specified in degrees:
樞軸接頭可以選擇受限制的範圍內旋轉。例如,如果構建一個“布娃娃“的人物,頭部/頸部的聯合將有一個有限的範圍內的角運動。這些限制指定旋轉度:
---------------------------
myJoint.isLimitEnabled = true -- (boolean)
 myJoint:setRotationLimits( -45, 45 )
a1, a2 = myJoint:getRotationLimits()
--------------------------
Other attributes: 其它屬性
-------------------
myJoint.jointAngle -- (get-only; value in degrees)
myJoint.jointSpeed -- (get only; value in degrees per second)
-------------------
Distance joint距離聯合


A distance joint joins two bodies at a fixed distance. The initial arguments are the two bodies joined, followed by an (x,y) anchor point for each body. Anchor points are declared in the world coordinates.
加入聯合的距離兩個機構在一個固定的距離。最初的參數是兩個機構的加盟,其次是從(x,y)的定位點為每個機構。錨點宣布在世界上的坐標。
-----------------------
myJoint = physics.newJoint( "distance", crateA, crateB, crateA.x,crateA.y, crateB.x,crateB.y )
---------------------
Other attributes:其它屬性


myJoint.length
myJoint.frequency
myJoint.dampingRatio
length is the distance between the anchor points, which should not be zero or very short (you should use a pivot joint if you need to directly anchor one body to another).
長度是定位點之間的距離,不應是零或非常短(你應該使用一個樞軸接頭,如果您需要直接錨一體到另一個)。
frequency is the mass-spring damping frequency (in Hz)
頻率是質量彈簧阻尼頻率(赫茲)


dampingRatio ranges from 0, for no damping, to 1, for critical damping.
dampingRatio範圍為0,沒有阻尼,為 1,臨界阻尼。


Piston joint活塞聯合


A piston joint (also known as a “prismatic joint” in Box2D terms) joins two bodies along a single, restricted axis of motion, like the pistons or a shock absorber in a car. The initial arguments are the two bodies joined, followed by an anchor point in the first body and a vector defining the axis along which motion is allowed.
一個活塞聯合(也稱為“棱柱聯合“在Box2D計算)沿著連接兩個機構單一,限制軸的運動,如活塞或在汽車減震器。最初的參數是兩個機構的加盟,其次是錨點在首體與向量定義軸沿此議案是允許的。
-----------------------------
myJoint = physics.newJoint( "piston", crateA, crateB, crateA.x,crateA.y, axisDistanceX,axisDistanceY )
(Note: at least one of the bodies should be dynamic, with non-fixed rotation.)
-----------------------------
Joint  motors聯合馬達
A piston joint may also be driven by a motor. Unlike the pivot joint, this motion will be linear, along the specified axis, rather than rotational.
一個聯合也可能是活塞驅動電機。不同的是樞軸接頭,這項議案將是線性的,沿指定軸,而不是轉動。
-------------------------------------
myJoint.isMotorEnabled -- (boolean)
myJoint.motorSpeed -- (linear speed, in units of pixels per second)
myJoint.motorForce  -- (get-only)
myJoint.maxMotorForce  -- (set-only)
-------------------------------------
Motion limits極限運動
In addition, limits on its range of linear motion may be specified:
此外,它的範圍限制在直線運動可以指定:
-----------------------
myJoint.isLimitEnabled = true -- (boolean)
myJoint:setLimits( 100, 200 )
p1, p2 = myJoint:getLimits()
-----------------------
Other attributes:其它屬性
-------------------------------
myJoint.jointTranslation -- (get-only; linear value in pixels)
myJoint.jointSpeed -- (get only; value in pixels per second)
-------------------------------


Friction joint聯合摩擦
A friction joint is a special kind of pivot joint that resists motion, and is therefore “sticky”:
摩擦聯合是一種特殊類型的支點聯合抵制運動,因而是“粘“:
-------------------------
myJoint = physics.newJoint( "friction", crateA, crateB, 200,300 )
-------------------------
It exposes only two attributes:
它暴露了只有兩個屬性:
--------------------
myJoint.maxForce
myJoint.maxTorque
--------------------


Weld joint焊接接頭
A weld joint “welds” two bodies together at a specified point in world coordinates:
一個焊接接頭“焊接“兩個機構共同在一個指定的點在世界坐標:
------------------------
myJoint = physics.newJoint( "weld", crateA, crateB, 200,300 )
--------------------------
This joint does not move or rotate at all, and so it exposes no attributes.
However, due to mathematical approximation, this joint may appear slightly “soft” during motion, and so if you want to rigidly bond multiple shapes together, you may want to specify them as multiple body elements within a single complex body, rather than using weld joints.
這一聯合並不在所有移動或旋轉,因此它沒有公開的屬性。
然而,由於數學近似,這一聯合可能出現輕微的“軟”運動期間,因此如果你想多個形狀硬性粘結在一起,你可能要指定它們作為多體元素在一個複雜的機構,而不是用焊接頭。


Wheel joint輪子聯合
A wheel joint (also known as a “line joint” in Box2D terms) combines a piston and a pivot joint, like a wheel mounted on the shock absorber of a car. Most of its properties are inherited from the standard piston joint; the difference is that the body at the end of the axis is allowed to rotate freely.
一個輪聯合(也稱為“行聯合“在Box2D計算)結合了活塞和一個支點聯合,像一個車輪安裝在減震器的汽車。它的大部分特性都繼承自標準活塞關節;所不同的是,該機構在結束軸可以自由旋轉。
-------------------
myJoint = physics.newJoint( "wheel", crateA, crateB, crateA.x,crateA.y, axisDistanceX,axisDistanceY )
---------------------


Joint motors聯合馬達
Like a piston joint, the “shock absorber” portion of a wheel joint may be driven by a joint motor. This motion will be linear, along the specified axis, rather than rotational.
像活塞聯合,“減震器”輪聯合部分可能是由聯合汽車。這項議案將是線性的,沿指定軸,而不是轉動。
--------------------------
myJoint.isMotorEnabled -- (boolean)
myJoint.motorSpeed -- (linear speed, in units of pixels per second)
myJoint.motorForce  -- (get-only)
-- myJoint.maxMotorForce  -- (not currently supported in Box2D library; known bug)
----------------------------


Motion limits極限運動
In addition, limits on its range of linear motion may be specified:
此外,它的範圍限制在直線運動可以指定:
-----------------
myJoint.isLimitEnabled = true -- (boolean)
myJoint:setLimits( 100, 200 )
p1, p2 = myJoint:getLimits()
----------------------------
Other attributes:
-----------------------
myJoint.jointTranslation -- (get-only; linear value in pixels)
myJoint.jointSpeed -- (get only; value in pixels per second)
--------------------------


Pulley joint 滑輪聯合


A pulley joint attaches two bodies with an imaginary rope whose length remains constant: if one body is pulled down, the other one will move up.
滑輪兩個機構聯合重視與一個假想的繩子的長度保持不變:如果一個人身體拉下來,其他人會向上移動。


This constructor is a bit complicated, because it must specify a joint anchor point within each body, and also a stationary anchor point in world coordinates for each side of the “rope” to hang from. Finally, there is a “ratio” property to optionally simulate a block and tackle arrangement, in which one side of the rope moves faster than the other. By default, this ratio is 1.0, which simulates a simple pulley.
此構造是一個有點複雜,因為它必須指定一個聯合機構在每個定位點,也是一個固定的定位點在世界坐標,每一方的“繩索“,以懸掛。最後,有一個“比“屬性有選擇地模擬塊和解決的安排,其中一側的繩索移動速度比其他。默認情況下,這個比例是1.0,它模擬一個簡單的滑輪。
-------------------------
myJoint = physics.newJoint( "pulley", crateA, crateB, anchorA_x,anchorA_y, anchorB_x,anchorB_y, crateA.x,crateA.y, crateB.x,crateB.y, 1.0 )
-------------------------


It exposes the following read-only attributes:
它暴露了以下只讀屬性:
myJoint.length1 -- (get-only; value in pixels)
myJoint.length2 -- (get-only; value in pixels)
myJoint.ratio -- (get-only)
----------------------------

Touch joint觸摸聯合

A touch joint (based on the Box2D “mouse joint”) connects a single object to the current position of an onscreen touch. This connection is an elastic joint with specifiable strength and behavior, but since it is impossible to apply infinite forces to the simulation, the dragged object may "lag behind" a rapidly moving touch. However, this method allows far more realistic behavior than the non-physical drag method demonstrated in the earlier "DragPlatforms" sample.
一個觸摸聯合(基於 Box2D的“鼠標聯合“​​)連接一個對象到目前位置屏幕上的觸摸。這種連接是一種具有彈性關節可指定的力量和行為,但因為它是無窮的力量無法適用於模擬,拖動的對象可能“落後“快速移動的接觸。然而,這種方法允許更為現實的行為比非物理拖動方法表現在早期的“DragPlatforms“樣本。


For an example of touch joints, including their use with multitouch, see the "DebugDraw" sample code.
舉個例子觸摸關節,包括他們使用多點觸控,看到“DebugDraw“示例代碼。
--------------------
myJoint = physics.newJoint( "touch", crate, targetX,targetY )

---------------------------


The initial values of targetX and targetY should be given in world coordinates, but represent the target point on the object. For example, to drag the above object by its center point, you could do the following:
初始值targetX和targetY應在世界坐標,而是代表目標指向的對象。例如,要拖動上述對象的中心點,你可以做以下幾點:
----------------
touchJount = physics.newJoint( "touch", crate, crate.x, crate.y )
------------------------


Alternatively, to drag an object by the point it was touched on:
另外,要拖動的對象由點上它被碰到:
----------------
touchJount = physics.newJoint( "touch", crate, event.x, event.y )
------------------------
...where the "event" is passed by a touch listener. This behavior is illustrated in the "DebugDraw" sample code.
..這裡的“事件“是通過一個觸摸聽眾。這種現象是說明了“DebugDraw“示例代碼。
Note: because the touch joint can follow any (x,y) values, it can also be used for things besides tracking a touch: for example, it could be used to make an object follow a path, if that path was specified as a series of (x,y) values. This should make it useful for Flight Control-style line following applications, among other things.
注意:因為觸摸關節可以按照任何(的x,y)值,也可用於跟踪的東西,除了一個觸摸:例如,它可以用來使物體遵循的路徑,如果路徑被指定為一系列(的x,y)值。這應該是有用的飛控式線以下的申請,除其他事情。


In general, a touch joint creates a temporary elastic joint between the object and your finger, which makes the object attempt to follow the touch. But since the object remains under simulation, its motion may be stopped by other solid objects, and will interact fully with the other bodies in the world. Also, the object will rotate realistically under gravity when "picked up by one end". (To avoid this behavior, you can connect the touch joint to the object's center of mass rather than to the specific point on the object that you touched.)
一般來說,觸摸聯合創建一個臨時對象之間的彈性關節和你的手指,這使得該對象企圖跟隨接觸。但由於對象仍然處於模擬,其運動可能會停止其他固體物體,並將充分互動與其他機構在世界上。此外,該對象將在重力作用下旋轉切實當“拾起一結束“。 (為了避免這種行為,您可以觸摸到該對象聯合的質心,而不是具體的指向對象,你感動。)


Joint target聯合目標

The touch-following behavior of touch joints is created by passing the current touch (x,y) position to the joint:
觸摸觸摸關節以下行為是創建通過目前的接觸(的x,y)位置的聯合:


myJoint:setTarget( targetX, targetY )


While the updated (targetX, targetY) values will normally come from a touch, any values can be passed. For example, the object could be made to follow another object, or track the points of a path.
而最新的(targetX,targetY)值通常來自觸覺,可以傳遞任何值。例如,對象可以是另一個對象作出跟進,或追踪點的道路。


The current target values can also be accessed:
目前的目標價值,也可訪問:
-------------------------
targetX, targetY = myJoint:getTarget()
-------------------------


Maximum force最大力量


The overall speed or "lag" of the joint depends on the force exerted:
整體速度或“滯後“的聯合取決於施加的力:
-------------------
myJoint.maxForce = 10000
force = myJoint.maxForce
------------------------
By default, this attribute is set to 1000 times the mass of the body, which allows for fairly rapid dragging.
默認情況下,這個屬性設置為 1000倍,大量的身體,使之有相當快速拖動。


Frequency頻率

The mass-spring damping frequency of the elastic joint (in Hz):
質量的彈簧的彈性阻尼頻率聯合(單位 Hz):
------------------------
myJoint.frequency = 50
frequency = myJoint.frequency
----------------------

Damping ratio阻尼比

The damping ratio of the elastic joint, ranging from 0, for no damping, to 1, for critical damping:
阻尼比的彈性關節,從0,沒有阻尼,為 1,臨界阻尼:
---------------------
myJoint.dampingRatio = 0.2
damping = myJoint.dampingRatio
---------------------
The above attributes behave similarly to their counterparts in the Distance joint (see above). As in that case, the default values should work fairly well, but you may want to experiment with other values.
上述屬性的行為同樣的同行在遠程聯合(見上文)。正如在這種情況下,默認值應該還算不錯,但你可能要進行實驗的其他值。


Dragging objects with touch joints拖動對象觸摸關節


Here is a general function for making a physics object draggable, as seen in the "DebugDraw" sample code:
這裡是一個通用函數用於製造一個物理對象拖動,就像在“DebugDraw“示例代碼:
-------------------
local physics = require("physics")
physics.start()
system.activate( "multitouch" )
 ----------------------
-- A general function for dragging physics bodies
一個通用函數物理機構拖拉
-------------------------
local function dragBody( event )
        local body = event.target
        local phase = event.phase
        local stage = display.getCurrentStage()
 
        if "began" == phase then
                stage:setFocus( body, event.id )
                body.isFocus = true
 
                -- Create a temporary touch joint and store it in the object for later reference
                body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
 
        elseif body.isFocus then
                if "moved" == phase then
                
                        -- Update the joint to track the touch
                        body.tempJoint:setTarget( event.x, event.y )
 
                elseif "ended" == phase or "cancelled" == phase then
                        stage:setFocus( body, nil )
                        body.isFocus = false
                        
                        -- Remove the joint when the touch ends                 
                        body.tempJoint:removeSelf()
                        
                end
        end
 
        -- Stop further propagation of touch event
        return true
end
-------------------------------------
-- Add a physics object 添加一個物理對象
--------------------
crate = display.newImage( "crate.png" )
physics.addBody( crate, { density=0.8 } )
-------------------
-- Make object draggable 使對象可拖動
---------------------------
crate:addEventListener( "touch", dragBody )
------------------------


Common features of all joints
所有接頭的共同特點
In addition to the above, all joint types share several common attributes and functions:
除上述情況外,所有類型共享聯合幾個共同的屬性和功能:
------------------
myJoint:getAnchorA
myJoint:getAnchorB
-------------------
A function that returns the (x,y) coordinates of the joint’s anchor points within objects A and B, which are the two joined objects. The values are given in the local coordinates of each object, so an anchor point at the center of the object would be at position (0, 0).
函數返回的(x,y)坐標的聯合的錨點在對象 A和B,這是兩個參與對象。給出的值在每個對象的局部坐標,使錨點在該中心的對象將在位置(0,0)。
----------------------------
x, y = myJoint:getAnchorA()
x, y = myJoint:getAnchorB()
----------------------------


myJoint:getReactionForce
A function that returns the reaction force (in Newtons) at the joint anchor in the second body.
一個函數,返回反作用力(在牛頓)在接頭錨點第二身體。
-------------------
reactionForceX, reactionForceY = myJoint:getReactionForce()
------------------------


myJoint.reactionTorque
A read-only property that returns the reaction torque (in N*m) at the joint anchor in the second body.
只讀屬性,返回反作用力矩(牛頓米)在接頭錨點第二身體。
------------------
reactionTorque = myJoint.reactionTorque
---------------------


Destroying joints關節破壞
To destroy an existing joint, and detach the two bodies (which may then collide), use removeSelf():
要銷毀現有的聯合和分離的兩個機構(可能當時發生碰撞),使用removeSelf():
-----------------
myJoint:removeSelf()
------------------

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 createps 的頭像
    createps

    遊戲人生 人生遊戲

    createps 發表在 痞客邦 留言(0) 人氣()