• Create Account

Pixel to Grid Location

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

#1stitchs  Members

Posted 16 October 2012 - 10:50 AM

Hello community,

|------------------------------------------------------------SOLVED------------------------------------------------------------|

Language: C++, Library: Allegro 4, Game: Tic-Tac-Toe

As the title suggests I am having a small issue with Pixel-to-Grid location. I understand the whole concept whereby to find the Grid Square that you are in you divide the Pixel by the Square-size and store it in an Integer, so that the result rounds down to the Grid coordinate.

My problem is that I recently offset the start corner of my Grid by adding a UI bar at the top of the game screen. My Grid has X and Y coordinates which are 0 and 48 respectively. Due to this, when I click on a square to place a shape, if I go too far near the lower edge of the game window, I receive a 'Vector subscript out of range' error. If I click in the UI bar it counts as a valid placement for one of the top three squares.

I thought I had the solution when I adjusted the Grid2Pixel calculation to take this offset into account, but this produces the same error. I debugged and found that it would find the Grid Square that the mouse clicked in ((0,1) for arguments sake), and then add the offset of 48 making the Grid Square location (0,49) - erroneous.

My question is, how do I take this offset into account, so that if I click in the UI it does not lead to a shape being placed and, if I click on the lower edge of the window, an error is not produced.

I hope I have provided enough information, please feel free to ask if anymore is needed?

Regards,

Stitchs.

Edited by stitchs, 18 October 2012 - 01:03 PM.

#2Álvaro  Members

Posted 16 October 2012 - 12:03 PM

You need to subtract, not add, the offset of 48.

#3stitchs  Members

Posted 16 October 2012 - 01:02 PM

Could you elaborate please? I am at the assumption that: I need to add the start corner of the Grid onto the height of the UI so that:

The UI starts at 0,0 and its height is 0,48. So when I initialise my Grids constructor, it reads ... = new Grid(..., Y = UiHeight). This would mean its 'world-space' (using the term loosely) world be 48 but its local Y-coord is 0. Wouldn't subtracting mean that it would start drawing/logic at an area outside the ScreenTop and just short of the Height?

The issue I have is that, regardless of +/-, I can't use the offset to work out the calculation of shape placement as this would make the Grid location (which is a maximum of 2,2) 2, 50 or 2, -46. These are waaaaay outside the valid range of the GridSquare positions. How do I account for the offset with regards to GridSquares, and not Pixel coordinates?

Regards,

Stitchs.

#4BeerNutts  Members

Posted 16 October 2012 - 02:45 PM

If your upper-left corner of the tic-tac-toe board is at 10,100, and each square is 20x20 pixels, then you can figure out which grid is clicked like this:

#define BOARD_X_OFFSET 10
#define BOARD_Y_OFFSET 100
#define BOARD_GRID_SIZE 20

// Will check if mouse click is within board, and, if so, sets GridX and GridY to proper place
bool CheckGridClicked(int MouseXloc, int MouseYloc, int &GridX, int &GridY)
{
// Check it's in bounds
if (MouseXloc < BOARD_X_OFFSET || MouseXloc > BOARD_X_OFFSET + BOARD_GRID_SIZE*3) {
// clicked too far left or right
return false;
}
if (MouseYloc < BOARD_Y_OFFSET || MouseYloc > BOARD_Y_OFFSET + BOARD_GRID_SIZE*3) {
// clicked too far up or down
return false;
}

// find which grid was clicked
GridX = (MouseXloc - BOARD_X_OFFSET)/BOARD_GRID_SIZE;
GridY = (MouseYloc - BOARD_Y_OFFSET)/BOARD_GRID_SIZE;

return true;


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#5kseh  Members

Posted 16 October 2012 - 03:00 PM

It sounds to me like you're doing the +/- of the 48 at the wrong time. You don't want to change the result after you calculate the tic-tac-toe grid co-ordinates, you want the input of your conversion formula to have the data it needs so that the calculation occurs correctly.

Also, consider it may help you in debugging to output stuff to the screen like the current mouse co-ordinates and the calculated grid location.Maybe it'll help give a feel for what's actually going on.

#6stitchs  Members

Posted 16 October 2012 - 03:55 PM

@BeerNutts: Thank you, I had something very similar, as in I created a 'clickable' area, or so it were. My issue was how to handle the offset, which subtracting seems to work. I did get a grip of why subtracting an offset works and couple of months ago, I have just completely forgot it..

@Kseh: That was my issue indeed, which is why I came here, I quickly learned that the offset needed to be part of the division process and not stapled on at the end.

Can anyone explain really quickly why I needed to subtract as oppose to add. I'm looking through my old notes but it seems I didn't leave myself much of an explanation.

Thanks again,

Stitchs.

#7Álvaro  Members

Posted 16 October 2012 - 07:08 PM

Can anyone explain really quickly why I needed to subtract as oppose to add.

Think about it with an example. Say the top-left corner of your board is at (40, 55) and each square has size 20x20. If I click on (94, 78), what square was just clicked? Make a drawing of the situation, figure out what square the point is in, figure out how you did it, and the formula should become clear.

#8stitchs  Members

Posted 18 October 2012 - 01:02 PM

I absolutely understand it now. It's because without subtracting the offset you get an invalid, out of bounds/off-screen Grid Square. My original method only works when the Grid starts at the origin corner of the Screen area, 0,0. I can't believe it took this long to remember that!

Thank you very much,

Stitchs.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.