Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  LibGDX + TileMap, Problem with collision, tile properties and dimension  (Read 2491 times)
0 Members and 1 Guest are viewing this topic.
Offline Sabomoth

Junior Member


Medals: 1
Exp: 2 years



« Posted 2013-11-20 18:08:45 »

I have a problem with my Tiled Maps that i have loaded into my game following this guide; https://github.com/libgdx/libgdx/wiki/Tile-maps. All is fine and dandy, i have created a collisionsystem by following a youtube tutorial, pick the tile cell right, left, above or below you depending on which direction you are going, and then check the properties of that tile, if its blocked its blocked.

This works, but, its all scewed. The collision doesnt happen where it should on the map but at other positions, like if the map would actualy be larger then the rendered representation.
Here is a picture to illustrate; https://www.dropbox.com/s/k4mxqh2j2h92uo1/faultystuff.png

The player there is standing at position 8, 8(position.x /32, and position.y /32), that position in Tiled would show the player further down if i translate the position. But the actual position on the map is rather at 8, 12. The thing is, i can continue to walk up a bit and run into this blocked tile out in the void when the character is about to run into tile 8, 12. I have the same problem at other parts of the map, but it seems that it counts 4 tiles wrong, the center pillar is blocked about 3 tiles to the left as well, and the lower part there continues left and right a bit.

I have tried adding a scale as the wiki page does to the map renderer but that didnt solve anything since it flipped out and made the map ultra small.

I dont know the causes to problems like this. The map rendering is not different from the wiki, the camera is orthographic set to the width and height of the screen and follows the characters with a few problems. The collision is the following from the youtube tutorial;

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  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
      float oldX = position.x, oldY = position.y;
      boolean collisionX = false, collisionY = false;
     
      // Move in X.
     position.x = (position.x + velocity.x * Gdx.graphics.getDeltaTime() * SPEED);
     
     
      // Check collision in X.
     if(velocity.x < 0){ // going left
        collisionX = collidesLeft();

         }   else if(velocity.x > 0) {// going right
        collisionX = collidesRight();
         
      }
      // React to x collision
     if(collisionX) {
         position.x = oldX;
         velocity.x = 0;
      }
      // Move in Y.
     position.y = (position.y + velocity.y * Gdx.graphics.getDeltaTime() * SPEED);
     
      // Check collision in Y.
     increment = colLayer.getTileHeight();
      increment = getHeight() < increment ? getHeight() / 2 : increment / 2;

      if(velocity.y < 0) {// going down
        collisionY = collidesBottom();
     
      }   else if(velocity.y > 0) {// going up
        collisionY = collidesTop();

      }

      // React to y collision
     if(collisionY) {
         position.y = oldY;
         velocity.y = 0;
      }
   }
   
   
   private boolean isCellBlocked(float x, float y) {
      Cell cell = colLayer.getCell((int) (x / colLayer.getTileWidth()), (int) (y / colLayer.getTileHeight()));
      return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey("blocked");
   }

   public boolean collidesRight() {
      /*for(float step = 0; step <= getHeight(); step += increment)
         if(isCellBlocked(position.x + getWidth(), position.y + step)){
            System.out.println("Right");
            return true;}*/

     
         if(isCellBlocked(position.x + getWidth(), position.y)){
            System.out.println("Right");
            return true;
         }else return false;
   }
/* there are 3 of these omitted due to being the same */


Offline CTucker1327
« Reply #1 - Posted 2013-11-22 02:05:12 »

I've actually ran into a problem similar to this while using LibGDX and Tiled maps, make sure that where you are rendering the map, you're not scaling it. 
Offline Sabomoth

Junior Member


Medals: 1
Exp: 2 years



« Reply #2 - Posted 2013-11-24 21:57:23 »

I have found nothing in my code that suggests any kind of scaling. I followed a LibGDX Tiled tutorial and did what he did, so have no idea where the error occurs. I have also checked the LibGDX wiki and found no sollutions there. When i try the scaling wound there all it does is to mess up the map to 1 x 1cm box, so that doesnt work.
Even so, if it would have been scaled, everything would have been scaled, not just one of the layers?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2013-11-25 18:44:20 »

