3
\$\begingroup\$

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,

http://www.ludumdare.com/compo/ludum-dare-26/?action=preview&uid=15150 (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

setAngleToward(Mouse.X,Mouse.Y)

in libGdx i made this function

@Override
public boolean mouseMoved(int screenX, int screenY) {

    Vector2 centerPosition = new Vector2((float)Gdx.graphics.getWidth() / 2, (float)Gdx.graphics.getHeight() / 2);
//  Vector2 centerPosition = new Vector2(getX(), getY());

    screenY = Gdx.graphics.getHeight() - 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)); */
        setRotation(mouseAngle);

        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

https://dl.dropboxusercontent.com/u/2553973/Others/zombiesblocksattack.jar

\$\endgroup\$
6
\$\begingroup\$

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

@Override
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);
    camera.unproject(worldCoordinates);

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

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

    return true;
}

hope this can be useful to other with similiar issue

\$\endgroup\$
  • 1
    \$\begingroup\$ You should probably mark your question as "answered" by accepting your own answer here. \$\endgroup\$ – XiaoChuan Yu Aug 14 '13 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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