Overview 概述


Corona makes it very easy to add physics to your games, even if you've never worked with a physics engine before. While the underlying engine is built around the well-known Box2D, we've taken a radically different design approach that eliminates most of the coding that is traditionally required.
Corona使得它很容易添加到您的物理遊戲,即使你從來沒有同一個物理引擎之前。雖然基礎引擎是建立在著名的Box2D的,我們已經採取了完全不同的設計方法,消除了最傳統的編碼是必需的。


More Information
For more information see the Corona Physics guide.
更多信息
欲了解更多信息,請參閱Corona物理指南。


Physics Engine The Corona Physics API


Corona makes it very easy to add physics to your games, even if you've never worked with a physics engine before. While the underlying engine is built around the well-known Box2D, we've taken a radically different design approach that eliminates most of the coding that is traditionally required.
Corona使得它很容易添加到您的物理遊戲,即使你從來沒有同一個物理引擎之前。雖然基礎引擎是建立在著名的Box2D的,我們已經採取了完全不同的設計方法,消除了最傳統的編碼是必需的。


In addition, we've tied physics seamlessly into mobile development: for example, using our new "gameUI" library, any physics object can now be made multitouch-draggable with one additional line of code.
此外,我們已經綁物理無縫移動開發:例如,用我們的新的“gameUI“庫,任何物理對象現在可以多點,拖動一個額外的一行代碼。


To work with the Corona physics engine, you begin with familiar Corona objects. Corona treats physical body properties as an extension of its graphics objects: any standard display object, including images, vector drawings, or animated sprites, can be “made physical”, and will automatically begin to interact with other objects in the simulation.
若要使用Corona物理引擎,你就開始熟悉的Corona對象。Corona處理的身體屬性,擴展其圖形對象:任何標準的顯示對象,包括圖片,向量圖,或動畫精靈,可以“做物理“,將自動開始與其他對象進行交互的模擬。


Our design goal is to allow users to think primarily in terms of visible game elements wherever possible, as opposed to constructing an invisible physical world and then attaching visible sprites afterwards.
我們的設計目標是讓用戶認為主要是在可見的遊戲元素方面盡可能,而不是構建一個無形的物質世界,然後附加可見精靈之後。


Also, Corona automatically translates between its familiar onscreen units and the internal metric units of the physical simulation. All position values are exposed in pixels, which are converted internally to meters at a default ratio of 30 pixels per meter (this ratio is user-settable with physics.setScale).
此外,Corona之間的自動轉換屏幕上熟悉的單位和公制單位的內部的物理模擬。所有位置值都暴露在像素,這是轉換內部米默認比例每米30像素(這個比例是用戶可設置與 physics.setScale)。


For consistency with the rest of the Corona SDK, all angular values are exposed in degrees, rather than radians, and position (0,0) remains at the upper left-hand corner of the screen, with the y-axis pointing downwards. Shape definitions should declare their points in clockwise order.
為了保持一致性,其餘的Corona SDK中,所有的角值都暴露度,而不是弧度而且位置(0,0)仍然在左上角在屏幕右下角,與 Y軸指向下方。形狀定義應申報點順時針順序。


Physics world setup物理世界格局

This line makes the physics engine features available under the “physics” namespace:
這條線使物理引擎可用功能下的“物理學“命名空間:
-------------
local physics = require "physics"
-------------



physics.start( [noSleep] )

The following functions start, pause, and stop the physics simulation. start either instantiates or resumes the world, and should be called before any of the other functions in this section.
下面的函數開始,暫停和停止的物理模擬。無論是實例啟動或恢復世界,應該叫之前的其他功能在這一節。


Note that stop is treated as a request to destroy the world, so if you merely want to pause the physics engine, you should use physics.pause().
請注意,停止被視為一個請求來毀滅世界,所以如果你僅僅是要暫停的物理引擎,你應該使用physics.pause()。
-------------
physics.start()
 
physics.pause()
physics.stop()
------------
By default, Box2D bodies not involved in a collision will "sleep" after a couple of seconds. This reduces overhead, but in some cases you may not want this behavior. 
默認情況下,Box2D的機構沒有參與碰撞將“睡眠“幾秒鐘後,幾個。這減少了開銷,但在某些情況下,您可能不希望這樣的行為。


For example, the "ShapeTumbler" sample code will not work well if bodies are allowed to sleep, since sleeping bodies do not respond to changes in the direction of gravity.
例如,“ShapeTumbler“示例代碼將無法正常工作,如果身體允許睡覺,因為睡覺機構不回應重力的方向的變化。


