0
\$\begingroup\$

I want to draw some sprites in a tiledmap where the tiles are active. I want to draw with the mouse, so how could I compare the position of the mouse, the sprite position and the position of active tiles? I tried to compare: sprite.getX, screenX and cell.getTile (). getProperties (). containsKey ("active") but I can not get the position of the active tiles.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The libgdx camera type OrthographicCamera has an unproject method. Supplying this method with the screen coordinates of the mouse click will give you a position in world space you can then use collision detection with on your tiles.

The following code shows how you might get the mouse coodinate and traslate it into a world space point.

Vector3 clickPos = new Vector3();
clickPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(clickPos);

At the end of this code snippet you can expect clickPos to now relate to a position in world space.

As noted by the API documentation, setting the z value of the input vector will give you a position at a distance along the ray spanning from the near to far plane. In the example, the z value is 0 therefore the returned coordinate will be on the camera's near plane. However, assuming this is orthographic this should be fine.

Getting the cell could then be done using getCell;

TiledMapTileLayer.Cell cell = TiledMapTileLayer.getCell(clickPos.x, clickPos.y);
\$\endgroup\$
1
  • \$\begingroup\$ Thank you for the answer, but it will not allow me to define the position of the tiles I want, for the coordinates of the mouse I used MouseMouve and mouseDragged. \$\endgroup\$
    – Android
    Jan 20, 2015 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .