Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Scrolling Tiledmap Slick2D  (Read 616 times)
0 Members and 1 Guest are viewing this topic.
Offline tdegroot96

Junior Member


Projects: 1



« Posted 2013-07-09 10:26:28 »

So I just started a new game again, it's gonna be a top-down view with bow and arrow, monsters and quests.
The Camera class is doing really fine.
For the map I'm using tiledmap, where I got in problems.
It renders the map fine, but the problem is: it renders the whole map.
Now there is this method TiledMap.render(x, y, sx, sy, width, height); where sx and sy stands for start x and y.
This method looked really fine until I read that the sx, sy, width and height were in tiledmap blocks. This results having full tiles rendered and not the smooth scrolling idea.
Does someone have any idea how to fix this?

Link to the TiledMap.render#:

http://slick.ninjacave.com/javadoc/org/newdawn/slick/tiled/TiledMap.html#render(int, int, int, int, int, int)

Github link to Level.java:
https://github.com/Desmaster/Nemesis/blob/master/src/nl/tdegroot/games/nemesis/level/Level.java

Thanks in advance!

Offline h3ckboy
« Reply #1 - Posted 2013-07-09 10:37:23 »

There used to be a great example by kevglass on this exact topic, but maybe it was lost when slick was taken over by other developers.

The basic idea is that you use Graphics.translate(float x, float y) to move everything so that the player (or wahtever your camera is following) stays at the center of the screen. The key to this is to find the offset of the player from the center and translate everything by that much.
Offline tdegroot96

Junior Member


Projects: 1



« Reply #2 - Posted 2013-07-09 10:42:01 »

I got that part working already but it's more the part of rendering a section of the map, for performance.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy
« Reply #3 - Posted 2013-07-09 10:57:02 »

You can just check how close your offset is to being a multiple of your tilesize and use that difference to move the map slightly so for example if your xoffset is 100 and your tilesize is 30 then your code would look like
1  
map.render(-20,0,100/30,0,width,height);


or more generally

1  
map.render((xoffset%tilesize)-tilesize,(yoffset%tilesize)-tilesize,xoffset/tilesize,yoffset/tilesize,width,height);


that may not be perfect because I just came up with it on the spot, but it should be the right idea. You might have to add 1 to the width and height, but you'll be able to figure that out.
Offline tdegroot96

Junior Member


Projects: 1



« Reply #4 - Posted 2013-07-09 11:25:14 »

That usual is the way to do this, yes.
But, because all the coordinates besides x, y are in tiles, it will only render full tiles, so that means an ugly scrolling in a block-by-block way.
I wish I could make a video of this..
Offline seismic

Senior Member


Medals: 3
Projects: 1



« Reply #5 - Posted 2013-07-09 12:43:32 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
public void render(SpriteBatch sb, Camera camera, float delta) {
        float camX = camera.position.x;
        float camY = camera.position.y;

        float startX = Math.max(((camX / TILE_SIZE) * camera.zoomFactor), 0);
        float startY = Math.max(((camY / TILE_SIZE) * camera.zoomFactor), 0);

        float endX = Math.min(((camX + Gdx.graphics.getWidth()) * camera.zoomFactor) / TILE_SIZE + 1, SIZE_X);
        float endY = Math.min(((camY + Gdx.graphics.getHeight()) * camera.zoomFactor) / TILE_SIZE + 1, SIZE_Y);

        // map
       for (float x = startX; x < endX; x++)
            for (float y = startY; y < endY; y++) {
                int tx = (int) x;
                int ty = (int) y;
                Float drawX = x * TILE_SIZE - (camX * camera.zoomFactor);
                Float drawY = y * TILE_SIZE - (camY * camera.zoomFactor);
                drawX -= (x % tx) * TILE_SIZE;
                drawY -= (y % ty) * TILE_SIZE;
                if (drawX.isNaN())
                    drawX = 0f;
                if (drawY.isNaN())
                    drawY = 0f;
                if (x >= SIZE_X)
                    continue;
                if (y >= SIZE_Y)
                    continue;

                if (x < 0)
                    continue;
                if (y < 0)
                    continue;

                // renderTile(x,y);


this allows for floating point smooth scrolling of a tile map, nevermind the zoomfactor Smiley
Offline tdegroot96

Junior Member


Projects: 1



« Reply #6 - Posted 2013-07-09 12:45:39 »

Result:

<a href="http://www.youtube.com/v/FhSydhUiRNs?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/FhSydhUiRNs?version=3&amp;hl=en_US&amp;start=</a>
Offline h3ckboy
« Reply #7 - Posted 2013-07-09 12:50:58 »

That usual is the way to do this, yes.
But, because all the coordinates besides x, y are in tiles, it will only render full tiles, so that means an ugly scrolling in a block-by-block way.
I wish I could make a video of this..


Yes, I understand what you mean and the video was very clear. I had thought that the code I posted would fix that because it takes into account the block issue by using the remainders. I can't test anythign becuase I am at work right now. So the code I gave you produced the same results as you had before?
Offline tdegroot96

Junior Member


Projects: 1



« Reply #8 - Posted 2013-07-09 13:34:59 »

Ahh yes I fixed it!
This is the final render way I'm using:
1  
map.render((int) - (camera.getX() % 64) - 64, (int) - (camera.getY() % 64) - 64, (int) camera.getX() / 64, (int) camera.getY() / 64, (1280 / 64) + 3, (720 / 64) + 3);

I divided screen width by tilesize and added 3 to it(smoothed it out) so it only shows the visible area plus a little margin Smiley.
The fps is finally normal again and pretty solid Cheesy.

I want to thank you both for helping me out! Smiley

Offline h3ckboy
« Reply #9 - Posted 2013-07-09 13:48:14 »

unless if I am misunderstanding your code, thats pretty much what I told you to do  persecutioncomplex?

anyways, kudos Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tdegroot96

Junior Member


Projects: 1



« Reply #10 - Posted 2013-07-09 13:49:30 »

That's right, I got it fixed thanks to you :p.
Offline h3ckboy
« Reply #11 - Posted 2013-07-09 14:10:01 »

Oh woops, I thought you said it didnt work. Ignore me I am obviously confused today Wink.
Offline tdegroot96

Junior Member


Projects: 1



« Reply #12 - Posted 2013-07-09 15:58:11 »

It didn't work at that moment, but after checking that code twice it worked :p.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!