Box2D 的 PTM_RATIO

來源:互聯網
上載者:User

原文連結:http://blog.csdn.net/zhangxaochen/article/details/8009508

根據box2d 的協助文檔:

1.7 Units

Box2D works with floating point numbers and tolerances have to be used to make Box2D perform well.These tolerances have been tuned to work well
withmeters-kilogram-second (MKS) units. In particular, Box2D has been tuned to work well with moving objects between 0.1 and 10meters.
So this means objects between soup cans and buses in size should work well. Static objects may be up to 50meters big without too much trouble.

Being a 2D physics engine, it is tempting to use pixels as your units. Unfortunately this will lead to a poor simulation and possibly weird behavior. An object of length 200 pixels would be seen by Box2D as the size of a 45 story
building.

box2d 使用MKS: 米-千克-秒 單位制,因為文檔稱他的浮點運算在這樣的單位下表現最佳。

一般情況下,我們希望將螢幕比如 480*320 映射為10米見方左右,那麼習慣上設定的 像素-米 比率為 32,即
#define PTM_RATIO 32 【PTM: pixel to meter】,那麼我們便將螢幕映射到了 15m*10m 這樣的小世界裡去。

我們可以很快接受這個概念。但是,昨天我突然有一個奇怪的念頭:

box2d 怎麼知道我的 ptm_ratio 多大?或者說,為什麼無論 PTM_RATIO 設為32 還是64, 為什麼那個 groundBox 外邊框總是能充滿螢幕呢?

想了很久才明白,事實上,box2d 從來不知道也不在乎 PTM_RATIO 的大小。 這個物理引擎就想一個瞎了眼的數學高手,一直在算啊算,但是他並不負責圖形的呈現。是我們寫的cocos2d 的代碼調用了openGles去做的繪製的工作。

如果我們設定 #define PTM_RATIO 32 那麼螢幕被映射為 15*10 平米,而呈現——即,把米換為螢幕座標的時候,一般都會做 ‘某座標*PRM_RATIO’ 的工作,意思就是,放大32倍畫在螢幕上。

如果 #define PTM_RATIO 64的話,儘管螢幕被映射為 7.5*5 平米的世界,似乎小了(物理世界確實小了),但是繪製的時候,因為我們對cocos2d 說:“給我放大64倍畫出來”,所以看起來 groundBox 外框仍然充滿螢幕。。。

就是這樣。

原文連結:http://blog.csdn.net/zhangxaochen/article/details/8009508

{{OVER}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.