How do you calculate a bounding cartesian rectangle inside an isometric rectangle (in ruby)

56 views Asked by At

I'm writing a 2d isometric game engine in ruby. I want the screen to render in isometric (actually dimetric), but I want the viewport to be bounded by a rectangle that has normal orientation (cartesian). I am having trouble figuring out how to calculate the position of the rectangle inside the isometric map (and to an extent, the width and height of it).

Here is a screenshot of the rectangle position/dimensions I want to calculate (in red): enter image description here

If the grid in black is the entire map, I only want the user's viewport to be able to move within the red rectangle (which is why I call it a bounding rectangle). The origin of the map is the top corner, (0, 0).

If I know the cartesian width/height of the map, how do I calculate the width, height, and position of the red rectangle? This is 2-1 isometric/dimetric projection, so each cell/tile is twice as wide as it is tall (in my case, 40px wide, 20px tall).

Currently, this is how I'm calculating the width and height of the red rectangle:

    def calculate_bounding_box
      rows = cartesian_map_width / tile_height
      cols = cartesian_map_height / tile_width

      # Rectangle dimensions
      width = (rows + cols) * tile_width/2
      height = (rows + cols) * tile_height/2
      #position = ?
    end

However I have no clue how to calculate the position of it. I was thinking, since I know the height of the map, would it be possible for me to use the Pythagorean theorem to calculate the origin? The left side of the red rectangle is the hypotenuse of the triangle it forms with the y axis of the grid and the bottom of it. If we call the other sides of the triangle (the non-red parts) a and b, and if a = b, could I do something like:

rectangle_height = 200
a = b
rectangle_height^2 = a^2 + b^2
rectangle_height^2 = 2 * a^2
(rectangle_height^2) / 2 = a^2
a = Sqrt((rectangle_height^2) / 2)

Couldn't I just do Sqrt((200^2) / 2) to get a?

And since I know the height of the map, I could do: map_height - a to get the start of the y axis of the bounding rectangle (and I already know the x axis will be 0).

That's the only thing I can think to do, but it's not working. I'm guessing I'm over-complicating it and there is a simple way to calculate this. Thanks

0

There are 0 answers