Jump to content
• Advertisement

Public Group

Map Coordinates

This topic is 2308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I've been puzzling over how to convert X,Y coordinates of a 2D map into an ID and back again. I want to be able to do this so that I can determine which cell on a map grid has been clicked but also display the coordinates to the user as they'd expect them. I intend to use a 10 x 10 grid map to begin with and I know that X is "how many from left" and Y is "how many from top" but I just can't figure out the formula for converting them. Have any of you done this before? Also, what is the technical name for this site of grid-related mathematics?

Share this post

Share on other sites
Advertisement
 //ID to x+y x = ID/width y = ID%width //x+y to ID ID = x+y*width 

This is all you need. width is the width of your map, in that case 10. Edited by The King2

Share this post

Share on other sites

 //ID to x+y x = ID/width 
 Leaves a remainder so I am flooring it but for ID '1' which should be coordinates 0,0 comes out as 0,1 while ID '34' is coming out as 3,4 but should be 3,3. I know I'm doing something wrong but can't see what. I'm testing this in Excel with the following formulas: X = FLOOR([@ID]/10, 1) Y = MOD([@ID], 10) The grid is a 10 x 10 area and I have labelled both axis with 0 through to 10. 
 0 
 Share this post Link to post Share on other sites 
 
 
 Juliean    7097 Juliean GDNet+ 7097 Posted August 22, 2012 (edited) Ok, so if your ID starts at 1, you need to say @ID-1 before applying the formula, and when reversing, you afterwards need to add 1. I'd suggest having "0" as the first ID anyway.. Edited August 22, 2012 by The King2 1 Share this post Link to post Share on other sites Exessuz    141 Exessuz Member 141 Posted August 23, 2012 if you want to make a 10 x 10 map , lets say that each tile is 32 x 32 px i used to make a linear array holding all the tile ids int width = 32; short map[100]; -> 10x10 supposing that you are drawing Y first for(int x =0 ; x < 10; x++){ for(int y =0 ; y < 10; y++){ RENDER() } } int x = click.x / 32; int y = click.y / 32; then the tile is int id = x*width + y; map[id].selected; 2 Share this post Link to post Share on other sites Bacterius    13178 Bacterius Member 13178 Posted August 24, 2012 (edited) [quote name='The King2' timestamp='1345638889' post='4972176'] //ID to x+y x = ID/width Leaves a remainder so I am flooring it but for ID '1' which should be coordinates 0,0 comes out as 0,1 while ID '34' is coming out as 3,4 but should be 3,3. I know I'm doing something wrong but can't see what. I'm testing this in Excel with the following formulas: X = FLOOR([@ID]/10, 1) Y = MOD([@ID], 10) The grid is a 10 x 10 area and I have labelled both axis with 0 through to 10. [/quote] You have to use integer math here, e.g. integer division (always rounds down, so flooring is correct barring floating-point inaccuracies) and integer remainder (= modulo operation). This assumes your array starts at 0 and (0, 0) respectively, so offset as needed. Edited August 24, 2012 by Bacterius 0 Share this post Link to post Share on other sites 
 Sign in to follow this   Followers 0 
 Go To Topic Listing Forum 
 Advertisement 
 Advertisement Are you ready to promote your game? Submit your game for Intel® certification by December 21, 2018 and you could win big!  Click here to learn more. Popular Tags 2D 3D Advice Algorithm C# C++ Concept Custom Design DX11 Feedback GameMaker Gameplay General Graphics Java Javascript Mobile Music OpenGL PC SFX Unity Unreal VR Popular Contributors Week Month Year All Time 1 Septopus 24 2 Rutin 19 3 khawk 18 4 A4L 14 5 Zakwayda 12 Show More Advertisement Popular Now 12 To prototype or not to prototype? By ZombibonezStarted Sunday at 07:43 PM 16 Java Sick of Javascript bugs, going back to Java By blesseddiscipleStarted December 13 26 OpenGL Texture mapping acts weird on some images. By babaliarisStarted December 12 10 Yet another interpolation question By rrr333Started December 12 44 3D Diligent Engine - A Modern Cross-Platform Low-Level Graphics Library By DiligentDevStarted November 13, 2017 Forum Statistics Total Topics 633768 Total Posts 3013736 GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Forum This Topic More options... Find results that contain... All of my search term words Any of my search term words Find results in... Content titles and body Content titles only Home Groups For Beginners Forum Map Coordinates 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Back Beginners Group Beginners Forum Beginners Resources Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community GameDev Challenges Affiliates Topical Workshops Gallery Groups Back For Beginners GameDev Challenges All Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Forums Back All Forums For Beginners Audio Back Music and Sound FX Games Career Development Business Back Games Career Development Production and Management Games Business and Law Game Design Back Game Design and Theory Writing for Games Programming Back Artificial Intelligence Engines and Middleware General and Gameplay Programming Graphics and GPU Programming Math and Physics Networking and Multiplayer Visual Arts Back 2D and 3D Art Critique and Feedback Community Back GameDev Challenges GDNet Lounge GDNet Comments, Suggestions, and Ideas Coding Horrors Your Announcements Hobby Project Classifieds Indie Showcase Affiliates Back NeHe Productions AngelCode Topical Workshops Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Subscriptions Chat Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry. Sign me up!