You can override this behavior on a given body with body.isSleepingAllowed = false, but you can also override this globally for all bodies in the world by using an optional boolean parameter in start:
您可以覆蓋此行為在給定的機構,body.isSleepingAllowed=false,但你也可以覆蓋這個全域的所有機構在世界上用一個可選的布爾參數開始:
----------------
physics.start( true ) -- prevent all bodies from sleeping  防止所有機構睡覺
 
physics.start( false ) -- default behavior; bodies may sleep  默認行為;機構可以睡覺
-----------------------


physics.setGravity
設置 x,y分量的全球重力載體,單位 m/s2。默認為(0,9.8)的標準來模擬地球引力,向下在Y軸。
-------------
physics.setGravity( 0, 10 )
--------------
physics.getGravity


Returns the x,y components of the global gravity vector, in units of m/s2. This takes advantage of the fact that Lua functions can return multiple values.
返回的x,y分量的全球重力載體,單位 m/s2。這充分利用了這一事實 Lua函數可以返回多個值。
--------------
gx, gy = physics.getGravity()
---------------


Tilt-based gravity傾斜的重力


Using the above setter, and the existing Corona accelerometer API, it is very easy to implement tilt-based dynamic gravity (note that the y-component returned by the accelerometer hardware must be inverted to conform to Corona coordinates):
使用上面的設置器,而現有的Corona加速API,它是很容易實現傾斜的動態重力(請注意,y分量加速度計傳回的硬件必須符合Corona反向坐標):
-----------------
local function onTilt( event )
        physics.setGravity( 10 * event.xGravity, -10 * event.yGravity )
end
 
Runtime:addEventListener( "accelerometer", onTilt )
----------------
See the “ShapeTumbler” sample project for an example of this feature (the accelerometer is not present in the Corona Simulator, so create a device build to see the effect).
參見“ShapeTumbler“示例項目為例,此功能(加速度計不存在Corona中的模擬器,因此創建一個設備構建看到效果)。


physics.setScale

Sets the internal pixels-per-meter ratio that is used in converting between onscreen Corona coordinates and simulated physics coordinates. This should be done only once, before any physical objects are instantiated.
設置內部像素的每米的比例,用於在屏幕上的Corona坐標之間的轉換和模擬物理坐標。這項工作應該只有一次,在任何物理對象被實例化。


Changing this value has no visual consequences, and simply affects the accuracy of the physical model. The Box2D engine is tuned for simulating medium-sized objects between 0.1m and 10m in size, so it works best when the objects in your game are mapped to physical properties that fall roughly within this range.
改變這個值沒有視覺的後果,影響了準確性和簡單的物理模型。該 Box2D的發動機調整為模擬中等大小的物體之間的0.1米和10米的大小,因此它最適合當你的遊戲中的對象映射到物理特性大致屬於這個範圍之內。


The default scaling value is 30, which means that the optimal 0.1m to 10m range corresponds to visible sprites between 3 and 300 pixels in size, which should cover most typical iPhone content. For higher-resolution devices like iPad, Android, or iPhone 4, you may wish to increase this value to 60 or more.
physics.setScale
默認縮放值是30,這意味著最佳0.1米至10米範圍對應於可見精靈3至300像素的大小,它應該包括最典型的iPhone內容。對於高解析度設備如iPad的,Android,或iPhone4,您可能希望增加該值到60以上。
---------------
physics.setScale( 60 )
---------------


You may also want to increase this value if the objects you are simulating are relatively small. As a rough guide, take the width of a sprite in pixels and divide it by its real-world width, if any. For example, a basketball is about 0.25 meters across, so a 20-pixel basketball sprite suggests a physics scaling factor of roughly (20 / 0.25) = 80. However, the resulting effect also depends on how much your chosen density (mass) values approximate the "real world". Therefore, you should ultimately set this scaling factor to whatever feels right within the context of your game: if objects seem too sluggish, and fall too slowly, then they are too big and heavy for your purposes -- try raising the scaling value and/or reducing their densities.
您可能還需要增加這個值,如果你是模擬的對象都比較小。作為一個粗略的指南,採取寬度 Sprite的像素和劃分它在其現實世界的寬度,如果有的話。例如,籃球是約 0.25公尺,所以20像素的籃球精靈表明了物理學的比例因子約為(20/ 0.25)=80。然而,由此產生的效果也取決於您選擇的多少密度(質量)值近似的“現實世界“。因此,你應該最終設置此換算係數的權利在任何情況下感覺你的遊戲:如果對象似乎過於緩慢,和下降速度太慢,那麼他們太又大又重您的目的-嘗試提高定標值和/或降低其密度。


