Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm porting a game that i did for Ludum Dare 26, this year, i made it using Contruct2, i decided to port it to Libgdx, for learning reasons, (check the web version)

My problem is that the Player moves base on the Angle, where the mouse is pointing at, in Contruct2, they have a really nice function called


in libGdx i made this function

public boolean mouseMoved(int screenX, int screenY) {

    Vector2 centerPosition = new Vector2((float) / 2, (float) / 2);
//  Vector2 centerPosition = new Vector2(getX(), getY());

    screenY = - screenY ; //Inverse the Y

        Vector2 mouseLoc = new Vector2(screenX, screenY);

        Vector2 direction = mouseLoc.sub(centerPosition);
        float mouseAngle  = direction.angle();/*(float)(Math. atan2(direction.y, direction.x)); */

        return true;

which works nice, but when i reach the corners on the maps, it give me the wrong angles, because the Player is not centered anymore,

I know the solution is just use "centerPosition" with the player coords, and use "mouseLoc" with the mouse coords but needs to be relative to the tiled map, and that's my problem i don't know how to get those paramenters , screenX and screenY, are the mouse location but base on screen position

im using OrthographicCamera and OrthogonalTiledMapRenderer

i hope can understand my problem, this is the jar i have right now

share|improve this question
up vote 6 down vote accepted

I was able to find a solution, in order to convert the mouse coords to the coords relative to the tile map, i used:

public boolean mouseMoved(int screenX, int screenY) {

    //screenX, screenY - Mouse Coords

    Vector2 centerPosition = new Vector2(player.getX(), player.getY());

    Vector3 worldCoordinates = new Vector3(screenX, y, screenY,0);

    Vector2 mouseLoc = new Vector2(worldCoordinates.x, worldCoordinates.y);

    Vector2 direction = mouseLoc.sub(centerPosition);
    float mouseAngle  = direction.angle();

    return true;

hope this can be useful to other with similiar issue

share|improve this answer
You should probably mark your question as "answered" by accepting your own answer here. – XiaoChuan Yu Aug 14 '13 at 2:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.