Slick Forums

Discuss the Slick 2D Library
It is currently Wed May 22, 2013 11:59 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Smooth Scrolling TileMap
PostPosted: Mon Nov 28, 2011 7:46 pm 
Offline

Joined: Tue Apr 05, 2011 4:56 pm
Posts: 12
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? :oops:
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 ! :)


Top
 Profile  
 
PostPosted: Tue Nov 29, 2011 6:13 am 
Offline
Slick2D Developer

Joined: Thu Mar 31, 2011 5:06 am
Posts: 239
This might be the bug, its kinda hard to read your code.

[quote]X-(HalfOfTheDisplayInTiles()-1/2)[/qoute]

This is taking 1/2, instead of taking 1 and then dividing by 2. Correct code is -

Code:
X-((HalfOfTheDisplayInTiles()-1) / 2)


Um by scrolling, what do you mean. It is evident that you can be helped better if the problem/task is explained, maybe if there is a better approach.

_________________
Liam (liamzebedee) Edwards-Playne
Cryptum


Top
 Profile  
 
PostPosted: Tue Nov 29, 2011 4:36 pm 
Offline

Joined: Tue Apr 05, 2011 4:56 pm
Posts: 12
Hi,

I'll try to explain me a little bit better. Imagine that you see your character from the top. The character doesn't move from the center, but when you press the keys movement, the absolute position of the hero on the map change and so the map behind him move also. This movement is accorded by a vector returned by my Hero class.

Hope you understand :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group