Note that this pixels-per-meter factor is relative to your original content dimensions, if you use the Corona content scaling features to deploy the same code across different screen resolutions (such as iPhone and Android). See the Corona API Reference for more information on autoscaling content for multiple screens.
請注意,此像素每平方米的因素是相對於原始內容的尺寸,如果您使用Corona內容縮放功能相同的代碼部署在不同的屏幕分辨率(如iPhone和Android)。見Corona API參考有關詳細信息,自動縮放多個屏幕內容。


Also, since onscreen objects are not resized when this value is changed, the visible simulation may behave strangely if it is changed while physical objects are already onscreen. In other words, this setting is not the correct way to visibly scale the world. To do that, you should add all the game objects to a common Corona display group, which will cause the simulation to operate according to the group’s internal coordinates rather than the global stage coordinates, and then scale or pan that group. See the “EggBreaker” sample code for a demonstration of this technique.
此外,由於屏幕上的對象不調整大小時,這個值改變時,可能會出現奇怪的可視化仿真,如果它被改變,而物質對象已經在屏幕上。換句話說,此設置是不是正確的方法,能明顯地規模的世界。為了做到這一點,你應該添加所有的遊戲對象到一個共同的Corona顯示組,這將導致模擬操作根據本群組的內部協調,而不是在全球舞台的坐標,然後縮放或平移該組。參見“EggBreaker“示例代碼演示了這種技術。


physics.setDrawMode
Selects one of three possible rendering modes for the physics engine. This mode may be changed at any time -- see the "DebugDraw" sample project for an example of how to toggle it on the fly.
三種可能的選擇之一渲染模式的物理引擎。這種模式可能會隨時改變- 見“DebugDraw“示例項目為例,如何切換它在飛。
----------
physics.setDrawMode( "debug" ) -- shows collision engine outlines only 只顯示碰撞發動機概述
physics.setDrawMode( "hybrid" ) -- overlays collision outlines on normal Corona objects 覆蓋碰撞物體輪廓正常Corona
physics.setDrawMode( "normal" ) -- the default Corona renderer, with no collision outlines 默認Corona渲染,沒有碰撞概述
------------
While this feature will also run on devices, it will probably be most useful in the Corona Simulator, when debugging unexpected physics engine behavior.
這個功能也將在設備上運行,它可能是最有用的Corona中的模擬器,調試時意外的物理引擎的行為。


When working with Corona display groups and Box2D, it is important to keep in mind that Box2D expects all physics objects to share a global coordinate system. This means that a set of ungrouped Corona objects will work well, and a set of objects that are all added to the same display group should also work, since they will share the internal coordinates of that group -- this is demonstrated in the "EggBreaker" sample code, which uses a single moving display group to create a "moving camera" effect.
當使用Corona顯示組和Box2D的,重要的是要記住,Box2D的預期所有對象共享一個物理全局坐標系統。這意味著,一組不分組Corona對象將工作做好,還有一組對象都添加到同一個顯示組也應努力,因為他們將分享該組的內部協調- 這是表現在“EggBreaker “示例代碼,它使用一個單一的移動顯示組創建一個“移動鏡頭“的效果。


However, unexpected results may occur if physical objects are added to different display groups, especially if the groups are then moved in different directions. The "hybrid" draw mode makes these issues much easier to diagnose, since it lets you overlay the physics engine's collision boxes and shapes on top of the Corona renderer.
但是,如果可能會出現意外的結果物理對象被添加到不同的顯示群體,特別是如果組,然後在不同的方向移動。 “混合“畫模式使這些問題更容易診斷,因為它可以讓你覆蓋了物理引擎的碰撞和形狀的盒子上面的Corona渲染。


The physics data is displayed using color-coded vector graphics, which reflect different object types and attributes:
物理數據顯示彩色編碼的矢量圖形,這反映了不同的對象類型和屬性:


Orange: dynamic physics bodies (the default body type)橙色:動態物理機構(默認主體類型)
Dark blue: kinematic physics bodies深藍色:物理機構運動學
Green: static physics bodies, such as the ground or walls綠色:靜態物理機構,如地面或牆壁
Gray: a body that is "sleeping" due to lack of activity 灰色:一個機構,是“睡覺“,由於缺乏活動
Light blue: joints 淺藍色:接頭
NOTE: While the physics debug draw renderer should correctly account for physics objects in nested display groups, it will display misleading results if display groups are rotated or scaled. This will be addressed in a future release.
注意:當物理調試畫渲染應該正確帳戶對物理對象的嵌套顯示組,它會顯示錯誤的結果,如果顯示組的旋轉或縮放。這將是在未來的版本處理。


