【问题标题】:Textures not drawing over Box2d Body没有在 Box2d Body 上绘制的纹理
【发布时间】:2012-03-10 07:36:47
【问题描述】:

我正在尝试在我在游戏世界中创建的 Box2d 对象上叠加纹理。但是纹理的坐标是错误的。纹理的 x 轴和 y 轴与世界上的实际对象位置相距甚远。

这是负责绘制纹理的代码行:

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

结果是纹理偏移了 (150,150) 的向量。我该如何解决这个问题?

【问题讨论】:

    标签: java android box2d libgdx


    【解决方案1】:

    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

    【讨论】:

    • 是的...试过了!...老实说,我现在放弃了 libgdx...只是不适合我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 2011-04-20
    • 2018-08-19
    • 2016-05-10
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多