pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Libgdx Box2D 像素到米的轉換?

Libgdx Box2D pixel to meter conversion?(Libgdx Box2D 像素到米的轉換?)
本文介紹了Libgdx Box2D 像素到米的轉換?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在嘗試使用 Box2D 編寫游戲時,我遇到了 Box2D 的問題.我為紋理和精靈的長度填充了像素數,以在其周圍創建一個框.一切都在正確的地方,但出于某種原因,一切都進展得很慢.通過查看互聯網,我發現如果您不將像素轉換為米,box2d 可能會將形狀處理為非常大的對象.這似乎是一切進展緩慢的合乎邏輯的原因.

When trying to program a game using Box2D, I ran into a problem with Box2D. I filled in pixel numbers for the lengths of the the textures and sprites to create a box around it. Everything was at the right place, but for some reason everything went very slowly. By looking on the internet I found out that if you didn't convert pixels to meters box2d might handle shapes as very large objects. this seemed to be a logical cause of everything moving slowly.

我在這個網站上發現了類似的問題,但答案似乎并沒有幫助.在大多數情況下,解決方案是制定使用比例因子將像素數轉換為米的方法.我試過了,但是所有東西都放錯了位置并且尺寸不對.這對我來說似乎是合乎邏輯的,因為數字發生了變化但具有相同的含義.

I found similar questions on this site, but the answers didn't really seem to help out. in most of the cases the solution was to make methods to convert the pixel numbers to meters using a scaling factor. I tried this out, but everything got misplaced and had wrong sizes. this seemed logical to me since the numbers where changed but had the same meaning.

我想知道是否有辦法使像素意味著更少的米,所以所有東西都應該在同一個地方以相同的(像素)大小,但意味著更少的米.如果您有其他您認為可能會有所幫助的方式,我也很想聽聽.

I was wondering if there is a way to make the pixels mean less meters, so everything whould be at the same place with the same (pixel) size, but mean less meters. If you have a different way which you think might help, I whould also like to hear it..

這是我用來創建相機的代碼

Here is the code i use to create the camera

        width = Gdx.graphics.getWidth() / 5;
        height = Gdx.graphics.getHeight() / 5;

        camera = new OrthographicCamera(width, height);
        camera.setToOrtho(false, 1628, 440);
        camera.update();

這是我用來創建對象的方法:

This is the method I use to create an object:

public void Create(float X, float Y, float Width, float Height, float density, float  friction, float restitution, World world){ 
    //Method to create an item
    width = Width;
    height = Height;

    polygonDef = new BodyDef();
    polygonDef.type = BodyType.DynamicBody;
    polygonDef.position.set(X + (Width / 2f), Y + (Height / 2f));

    polygonBody = world.createBody(polygonDef);

    polygonShape = new PolygonShape();
    polygonShape.setAsBox(Width / 2f, Height / 2f);

    polygonFixture = new FixtureDef();
    polygonFixture.shape = polygonShape;
    polygonFixture.density = density;
    polygonFixture.friction = friction;
    polygonFixture.restitution = restitution;

    polygonBody.createFixture(polygonFixture);
}

要創建一個項目,在本例中是一個表格,我使用以下內容:

To create an item, in this case a table, I use the following:

    Table = new Item();
    Table.Create(372f, 60f, 152f, 96f, 1.0f, 0.2f, 0.2f, world);

使用以下方法在項目上繪制 Sprite:

The Sprites are drawn on the item by using the following method:

public void drawSprite(Sprite sprite){
    polygonBody.setUserData(sprite);
    Utils.batch.begin();
    if(polygonBody.getUserData() instanceof Sprite){
        Sprite Sprite = (Sprite) polygonBody.getUserData();
        Sprite.setPosition(polygonBody.getPosition().x - Sprite.getWidth() / 2, polygonBody.getPosition().y - Sprite.getHeight() / 2);
        Sprite.setRotation(polygonBody.getAngle() * MathUtils.radiansToDegrees);
        Sprite.draw(Utils.batch);
    }
    Utils.batch.end();
}

精靈也有像素大小.

使用這種方法,它會在正確的位置顯示圖像,但一切都移動得很慢.我想知道如何或是否必須更改它以使對象正確移動和/或減少.提前致謝.

推薦答案

Box2D 完全獨立于您使用的圖形庫.它沒有任何精靈和紋理的概念.您在線閱讀的內容是正確的,您必須將像素轉換為米,因為 Box2D 使用米(距離的標準單位).

Box2D is an entirely independent of the graphics library that you use. It doesn't have any notion of sprites and textures. What you read online is correct, you'll have to convert pixels to metres, as Box2D works with metres(the standard unit for distance).

例如,如果您繪制了一個大小為 100x100 像素的精靈,那么這就是您希望用戶在屏幕上看到的精靈的大小.在現實世界中,對象的大小應該以米為單位,而不是以像素為單位——所以如果你說 1px = 1m,那么這會將精靈映射到一個巨大的 100x100 米對象.在 Box2D 中,大型世界對象會減慢計算速度.因此,您需要做的是將 100 像素映射到較小的米數,例如 1 米 - 因此 100x100px 的精靈將在 Box2D 世界中由 1x1 米對象表示.

For example, if you drew a sprite of size 100x100 pixels, that's the size of the sprite that you want the user to see on the screen. In real world the size of the object should be in metres and not in pixels - so if you say 1px = 1m, then that'll map the sprite to a gigantic 100x100 meter object. In Box2D, large world objects will slow down calculations. So what you need to do is map the 100 pixels to a smaller number of meters, say, 1 meter - thus 100x100px sprite will be represented in Box2D world by a 1x1 meter object.

Box2D 不適用于非常小的數字和非常大的數字.因此,請將其保持在 0.5 到 100 之間,以獲得良好的性能.

