Results 1 to 9 of 9

Thread: Get X/Y coordinates

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Get X/Y coordinates

    I'm rewriting a 2d tile based map creator program that I wrote a good while back. One of the features that I'm trying to implement is to know where your mouse is in relation to the map. Currently I have a point variable named currentLocation. This represents the tile that is visible at (0, 0) in relation to the map. So for example, if I have this matrix:
    [00, 10, 20, 30, 40, 50]
    [01, 11, 21, 31, 41, 51]
    [02, 12, 22, 32, 42, 52]
    [03, 13, 23, 33, 43, 53]
    [04, 14, 24, 34, 43, 54]

    And I move to the right one:
    [10, 20, 30, 40, 50, 60]
    [11, 21, 31, 41, 51, 61]
    [12, 22, 32, 42, 52, 62]
    [13, 23, 33, 43, 53, 63]
    [14, 24, 34, 44, 53, 64]

    My currentLocation variable after the move would be (1, 0).

    What I've tried is this:
    Code:
        Private Function GetLocation(ByVal e As MouseEventArgs) As Point
            'Get the size of the tiles there are in the visible area of the screen
            'We do this by dividing the width or the height by the size of the tile's width or height
            Dim tilesPerHeight As Double = Math.Floor(Me.Height + ToolStrip1.Bottom / 32)
            Dim tilesPerWidth As Double = Math.Floor(Me.Width / 32)
    
            'Get the current location of the mouse
            Dim mouseLocation As Point = e.Location
    
            'Get the tiles the mouse is currently on
            'Do this by dividing the mouse location by the height or width of the tiles
            Dim tempLocation As Point = New Point(CInt(Math.Ceiling(mouseLocation.X / tilesPerWidth)), CInt(Math.Ceiling(mouseLocation.Y / tilesPerHeight)))
    
            Return tempLocation
        End Function
    The reason I divide everything by 32 is because the tile size is 32x32. But what I accidentally wound up doing is just dividing the map into 32 pieces and making a redundant mess. How would I do what I'm wanting to do?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: Get X/Y coordinates

    I don't have time right now to look into this in depth, but after a quick glance, instead of dividing everything by 32, have you tried dividing the height by the number of rows (5) and the width by the number of columns (6)?

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Get X/Y coordinates

    Well there is no set number of rows and columns. I just used 5x6 as an example. That's why I'm dividing the width/height by 32 to get the number of rows and columns.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: Get X/Y coordinates

    Ahhh... I get what you mean now. I can see a couple of potential problems:
    1. What if the height and width are not evenly divisible by 32?
    2. What if the mouse is not over the form?
    3. e.Location... is this location relative to the origin of the form or the screen?
    4. What are you trying to return? The on the screen, or a specific tile in your mapping system?

  5. #5

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Get X/Y coordinates

    1. I'll use Math.Ceiling because if it isn't evenly divisible by 32 then part of the decimal will be showing.
    2 & 3. e.Location is in relation to the form, so the calculation will only happen when the mouse is over the form
    4. The specific tile in my map
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: Get X/Y coordinates

    If the form is not divisible by 32, do you show a partial tile? If not, what do those pixels on the outside of outer most tiles of the map represent? I think you're being sloppy in how you handle this particular contingency, as I think Ceiling() is the wrong tool for this. I'd use Floor() instead of ceiling and find some graceful method for handling the possibility that the screen is not divisible by the tile size.

    Also, if you want the specific tile in your map, then you should probably add your x & y to currentLocation.x & y, respectively (ignore this if you are already doing this step in another function, of course).

  7. #7

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Get X/Y coordinates

    Yeah, I show a partial tile. This is why I'm using Math.Ceiling, take for example a form where it's visible area is 300x300(the area that is not obscured by any menu/toolstrips, etc.). I would show 9 full tiles and one extra tile that only %37.5 of it is being shown.

    When I get back to my computer with the program on it I'll try some things out and post them back here.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: Get X/Y coordinates

    OK, but do you have a limit to the map. IE, I assume there's a top and left to the map, since you never mention a (-1,-1) coordinate, but is there a right or bottom tile, or does it go on forever? If there is an edge, then you need to handle that gracefully somehow.

    Also, I think I've changed my mind about floor/ceiling. However, I think you need to take (Ceiling() - 1), not just (Ceiling()), since you want the top left tile to be (0,0). Anyway, I'm out for the night. Good luck.

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Get X/Y coordinates

    What you're trying to do and what's not working aren't terribly clear to me, but are you sure you wanted

    Code:
    Dim tilesPerHeight As Double = Math.Floor(Me.Height + ToolStrip1.Bottom / 32)
    instead of

    Code:
    Dim tilesPerHeight As Double = Math.Floor((Me.Height + ToolStrip1.Bottom) / 32)
    (or something along those lines)? It's very weird to me that you're dividing Me.Width by 32 but not Me.Height. The variable tilesPerHeight is really measuring height per tile, so it's very confusingly named. You also aren't using currentLocation, which as far as I can tell doesn't make sense. tempLocation (also confusingly named) represents the 1-indexed (row, column) numbers for the grid of tiles

    [11, 21, 31, 41, 51, 61]
    [12, 22, 32, 42, 52, 62]
    [13, 23, 33, 43, 53, 63]
    [14, 24, 34, 44, 54, 64]
    [15, 25, 35, 45, 55, 65]

    (where this matrix represents the return values of GetLocation as the mouse moves around the form, if we divide the form into a grid with 5 rows and 6 columns and tweak the code by making the change above, assuming ToolStip.Bottom = 0 and the 32's are replaced by 5 and 6). The Ceiling call makes this 1-indexed; Floor would make it 0-indexed. What makes much more sense to me is using Floor and adding (currentLocation.X, currentLocation.Y) to the result: that would give the logical location of the tile the mouse is hovering over. This does not handle partial tiles at all. Oh, I should also mention your indexing scheme is inconsistent--in the images you've drawn, you've written (column, row), where in your code, you've used (row, column).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width