0

I am translating in my main class' render. How do I get the mouse position based on the translation?

public void render(GameContainer gc, Graphics g) 
        throws SlickException 
{
    float centerX = 800/2;
    float centerY = 600/2;
    g.translate(centerX, centerY);
    g.translate(-player.playerX, -player.playerY);
    gen.render(g);
    player.render(g);
}

playerX = 800 /2 - sprite.getWidth();
playerY = 600 /2 - sprite.getHeight();

I update the player position on keydown by .2f * delta

Picture to help with explanation

i92.photobucket.com/albums/l22/occ31191/Untitled-4.png

Corey
  • 73
  • 4
  • 13
  • Can you be more specific? The title says _mouse to world_, your question asks for the reverse. – Stefan Hanke Apr 11 '12 at 05:01
  • I can't edit in a picture so I uploaded here http://i92.photobucket.com/albums/l22/occ31191/Untitled-4.png That should help explain better what I am talking about. Sorry for being unclear – Corey Apr 11 '12 at 05:09

2 Answers2

2

World coordinates = camera position + mouse position

Camera position is calculated/explained in my answer to this question: Slick2D and JBox2D. How to draw

Community
  • 1
  • 1
jefflunt
  • 33,527
  • 7
  • 88
  • 126
1

You're making a tile-based game, where each tile seems to have the same size. For this case, you don't need a generalized unprojection.

Imagine the complete map. The viewport shows only a portion of it; somewhere you need to store the (x,y) offets of the viewport into the complete map. Since the mouse coordinates are relative to the viewport, you need to add this offset to the mouse coordinates. Now, you can easily get the tile coordinates by using modulo operations on the shifted mouse coordinates with the tile's width and height.

Effectively, this is a coordinate transformation of window coordinates to tile coordinates.

Stefan Hanke
  • 3,458
  • 2
  • 30
  • 34
  • How exactly do you get the viewport and the offset? – Corey Apr 11 '12 at 17:32
  • Please read the excellent advice by @normalocity given in his epic answer. :) The viewport is implicit ("camera sees"), the offset describes how much the viewport is shifted in the world. – Stefan Hanke Apr 11 '12 at 17:47