I have found nothing in my code that suggests any kind of scaling. I followed a LibGDX Tiled tutorial and did what he did, so have no idea where the error occurs. I have also checked the LibGDX wiki and found no sollutions there. When i try the scaling wound there all it does is to mess up the map to 1 x 1cm box, so that doesnt work.
Even so, if it would have been scaled, everything would have been scaled, not just one of the layers?

I think its a camera issue.
If you followed DermetFan teachings, im almost sure he went through *fixing" the camera...

Offline Sabomoth

Junior Member


Medals: 1
Exp: 2 years



« Reply #4 - Posted 2013-11-26 15:22:13 »

Looking at the bitbucket for his project to be sure, i have all camera settings just as him.

We both create a OrthographicCamera, we both instanciate it in the show method and at the same time send it to the maprenderer, we both set the camera position on the player, and we update the camera after that. And if we resize the window we change the viewport.

So there are no differences there.
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #5 - Posted 2013-11-26 16:02:11 »

Well, the collision code is probably right, just show the map rendering / camera setup stuff. The problem is probably hidden there somewhere. I haven't looked at the bitbucket project you mentioned but here is how it looks in mine (stripped down to a minimum):

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  
// somewhere in the main class
public void create() {
        camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
        camera.setToOrtho(false, VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
}

public void render() {
       mapHandler.draw();
       batch.setProjectionMatrix(camera.combined); // this one is important too, as you want the same projection matrix for your        
      //       sprite batch as for the map
      batch.begin();
       // render other stuff
      batch.end();
}


/// other class
public class MapHandler {
    private TiledMap map;
    private TiledMapRenderer mapRenderer;

    public void draw(SpriteBatch sb) {
        mapRenderer.render();
    }

    public void load(String s) {
        map = new TmxMapLoader().load(s);
        mapRenderer = new OrthogonalTiledMapRenderer(map);
        mapRenderer.setView(game.getCamera());
    }
}
Offline Sabomoth

Junior Member


Medals: 1
Exp: 2 years



« Reply #6 - Posted 2013-11-26 16:50:20 »

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  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
public class GameScreen implements Screen {

   TheGame game;
    TiledMap map;
   OrthogonalTiledMapRenderer mapRender;
   OrthographicCamera camera;
   Player player;
   SpriteBatch batch;  
   int height = Gdx.graphics.getHeight(), width = Gdx.graphics.getWidth();
   float scale = 1 / 32f;
   
   public GameScreen (TheGame game) {
      this.game = game;
   }

   public void render(float delta) {
      Gdx.gl.glClearColor(0, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     
      mapRender.setView(camera);
      mapRender.render();
      mapRender.getSpriteBatch().begin();
      mapRender.getSpriteBatch().end();
      batch.begin();
      batch.draw(player.getTexture(), player.position.x, player.position.y);
      batch.end();
      player.update();
      camera.position.set(player.position.x + player.getWidth() / 2, player.position.y + player.getHeight() / 2, 0);
      camera.update();
   }

   public void resize(int width, int height) {
      camera.viewportWidth = width;
      camera.viewportHeight = height;
      camera.update();

   }

   public void show() {
      map = new TmxMapLoader().load("maps/meh.tmx");
      mapRender = new OrthogonalTiledMapRenderer(map);
      camera = new OrthographicCamera(width, height);

      player = new Player(new Vector2(width / 2, height /2), 1, 30, new Texture("img/player2.png"), (TiledMapTileLayer) map.getLayers().get(0));
      Gdx.input.setInputProcessor(player);
      batch = new SpriteBatch();

   }


Thats all my code in the GameScreen. The scale float is a leftover from trying to fix the problem with the wiki article.
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #7 - Posted 2013-11-26 17:56:23 »

did you try the
1  
batch.setProjectionMatrix(camera.combined);

?
Offline Sabomoth

Junior Member


Medals: 1
Exp: 2 years



« Reply #8 - Posted 2013-11-26 20:02:54 »

I had tried that before to no avail. But it seems to fix part of the problem. The map is no longer scaled, but the collision isnt working correctly. Only seem to work on some of the sides of the blocked tiles, but its different on different combinations of placed tiles. Top side is blocked all the time though. Left only when its a single tile, right only single tiles, but also works when there are tiles to the left of it, but not above or below. And bottom side never blocks. :/

Amazing how copy pasting can have such different results.
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #9 - Posted 2013-11-27 07:56:59 »

And you did set your mapRenderer.setView() to camera.combined, too? Cheesy
Sorry, just trying to help but I can't think of anything else now Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #10 - Posted 2013-11-28 16:24:57 »

And you did set your mapRenderer.setView() to camera.combined, too? Cheesy
Sorry, just trying to help but I can't think of anything else now Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
@Override
    public void resize(int width, int height) {
        camera.position.set(player.getX(), player.getY(), 0f);
        camera.viewportWidth = 800;
        camera.viewportHeight = 600;
        stage.setViewport(width, height, true);

       

    }


Try that.

And also, this :
*renderer = mapRender

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
...
 updateCamera();

//ARE YOU USING THIS : ?
           camera.update(true);

            //Clear Screen
           Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            //Render
           //Add Camera to renderer
           renderer.setView(camera);
            renderer.render();
....
Offline Sabomoth

Junior Member


Medals: 1
Exp: 2 years



« Reply #11 - Posted 2013-11-28 22:23:28 »

seismic: The maprenderer does not allow to set views, i have tried that.
Andre: I am not using stages in my game. Should i?
Offline Andre Lopes
« Reply #12 - Posted 2013-11-30 15:04:52 »

seismic: The maprenderer does not allow to set views, i have tried that.
Andre: I am not using stages in my game. Should i?

I will contact dermetFan for you , because i really dont see any issue there.
Offline dermetfan

Senior Member


Medals: 11



« Reply #13 - Posted 2013-11-30 16:23:25 »

Andre: I am not using stages in my game. Should i?
This has nothing to do with your issue. There's no need for a Stage for what you want to do.

And you did set your mapRenderer.setView() to camera.combined, too? Cheesy
The setView method takes an OrthographicCamera, not a Matrix4. It uses the combined matrix of the given camera already.

Sabomoth: You are using two SpriteBatches (batch and the one in the map renderer). A SpriteBatch is a heavy object, so you should always just have one at a time. Also, it makes it more complicated to keep track of their matrices, like you are experiencing. In the code you posted in reply #6, you used the other SpriteBatch that didn't have the combined matrix set. That's why seismic's suggestion in reply #7 fixed at least part of the problem. I suggest you to get rid of batch and use only the one from the map renderer. It's projection matrix gets set by the setView(..) method already. Also, update before you render to avoid a lag of one frame.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public void render(float delta) {
      Gdx.gl.glClearColor(0, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     
      player.update();
      camera.position.set(player.position.x + player.getWidth() / 2, player.position.y + player.getHeight() / 2, 0);
      camera.update();

      mapRender.setView(camera);
      mapRender.render();
      mapRender.getSpriteBatch().begin();
      mapRenderer.getSpriteBatch().draw(player.getTexture(), player.position.x, player.position.y);
      mapRender.getSpriteBatch().end();
   }


Maybe your collision isn't working properly because you don't set increment before checking for the collision on the X axis:
1  
2  
3  
4  
5  
// Move in X.
     position.x = (position.x + velocity.x * Gdx.graphics.getDeltaTime() * SPEED);
     
      // Check collision in X.
     // ...

It should be more along those lines:
1  
2  
3  
4  
5  
6  
7  
8  
      // Move in X.
     position.x = (position.x + velocity.x * Gdx.graphics.getDeltaTime() * SPEED);
     
      // calculate the increment for step in #collidesLeft() and #collidesRight()
     increment = collisionLayer.getTileWidth();
      increment = getWidth() < increment ? getWidth() / 2 : increment / 2;
     
      // Check collision in X.


Also make sure you use the correct coordinates in the collidesLeft, Right, Top and Bottom methods.

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.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (72 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (43 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!