Box2D doesn't work well with very small numbers and very large numbers. So keep it in between, say between 0.5 and 100, to have good performance.

行.現在我明白你的問題了.不要編碼到像素.就這么簡單.我知道這需要一些時間才能理解(這對我來說).但是一旦你掌握了它的竅門,它就直截了當.而不是像素,使用一個單位,比如說,你稱之為米.所以我們決定我們的視口應該是 6mx5m.

Ok. Now I get your question. Don't code to pixels. Its as simple as that. I know it'll take some time to understand this(it took for me). But once you get the hang of it, its straight forward. Instead of pixels, use a unit, say, you call it meter. So we decide our viewport should be say 6mx5m.

所以初始化是

Constants.VIEWPORT_WIDTH = 6;
Constants.VIEWPORT_HEIGHT = 5;
...

void init() {
    camera = new OrthographicCamera(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
    camera.position.set(Constants.VIEWPORT_WIDTH/2, Constants.VIEWPORT_HEIGHT/2, 0);
    camera.update();
}

一旦知道了實際的寬度和高度,就可以調用以下函數來保持縱橫比:

Once you know the actual width and height, you call the following function in order to maintain aspect ratio:

public void resize(int width, int height) {
        camera.viewportHeight = (Constants.VIEWPORT_WIDTH / width) * height;
        camera.update();
}

resize() 可以在您更改屏幕大小的任何時候調用(例如:當您更改屏幕方向時).resize() 取實際的寬度和高度(320x480 等),即像素值.

resize() can be called anytime you change your screen size(eg: when you screen orientation changes). resize() takes the actual width and height (320x480 etc), which is the pixel value.

現在您可以在這個 6x5 大小的新世界中指定精靈的大小、位置等.你可以忘記像素.填充屏幕的精靈的最小尺寸為 6x5.

Now you specify you sprite sizes, their positions etc. in this new world of size 6x5. You can forget pixels. The minimum size of the sprite that'll fill the screen will be 6x5.

您現在可以使用與 Box2D 相同的單位.由于新的尺寸會更小,這對 Box2D 來說不是問題.如果我沒記錯的話,Box2D 沒有任何單位.為方便起見,我們將其稱為儀表.

You can now use the same unit with Box2D. Since the new dimensions will be smaller, it won't be a problem for Box2D. If I remember correctly Box2D doesn't have any unit. We just call it meter for convenience sake.

現在您可能會問在哪里指定窗口的尺寸.這取決于平臺.以下代碼顯示了一個 320x480 窗口桌面游戲:

Now you might ask where you specify the dimensions of the window. It depends on the platform. Following code shows a 320x480 windowed desktop game:

public class Main {
    public static void main(String[] args) {
        LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
        cfg.title = "my-game";
        cfg.useGL20 = false;
        cfg.width = 480;
        cfg.height = 320;

        new LwjglApplication(new MyGame(), cfg);
    }
}

我們的相機會智能地將 6x5 視口映射到 480x320.

Our camera will intelligently map the 6x5 viewport to 480x320.

這篇關于Libgdx Box2D 像素到米的轉換?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 泰州物流公司_泰州货运公司_泰州物流专线-东鑫物流公司 | 餐饮加盟网_特色餐饮加盟店_餐饮连锁店加盟 | 气力输送设备_料封泵_仓泵_散装机_气化板_压力释放阀-河南锐驰机械设备有限公司 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 石牌坊价格石牌坊雕刻制作_石雕牌坊牌楼石栏杆厂家_山东嘉祥石雕有限公司 | LZ-373测厚仪-华瑞VOC气体检测仪-个人有毒气体检测仪-厂家-深圳市深博瑞仪器仪表有限公司 | 5L旋转蒸发器-20L-50L旋转蒸发器-上海越众仪器设备有限公司 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 浙江皓格药业有限公司| 国际学校_国际学校哪个好_国际课程学校-国际学校择校网 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 货车视频监控,油管家,货车油管家-淄博世纪锐行电子科技 | SOUNDWELL 编码器|电位器|旋转编码器|可调电位器|编码开关厂家-广东升威电子制品有限公司 | HV全空气系统_杭州暖通公司—杭州斯培尔冷暖设备有限公司 | 恒湿机_除湿加湿一体机_恒湿净化消毒一体机厂家-杭州英腾电器有限公司 | 叉车电池-叉车电瓶-叉车蓄电池-铅酸蓄电池-电动叉车蓄电池生产厂家 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 辊道窑炉,辊道窑炉厂家-山东艾希尔 | 电池挤压试验机-自行车喷淋-车辆碾压试验装置-深圳德迈盛测控设备有限公司 | 上海租车公司_上海包车_奔驰租赁_上海商务租车_上海谐焕租车 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 塑料瓶罐_食品塑料瓶_保健品塑料瓶_调味品塑料瓶–东莞市富慷塑料制品有限公司 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | 网站建设-临朐爱采购-抖音运营-山东兆通网络科技 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | 扬尘在线监测系统_工地噪声扬尘检测仪_扬尘监测系统_贝塔射线扬尘监测设备「风途物联网科技」 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | elisa试剂盒-PCR试剂盒「上海谷研实业有限公司」 | 河南生物显微镜,全自动冰冻切片机-河南荣程联合科技有限公司 | 复合肥,化肥厂,复合肥批发,化肥代理,复合肥品牌-红四方 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 半自动预灌装机,卡式瓶灌装机,注射器灌装机,给药器灌装机,大输液灌装机,西林瓶灌装机-长沙一星制药机械有限公司 | 噪声治理公司-噪音治理专业隔音降噪公司 | 回转炉,外热式回转窑,回转窑炉-淄博圣元窑炉工程有限公司 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 |