• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
stitchs_login

Pixel to Grid Location

7 posts in this topic

Hello community,

[b]|------------------------------------------------------------SOLVED------------------------------------------------------------|[/b]

[i]Language: C++, Library: Allegro 4, Game: Tic-Tac-Toe[/i]

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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:
[code]

#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;
[/code]
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
@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.
0

Share this post


Link to post
Share on other sites
[quote name='stitchs' timestamp='1350424538' post='4990891']
Can anyone explain really quickly why I needed to subtract as oppose to add.
[/quote]


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.
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0