android - libgdx multiplayer - how to handle different screen resolutions -


i'm making multiplayer game of air-hockey between android mobiles libgdx. able connect 2 phones tcp connection , exchange locations of each other mallet . 2 phones have different resolutions , causes graphic problems example : - when in 1 of phones mallet in same line puck in other phone mallet not near puck . ) http://en.wikipedia.org/wiki/air_hockey can see here mean when mallet , puck until reading called tool , disk)

what need in order solve different screen sized phones can play without trouble 1 against . here additional info may :

the sizes of game objects , wall locations determined percents of screen example radius of mallet 7 percent of screen's width. send coordinates between 2 phones float numbers between 0 -1 represent in screen example middle point (0.5 , 0.5). don't solves .

here things relevant game graphics in render function :

batch = new spritebatch(); camera = new orthographiccamera(); height = gdx.graphics.getwidth(); width = gdx.graphics.getheight(); camera.settoortho(false, height, width); 

maybe changing parameters in functions may have no idea if need change here .

thanks helpers.

okay, solution did not use camera class @ all, , kinda subpar in opinion, came year ago when had similar problem.

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;     } } 

which uses following (where appwidth model width, , appheight model height, aka size of world)

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) ;      } } 

and set proper parameters screen's viewport transform, , projection transform draws objects within [0,480]x[0,320] grid:

    gdx.gl.glviewport(0,  0,  gdx.graphics.getwidth(), gdx.graphics.getheight()); //init screen [0,screenwidth]x[0,screenheight]     resources.batch = new spritebatch();     resources.normalprojection = new matrix4().settoortho2d(0, 0, 480, 320); //create transform [0,480]x[0,320] world grid     resources.batch.setprojectionmatrix(resources.normalprojection); //initialize drawing transform     resources.shaperenderer.setprojectionmatrix(resources.normalprojection); //initialize drawing transform 

but if are using orthographiccamera, need replace inputtransform camera.unproject(), , set camera viewport stretchviewport. considering approach gave above has limitation, need hack around move camera [0,480]x[0,320] grid.

so per documentation on page,

private viewport viewport; private camera camera;  public void create() {     camera = new orthographiccamera();     viewport = new stretchviewport(480, 320, camera); } 

or more complete example on wiki:

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

or most importantly, example using orthographic camera viewports:

http://www.gamefromscratch.com/post/2014/12/09/libgdx-tutorial-part-17-viewports.aspx

//copy paste gamefromscratch: package com.gamefromscratch;  import com.badlogic.gdx.applicationadapter; import com.badlogic.gdx.gdx; import com.badlogic.gdx.inputprocessor; import com.badlogic.gdx.graphics.gl20; import com.badlogic.gdx.graphics.orthographiccamera; import com.badlogic.gdx.graphics.texture; import com.badlogic.gdx.graphics.g2d.sprite; import com.badlogic.gdx.graphics.g2d.spritebatch; import com.badlogic.gdx.math.vector3; import com.badlogic.gdx.utils.viewport.stretchviewport; import com.badlogic.gdx.utils.viewport.viewport;  public class mouseproject extends applicationadapter implements inputprocessor {    spritebatch batch;    sprite aspectratios;    orthographiccamera camera; //the camera     viewport viewport; //the viewport     @override    public void create () {       batch = new spritebatch();       aspectratios = new sprite(new texture(gdx.files.internal("aspect.jpg")));       aspectratios.setposition(0,0);       aspectratios.setsize(100,100);        camera = new orthographiccamera();       viewport = new stretchviewport(100,100,camera); //stretch screen [0,100]x[0,100] grid       viewport.apply();        camera.position.set(camera.viewportwidth/2,camera.viewportheight/2,0); //set camera @ center of viewport       gdx.input.setinputprocessor(this);    }     @override    public void render () {        camera.update();       gdx.gl.glclearcolor(1, 0, 0, 1);       gdx.gl.glclear(gl20.gl_color_buffer_bit);        batch.setprojectionmatrix(camera.combined); //make batch draw location defined camera       batch.begin();       aspectratios.draw(batch);       batch.end();    }     @override    public void dispose(){       aspectratios.gettexture().dispose();    }     @override    public void resize(int width, int height){       viewport.update(width, height);       camera.position.set(camera.viewportwidth / 2, camera.viewportheight / 2, 0);    }     @override    public boolean keydown(int keycode) {       return false;    }     @override    public boolean keyup(int keycode) {       return false;    }     @override    public boolean keytyped(char character) {       return false;    }     @override    public boolean touchdown(int screenx, int screeny, int pointer, int button) {        gdx.app.log("mouse event","click @ " + screenx + "," + screeny);       vector3 worldcoordinates = camera.unproject(new vector3(screenx,screeny,0)); //obtain touch in world coordinates: similar inputtransform used above       gdx.app.log("mouse event","projected @ " + worldcoordinates.x + "," + worldcoordinates.y);       return false;    }     @override    public boolean touchup(int screenx, int screeny, int pointer, int button) {       return false;    }     @override    public boolean touchdragged(int screenx, int screeny, int pointer) {       return false;    }     @override    public boolean mousemoved(int screenx, int screeny) {       return false;    }     @override    public boolean scrolled(int amount) {       return false;    } } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -