簡體   English   中英

紋理不在Box2d主體上繪制

[英]Textures not drawing over Box2d Body

我試圖在我的游戲世界中創建的Box2d對象上疊加紋理。 但是紋理的坐標是錯誤的。 紋理的x軸和y軸與實際對象在世界上的位置相距甚遠。

這是負責繪制紋理的代碼行:

batch.draw(khumbtexture, bodyKhumb.getPosition().x ,bodyKhumb.getPosition().y );

結果是紋理偏移了(150,150)向量。 我該如何解決?

Box2D使用儀表作為其坐標系。 您的批處理可能在屏幕坐標下運行,或者您定義了其投影矩陣,這可能會導致在嘗試繪制Box2D坐標時產生差異。 您可以張貼一些代碼來設置SpriteBatch嗎?

這是一種方法。 1.設置相機2.設置您的SpriteBatch以使用相機繪制圖像,而不是使用其自己的內部圖像

// setup the camera. In Box2D we operate on a
// meter scale, pixels won't do it. So we use
// an orthographic camera with a viewport of
// 48 meters in width and 32 meters in height.
// We also position the camera so that it
// looks at (0,16) (that's where the middle of the
// screen will be located).
camera = new OrthographicCamera(48, 32);    
camera.position.set(0, 15, 0);

然后在您的渲染方法中

camera.update();
batch.setProjectionMatrix(camera.combined);
//clear screen here
//draw your stuff in Box2D meter coordinates
batch.draw( texture,1,2); 

第一部分參考: http : //www.java2s.com/Open-Source/Android/Game/libgdx/com/badlogic/gdx/tests/box2d/Box2DTest.java.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM