1
\$\begingroup\$

I've been having this problem for a long time and I just can't seem to find the exact problem. I have a game where the map is 480x1600 and my camera has a view of 480x800. I have a button that when pressed, allows the user to place a platform on the map, and since the map is too big to fit on the screen, I made it so the user can move the camera up and down the map by dragging.

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    Vector3 pos = new Vector3(screenX, screenY, 0);
    cam.unproject(pos);
    cam.position.y = pos.y;
    return true;
}

Method that controls when the user tries to put a platform down

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector3 pos = new Vector3(screenX, screenY, 0);
    cam.unproject(pos);
    Gdx.app.log(Game.LOG, "X Coordinate: " + pos.x + " Y Coordinate: " + pos.y);

    if (GameScreen.createPlatform == true) {
        world.setPlatform(new Vector2(pos.x - 0.6f, pos.y - 0.1f), 1);
        GameScreen.createPlatform = false;
        return true;
    }

    ...
}

Where I render the platform sprites

public void render(float delta) { 
    ...
    spriteBatch.setProjectionMatrix(cam.combined);
    spriteBatch.begin();
    for (Sprite platformSprite: world.getPlatformSprites()) {
        platformSprite.draw(spriteBatch);
    }
    spriteBatch.end();
    ... 


}

The game works fine when I don't move the camera. But when I do, the platforms aren't placed where I click, they're always either higher or lower where I actually clicked. Also, the platforms are always placed somewhere where the camera was originally looking before anything has been moved.

I think it's doing this is because for some reason, the coordinates never actually change. So wherever I am on the map, the top of the screen is always 120 and the bottom is always 90. This also causes objects on the map to have different coordinates if I move the camera.

This problem has me completely lost and I would appreciate any help.

\$\endgroup\$
4
  • \$\begingroup\$ You need to reverse the transform on your mouse click from screen coordinates to world coordinates. You can do this by applying the inverse of the world-to-screen transformation matrix which was used for the camera. \$\endgroup\$ Aug 14 '13 at 17:33
  • \$\begingroup\$ @ShotgunNinja Sorry, I'm not sure if I know exactly what you mean. Are you saying that I need to reverse cam.unproject(pos)? \$\endgroup\$
    – Jonathan
    Aug 14 '13 at 19:02
  • \$\begingroup\$ gamedev.stackexchange.com/a/27793/20399 \$\endgroup\$
    – wes
    Aug 14 '13 at 20:18
  • \$\begingroup\$ @wes Sorry, I still don't think I'm getting it. Am I supposed to do cam.project(pos) somewhere in touchDown()? If I am, then the platforms still aren't being placed in the right spot. I just don't see why I would need to go from world to screen coordinates anywhere in my program \$\endgroup\$
    – Jonathan
    Aug 16 '13 at 3:25
3
\$\begingroup\$

cam.unproject(touchPos) will give you the coordinates as they relate to your screen and cam.project(touchPos) will give you the coordinates as they relate to the game world.

gamedev.stackexchange.com/a/27793/20399

so in here:

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector3 pos = new Vector3(screenX, screenY, 0);
    cam.unproject(pos);
    Gdx.app.log(Game.LOG, "X Coordinate: " + pos.x + " Y Coordinate: " + pos.y);

    if (GameScreen.createPlatform == true) {
        world.setPlatform(new Vector2(pos.x - 0.6f, pos.y - 0.1f), 1);
        GameScreen.createPlatform = false;
        return true;
    }

    ...
}

make it like this:

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector3 pos = new Vector3(screenX, screenY, 0);
    cam.project(pos); //changed
    Gdx.app.log(Game.LOG, "world X Coordinate: " + pos.x + " world Y Coordinate: " + pos.y);

    if (GameScreen.createPlatform == true) {
        world.setPlatform(new Vector2(pos.x - 0.6f, pos.y - 0.1f), 1);
        GameScreen.createPlatform = false;
        return true;
    }

    ...
}
\$\endgroup\$
5
  • \$\begingroup\$ I tried that and the platforms aren't in the screen anymore. When I tried putting it down in the middle of the screen the coordinates come out to be "Pos.x: 6120 and Pos.y: 6373". Are they supposed to be that high? \$\endgroup\$
    – Jonathan
    Aug 19 '13 at 17:41
  • \$\begingroup\$ badlogicgames.com/wordpress/?p=1550 answers.unity3d.com/questions/254141/… \$\endgroup\$
    – wes
    Aug 19 '13 at 18:48
  • \$\begingroup\$ did you read the linked answer, also? gamedev.stackexchange.com/a/27793/20399 \$\endgroup\$
    – wes
    Aug 19 '13 at 18:49
  • \$\begingroup\$ Yes, I read all of your links. Either I'm not understanding something, or something's wrong. I've changed the cam.unproject() to project() and I get those large x and y coordinates where the platform is being placed somewhere I can't see. I've tried random things to try getting a correct coordinate, but nothings worked. \$\endgroup\$
    – Jonathan
    Aug 21 '13 at 1:07
  • \$\begingroup\$ I have had an idea ! have you resolved the problem ? \$\endgroup\$
    – Ion Farima
    Jan 13 '14 at 19:24

You must log in to answer this question.

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