libgdx多人游戏 - 如何处理不同的屏幕分辨率

时间:2015-05-11 21:07:19

标签: android libgdx render resolution multiplayer

我正在使用libgdx在Android手机之间制作一个空中曲棍球的多人游戏。我已经能够通过tcp连接连接两部手机,并且他们交换了彼此槌的位置。  这两款手机有不同的分辨率,这会导致图形问题,例如:   - 当在其中一部电话中,与另一部电话中的冰球在同一行中的槌时,槌不在冰球附近。 )http://en.wikipedia.org/wiki/Air_hockey你可以在这里看到我的意思当我说槌和冰球直到阅读它我称之为工具和磁盘)

为了解决这个问题,我需要做些什么,这样不同屏幕尺寸的手机可以毫无困难地进行游戏。 以下是可能有用的其他信息:

游戏物体和墙壁位置的大小已经由屏幕的百分比确定,例如,木槌的半径是屏幕宽度的7%。此外,我通过0 -1之间的浮点数发送两个电话之间的坐标,表示屏幕中的位置,例如中间点(0.5,0.5)。但它无法解决它。

以下是我的渲染功能中与游戏图形相关的内容:

batch = new SpriteBatch();
camera = new OrthographicCamera();
height = Gdx.graphics.getWidth();
width = Gdx.graphics.getHeight();
camera.setToOrtho(false, height, width);

也许更改此功能中的任何参数可能会有所帮助,但我不知道我是否需要在此更改某些内容。

感谢帮助者。

1 个答案:

答案 0 :(得分:0)

好的,这个解决方案根本没有使用camera类,因此在我看来有点不合适,但这是我一年前遇到类似问题时提出来的。

public abstract class BaseScreen implements Screen, InputProcessor
{
    protected int windowWidth;
    protected int windowHeight;

    public BaseScreen() 
    {
        windowWidth = Gdx.graphics.getWidth(); //width of screen
        windowHeight = Gdx.graphics.getHeight(); //height of screen
    }

    @Override
    public void resize(int width, int height)
    {
        windowWidth = width;
        windowHeight = height;
    }

    public void show()
    {
        Gdx.input.setInputProcessor(this);
    }

    public int getWindowWidth()
    {
        return windowWidth;
    }

    public int getWindowHeight()
    {
        return windowHeight;
    }

    public void setWindowWidth(int windowWidth)
    {
        this.windowWidth = windowWidth;
    }

    public void setWindowHeight(int windowHeight)
    {
        this.windowHeight = windowHeight;
    }

    ...

    public abstract void onTouchDown(float pointerX, float pointerY);
    public abstract void onTouchUp(float pointerX, float pointerY);

    @Override
    public final boolean touchDown(int screenX, int screenY, int pointer, int button)
    {
        float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
        float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
        onTouchDown(pointerX, pointerY);
        return false;
    }

    @Override
    public final boolean touchUp(int screenX, int screenY, int pointer, int     button) 
    {
            float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
            float pointerY = InputTransform.getCursorToModelY(windowHeight,     screenY);
            onTouchUp(pointerX, pointerY);
            return false;
    }
}

使用以下内容(其中appWidth是模型宽度,appHeight是模型高度,也就是世界的大小)

public class InputTransform
{
    private static int appWidth = 480;
    private static int appHeight = 320;

    public static float getCursorToModelX(int screenX, int cursorX) 
    {
        return (((float)cursorX) * appWidth) / ((float)screenX); 
    }

    public static float getCursorToModelY(int screenY, int cursorY) 
    {
        return ((float)(screenY - cursorY)) * appHeight / ((float)screenY) ; 
    }
}

然后我为屏幕的视口变换设置了适当的参数,并为[0,480] x [0,320]网格中的对象绘制了投影变换:

    Gdx.gl.glViewport(0,  0,  Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); //init screen to [0,ScreenWidth]x[0,ScreenHeight]
    Resources.batch = new SpriteBatch();
    Resources.normalProjection = new Matrix4().setToOrtho2D(0, 0, 480, 320); //create transform to [0,480]x[0,320] world grid
    Resources.batch.setProjectionMatrix(Resources.normalProjection); //initialize drawing with transform
    Resources.shapeRenderer.setProjectionMatrix(Resources.normalProjection); //initialize drawing with transform

但是,如果 使用OrthographicCamera,那么您只需要将InputTransform替换为camera.unproject(),并将相机视口设置为{{1 }}。考虑到我上面给出的方法有其局限性,即你需要破解它以实际移动相机从[0,480] x [0,320]网格。

根据页面上的文档,

StretchViewport

或者wiki上的一个更完整的例子:

https://github.com/libgdx/libgdx/wiki/Orthographic-camera

最重要的,一个使用带有视口的正交相机的示例:

http://www.gamefromscratch.com/post/2014/12/09/LibGDX-Tutorial-Part-17-Viewports.aspx

private Viewport viewport;
private Camera camera;

public void create() {
    camera = new OrthographicCamera();
    viewport = new StretchViewport(480, 320, camera);
}
相关问题