0
\$\begingroup\$

I am making a topdown isometric real time game using Monogame.

I know how to get the mouse position for my camera view. How do I get the mouse coordinates relative to the entire scene?


My game's rendering looks isometric, but it's actually simply 2D and the illusion of isometric projection is done with a simple script that converts the cartesian coordinates for the mouse position to isometric ones, which I pass into a simple grid pathfinding algorithm that makes paths from them. This worked well for me when I used Game Maker.

\$\endgroup\$
4
  • \$\begingroup\$ TO BE CLEAR , I just want some advice on how to handle the camera system and such please I post every where but nobody adviced me :/ \$\endgroup\$
    – Warsloop
    Commented Oct 13, 2014 at 18:40
  • \$\begingroup\$ Yea indeed it can help me , but yo don't have a more step to step method on how to achieve this ? \$\endgroup\$
    – Warsloop
    Commented Oct 13, 2014 at 20:19
  • \$\begingroup\$ @Warsloop In what way does the proposed duplicate differ from your question? If they're the same, but the answers still don't satisfy you, ask for clarification. Once you've got enough reputation, you could also start a bounty to attract more answers. \$\endgroup\$
    – Anko
    Commented Oct 14, 2014 at 19:27
  • \$\begingroup\$ @Anko yea you right but I'm a little confused as they are multiple way to achieve what I want and I don't know what to chose :/ \$\endgroup\$
    – Warsloop
    Commented Oct 14, 2014 at 19:43

1 Answer 1

4
\$\begingroup\$

I don't have a specific example for an Isometric camera, but it sounds like you are asking about the basic camera system in general.

The camera I use in my games is somewhat simple and is illustrated in the figure below: Example of camera system from A Running Man

The Camera class contains the following member variables:

Vector2 position;
Vector2 viewport;
Rectangle worldRectangle;

Converting from world coordinates to camera coordinates is only a matter of subtracting the position of the object from the position of the camera, and going from camera coordinates to world coordinates is only addition.

Getting the position of the mouse on the screen in a 2D game is almost identical to getting the position of an object, and all we have to do is get the on-screen mouse coordinates and add them to the position of the camera.

I do this using the following functions (and various overloads for Vector2, int, etc):

Note: The variable position refers to Camera.position, and worldRectangle is defined when the camera is created

/// <summary>
/// Converts a rectangle in Pixel-based World coordinates to 
/// camera view Screen coordinates
/// </summary>
/// <param name="worldRectangle">Rectangle to convert</param>
/// <returns>The new Screen-based Rectangle</returns>
public Rectangle WorldToScreen(Rectangle worldRectangle)
{
    return new Rectangle(
        worldRectangle.Left - (int)(position.X),
        worldRectangle.Top - (int)(position.Y),
        worldRectangle.Width,
        worldRectangle.Height
    );
}

/// <summary>
/// Converts a rectangle in Screen coordinates to World coordinates
/// </summary>
/// <param name="screenRectangle">Rectangle to convert</param>
/// <returns>The new World-based Rectangle</returns>
public Rectangle ScreenToWorld(Rectangle screenRectangle)
{
    return new Rectangle(
        screenRectangle.Left + (int)position.X,
        screenRectangle.Top + (int)position.Y,
        screenRectangle.Width,
        screenRectangle.Height);
}

So in order for you to convert mouse coordinates to world coordinates, all you have to do is have a simple function like the one below, and then pass it to your script:

public Vector2 ScreenToWorld(int x, int y)
{
    return new Vector2(position.X + x, position.Y + y);
}

This can be confirmed mathematically using the image above and Photoshop (assume pixel units). Suppose that the top left of the world is (0, 0) and the Camera's Viewport is defined by the Rectangle(623, 15, 683, 436). If we hover the mouse over the top-left corner of the player, we get the coordinates (364, 276). In order to find the world location of the point, we add the camera location:

x-coordinate = 364 + 623 y-coordinate = 276 + 15

Which gives us the final coordinates: (987, 291)

\$\endgroup\$
8
  • \$\begingroup\$ Wow thank you for your awesome answer dude ! Yea I'm a noob at game dev. So thank you for explaining me the basis of a camera projection ! \$\endgroup\$
    – Warsloop
    Commented Oct 14, 2014 at 5:29
  • \$\begingroup\$ Im so sorry to ask a question again , but just what method should I use , the vector2 ScreenToWorld or the rectangle one ? I'm a little confused ... \$\endgroup\$
    – Warsloop
    Commented Oct 14, 2014 at 19:41
  • \$\begingroup\$ If you are only looking to convert the mouse coordinates, then you should call the Vector2 method, since the mouse coordinates are just a point on the screen. It also depends on the implementation of the Camera class, however. I would suggest that you pick up a book like this one packtpub.com/game-development/… It is very good for starting out game development with XNA \$\endgroup\$ Commented Oct 14, 2014 at 19:44
  • \$\begingroup\$ Ok I will take a look thank you for quick answer ! In fact to be clear yea I need the basis theory on how camera work on xna :) \$\endgroup\$
    – Warsloop
    Commented Oct 14, 2014 at 19:46
  • \$\begingroup\$ In that case you will definitely get good use from that book. XNA does not contain a "Camera" class from the get-go. It is up to the developer to create one. Good luck! \$\endgroup\$ Commented Oct 14, 2014 at 19:49

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