I, i have a problem with my tiled map, this is a video about it :
g-0sAHHqAO0
My problem is that there are some artefact in my tiled map, but the image haven’t that error. I have tryed with lot of tileset, but the result is the same. I’ve tryed also to use nearest to filter the texture because i read in lot of post that can resolve the problem, but not is changed.
I think that the proble is the precision with the texture il loaded is too high and the opengl fail, i think that the problem is in the camera resolutio/tile with, height but i’m not able to solve it, can somebody help me?
There is my code :
package com.grevius.Screens;
import net.dermetfan.util.libgdx.box2d.Box2DMapObjectParser;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.QueryCallback;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.ObjectMap;
public class Livello extends InputAdapter implements Screen{
// Variabili globali
OrthographicCamera camera;
World mondo;
Box2DDebugRenderer renderer;
OrthogonalTiledMapRenderer mappa;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mondo.step(1/45f, 5, 1);
mappa.setView(camera);
mappa.render();
FollowPlayer();
renderer.render(mondo, camera.combined);
}
@Override
public void resize(int width, int height) {
float rateo = (float)width/height;
// 4:3
if(rateo>1.2f && rateo<1.4f) {
camera.viewportWidth = 800/10;
camera.viewportHeight = 600/10;
}
// 16:9 if (rateo>1.6f && rateo<1.9f)
else {
camera.viewportWidth = 854/10;
camera.viewportHeight = 480/10;
}
MapProperties prop = mappa.getMap().getProperties();
int mapWidth = prop.get("width", Integer.class);
int mapHeight = prop.get("height", Integer.class);
int tilePixelWidth = prop.get("tilewidth", Integer.class);
int tilePixelHeight = prop.get("tileheight", Integer.class);
camera.position.set(10*mappa.getUnitScale()*tilePixelWidth, 11*mappa.getUnitScale()*tilePixelHeight, 0);
// camera.position.set(mapWidth*tilePixelWidth/2*mappa.getUnitScale(), mapHeight*tilePixelHeight/2*mappa.getUnitScale(), 0);
camera.update();
Gdx.app.log("INFO", "dim : " + width + " " + height);
}
@Override
public void show() {
// Inizzializzo la camera
camera = new OrthographicCamera();
Gdx.input.setInputProcessor(this);
// Inizializzo il mondo
mondo = new World(new Vector2(0, -9.81f), true);
renderer = new Box2DDebugRenderer();
TmxMapLoader.Parameters par = new TmxMapLoader.Parameters();
par.textureMinFilter = TextureFilter.Nearest;
par.textureMagFilter = TextureFilter.Nearest;
TiledMap level = new TmxMapLoader().load("data/prova.tmx", par);
mappa = new OrthogonalTiledMapRenderer(level, 0.20f);
Box2DMapObjectParser parser = new Box2DMapObjectParser(0.20f);
parser.load(mondo, level);
ObjectMap<String, Body> corpi = parser.getBodies();
corpo = corpi.get("Player");
}
@Override
public void hide() {
this.dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
mondo.dispose();
renderer.dispose();
mappa.dispose();
}
private void FollowPlayer() {
float lerp = 0.1f;
Vector3 position = camera.position;
position.x += (corpo.getPosition().x - position.x) * lerp;
position.y += (corpo.getPosition().y - position.y) * lerp;
camera.position.set(position);
camera.update();
}
// Vettore per la posizione del tocco
private Vector3 pos = new Vector3(), tmp = new Vector3();
private Vector2 forza = new Vector2();
private Body corpo;
private boolean toccato = false;
private QueryCallback queryCallBack = new QueryCallback() {
@Override
public boolean reportFixture(Fixture fixture) {
// Controllo se c'è veramente un corpo
if(!fixture.testPoint(pos.x, pos.y))
return false;
corpo = fixture.getBody();
toccato = true;
Gdx.app.log("INFO", "nome : " + fixture.getBody().isAwake());
return false;
}
};
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// Imposto dove è stato dato il tocco nelle unità del mondo
camera.unproject(pos.set(screenX, screenY, 0));
mondo.QueryAABB(queryCallBack, pos.x, pos.y, pos.x, pos.y);
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if(toccato) {
forza.limit(20);
if(!corpo.isAwake())
corpo.setLinearVelocity(forza);
Gdx.app.log("INFO", "nome : " + corpo.getUserData());
}
toccato = false;
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// Se ho toccato il player imposto la forza con cui esso si può lanciare
if(toccato) {
tmp.set(screenX, screenY, 0);
camera.unproject(tmp);
forza.set(pos.x-tmp.x, pos.y-tmp.y);
}
return true;
}
}