Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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:

@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

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

Your Answer

 
discard

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.