hello everyone !
I'm posting this thread because I don't find any kind of correct solution for my smooth scrolling...
Can you help me a little bit?
First of all, please notify that my main character will stay in center of the screen. This is the map who move under him.
The main character have a absolute position on all the map => (0,0) is the center tile of the map.
I tried to draw map like this :
- Calculate Absolute position in tile => So the center of the screen is the tile (X,Y)
- Then, I try to determine the left up corner of the screen => (X-(HalfOfTheDisplayInTiles()-1/2),Y(HalfOfTheDisplayInTiles()-1/2))
==> BUG HERE I THINK!- After that, I calculate the translation vector (Determined by difference between the Tile on wich is the player and the left corner of this tile.
Algoritm seams to be correct but result is something... laggy and saccaded...
There is the code of the render Method of my map Class :
Code:
//DRAW MAP
public void render(GameContainer container, Graphics g){
tile.render((int)diffT.x,(int)diffT.y,(int)leftUpCorner.x,(int)leftUpCorner.y,(int)(3*offsetTilesX),(int)(3*offsetTilesY));
}
//Called when application Start => set variables for width and height of the screen
public void prepareMap(GameContainer container){
setCenterOfMap(getCenterOfMap());
setOffset(Math.round(container.getWidth()/tile.getTileWidth()),Math.round(container.getHeight()/tile.getTileHeight()));
setOffsetTiles(container.getWidth()/tile.getTileWidth(), container.getHeight()/tile.getTileHeight());
}
//Recalculate offset of the camera
public void centerCameraOn(Object object){
if(object.getClass().toString().contains("Hero")){
Hero hero = (Hero)object;
float x = hero.getAbsoluteX();
float y = hero.getAbsoluteY();
//Position Left up corner (in tiles)
leftUpCorner.set(posT.x-offsetTilesX,posT.y-offsetTilesY);
//Détermine position actuelle en Tiles
posT.set((float)(x+(getCenterOfMap().x+0.5)),(float)(y+(getCenterOfMap().y+0.5)));
diffT.set((((int)posT.x)-posT.x)*tile.getTileWidth(),(((int)posT.y)-posT.y)*tile.getTileHeight());
}
}
Thanks for all help !