physics.setPositionIterations
Sets the accuracy of the engine's position calculations.
設置精度發動機的位置計算。
----------------
physics.setPositionIterations( 16 )
-----------------


The default value is 8, which means that the engine will iterate through eight position approximations per frame for each object. Increasing this number will cause fewer momentary innacuracies (overlapping objects. etc.) but will increase computational overhead. The default value should be good for most general cases.
默認值是8,這意味著該引擎將遍歷八個方位逼近每幀為每個對象。增加這個數字將產生更少的瞬間 innacuracies(重疊的對象。等),但會增加計算開銷。默認值應該是很好的最一般的情況。



physics.setVelocityIterations

Sets the accuracy of the engine's velocity calculations.
設置精度發動機的速度計算。
-------------
physics.setVelocityIterations( 6 )
-------------
The default value is 3, which means that the engine will iterate through three velocity approximations per frame for each object. Increasing this number will cause fewer momentary innacuracies (overlapping objects. etc.) but will increase computational overhead. The default value should be good for most general cases.
默認值是3,這意味著該引擎將通過三個迭代逼近每幀的速度為每個對象。增加這個數字將產生更少的瞬間 innacuracies(重疊的對象。等),但會增加計算開銷。默認值應該是很好的最一般的情況。


A note for current Box2D users 當前 Box2D的用戶請注意


Corona incorporates the latest version of the Box2D physics engine (currently v2.1.2) , written by Erin Catto of Blizzard Entertainment.
Corona採用了最新版本的Box2D的物理引擎(目前v2.1.2),書面由Erin卡托暴雪娛樂。


Porting Box2D applications from other platforms should be fairly easy. The Corona API includes most of the core features of Box2D, including:
移植Box2D的應用程序從其他平台應該是相當容易的。電暈 API包括最核心的特徵 Box2D的,其中包括:


Automatic world setup, scaling, and synchronization with visible Corona objects
Basic collision geometry (boxes, circles and arbitrary polygons)
Complex collision geometry (multi-element bodies)
Attributes for primary body and fixture properties
Collision events via Corona event listeners, including collision forces
Primary collision event phases: began and ended
Additional collision event types: preCollision and postCollision, which correspond to Box2D "preSolve" and "postSolve" collision features
Draggable object handling within the simulation
Eight types of joints: distance, pivot, piston, friction, weld, wheel, pulley and touch (some of these are renamed from their Box2D counterparts; see “Joints” section below)
Touch joints (based on Box2D "mouse joints") can be used with multitouch, for simultaneous dragging of 5-10 physics objects; see "DebugDraw" sample project
Joint motors and limits
Applied forces and impulses, both linear and angular
Dynamic gravity, including accelerometer-driven gravity
Sensors and bullets
Collision categories, masking and groups
“DebugDraw” mode, including an exclusive "hybrid" mode showing the debug view as an overlay
Joint and body destructors


全自動世界格局,縮放和同步對象可見電暈
基本碰撞幾何(盒,圓和任意多邊形)
幾何形狀複雜的碰撞(多元素機構)
主要機構的屬性和夾具性能
通過電暈碰撞事件的事件偵聽器,包括碰撞力
碰撞事件的初步階段:開始和結束
額外的碰撞事件類型:preCollision和postCollision,它們對應於 Box2D的“preSolve“和”postSolve“碰撞特性
可拖動對象在模擬處理
八種類型的關節:距離,樞紐,活塞,摩擦,焊接,輪,滑輪和觸摸(其中一些是從他們的Box2D的同行改名,見“關節“一節)
觸摸關節(基於 Box2D的“鼠標接頭“)可用於多點,可同時拖動5-10物理對象;見“DebugDraw“示例項目
聯合電機和限制
作用力和衝動,線性和角
動態重力,包括加速推動的重力
傳感器和子彈
碰撞類別,掩蔽和組
“DebugDraw“模式,包括獨有的“混合“模式顯示了作為一個覆蓋調試視圖
聯合和身體析構


Features not yet supported include:
功能尚不支持包括:
Gear joints
Ray casting, region queries, etc.
Automatic resizing of physics objects when Corona display objects are resized with xScale/yScale attributes (avoid using xScale/yScale on an already-existing physical object in the meantime).
齒輪接頭
光線投射,區域查詢等
自動調整大小的物理對象時顯示對象大小電暈帶 XScale/ yScale屬性(避免使用的Xscale/ yScale在一個已經存在的物理對象在此期間)。
























arrow
arrow
    全站熱搜

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