physics.addBody()
Description:
Allows you to turn any Corona display object into a simulated physical object with one line of code, including the assignment of physical properties.
可讓您在任何顯示對象Corona成模擬的物理對象一行代碼,包括分配的物理特性。
If no shape information is specified, then the body boundaries will snap to the rectangular boundaries of the display object used to create the physics body.
如果沒有指定形狀的信息,然後身體邊界將捕捉到的矩形邊界的顯示對象,用於創建物理身體。
If a radius is specified, then the body boundaries will be circular, centered at the middle of the display object used to create the physics body.
如果指定的半徑,那麼身體邊界將被通知,集中在中間的顯示對象,用於創建物理身體。
If a shape is specified, then the body boundaries will follow the polygon provided by the shape. Note that the maximum number of sides per shape is eight, and all angles must be convex. (Specifying a shape will override the radius property, if both radius and shape are specified in the same element.)
如果指定了一個形狀,然後身體邊界將按照所提供的多邊形形狀。請注意,每邊最多的形狀是八,所有的角度必須是凸起。 (指定的形狀將覆蓋半徑屬性,如果兩個半徑和形狀相同的元素中指定。)
A body shape is simply a table of local (x,y) coordinates, relative to the center of the display object; see example below.
一個身材只是一個表地方(的x,y)坐標,相對於中心的顯示對象,見下面的例子。
Finally, a more complex body may be constructed by specifying multiple body elements and shapes. In cases where the overall shape of the object is concave, or the shape has more than eight sides, you can use multiple body elements to construct it.
最後,一個更複雜的身體構造,可通過指定多個體元素和形狀。在情況下,整體造型的對象是凹的,或形狀有超過八個方面,可以使用多種元素來構造它的身體。
Each body element may have its own physical properties, and the body element index is reported as part of each collision event -- so it is possible to detect which part of a complex body was involved in a collision.
每個 body元素可以有自己的物理性質,和身體元素的索引報導的一部分,每一個碰撞事件- 因此它可以檢測哪部分是一個複雜的機構參與了衝突。
For further information, see Physics Bodies.
如需進一步信息,請參閱物理機構。
Syntax:語法
physics.addBody(object, [bodyType,] {density=d, friction=f, bounce=b [,radius=r]})
physics.addBody(object, [bodyType,] {density=d, friction=f, bounce=b [,shape=s]})
-- For a complex body (multiple body elements) 對於一個複雜的身體(身體多個元素)
physics.addBody(object, [bodyType,]
{density=d1, friction=f1, bounce=b1, shape=s1},
{density=d2, friction=f2, bounce=b2, shape=s2},
{density=d3, friction=f3, bounce=b3, shape=s3}
)
----------------------------------
Example:
Default (rectangular) bodies
local physics = require( "physics" )
physics.start()
local sky = display.newImage( "bkg_clouds.png" )
sky.x = 160; sky.y = 195
local ground = display.newImage( "ground.png" )
ground.x = 160; ground.y = 445
physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } )
local crate = display.newImage( "crate.png" )
crate.x = 180; crate.y = -50; crate.rotation = 5
physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } )
Circular bodies
local ball = display.newImage("ball.png")
physics.addBody( ball, { density = 1.0, friction = 0.3, bounce = 0.2, radius = 25 } )
Polygon bodies
local pentagon = display.newImage("pentagon.png")
pentagonShape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }
physics.addBody( pentagon, { density=3.0, friction=0.8, bounce=0.3, shape=pentagonShape } )
------------------------------------------------
Parameters:
object
object: A display object.
bodyType
string: The body type may be specified in an optional string parameter before the first body element. The possible types are “static”, “dynamic” and “kinematic”, and the default type is “dynamic” if no value is specified.
字串:身型,可以在一個可選的字符串參數在第一個 body元素。可能的類型是“靜態“,“動態”和“運動“,而默認類型是“動態“如果沒有指定值。
density 密度
number: Multiplied by the area of the body’s shape to determine mass. Based on a standard value of 1.0 for water. Lighter materials (such as wood) have a density below 1.0, and heavier materials (such as stone) have a density greater than 1.0. Default value is 1.0.
數值:乘以面積身體的形狀,以確定質量。基於對標準值1.0的水。更輕的材料(如木材)的密度小於 1.0,和重的材料(如石頭)有密度大於1.0。默認值是1.0。
friction
number: May be any non-negative value; a value of 0 means no friction and 1.0 means fairly strong friction. The default value is 0.3.
摩擦力
數值:可以是任何非負價值;值0意味著沒有摩擦,1.0表示比較強烈的摩擦。默認值是0.3。
bounce
number: Determines how much of an object’s velocity is returned after a collision. The default value is 0.2.
彈跳
數值:確定一個物件的速度發生碰撞後返回的值。默認值是0.2,數字越小,彈跳越小
radius半徑
number: Radius of the bounding circle.
數值:圓的半徑邊界。
shape
number: Shape value in the form of a table with the shape vertices, {x1,y1,x2,y2,...,xn,yn}. For example: squareShape = { -20,-10, 20,-10, 20,10, -20,10 }
形狀
數值:形狀值表的形式與形狀頂點,{x1,y1,x2,y2,...,xn,yn}。例如:squareShape={-20,-10,20,-10,20,10,-20,10}
Returns:
None
沒有回傳值
Remarks:
Supported on operating systems and platforms for build numbers shown:
備註:
支持的操作系統和平台,建立數字所示:
Mac OS X:Build 2010.222
Windows: Build 2010.222
iOS: Build 2010.222
Android: Build 2010.222