6.4 Grid to World Coordinates
Converting Grid Indexes to World Coordinates
Unit 6: 2D Arrays and Tile Maps · Lesson 6.4 · about 30 minutes · no coding experience needed
You have a grid of numbers and a world of coordinates. This lesson is the bridge between them, and it contains one swap that trips up nearly everybody.
What you will be able to do
- Convert a row and column into world coordinates
- Convert an actor position back into a grid cell
- Explain why the two orders are reversed
Words you will need
- Conversion
- In plain words Turning a grid position into a world one, or back.
- More precisely Mapping between array indexes and world coordinates.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen A diagram with a grid on the left and a world on the right, and an arrow showing col becoming x and row becoming y.
// Grid says [row][col]. The world wants (x, y). // x is horizontal, and so is the COLUMN. // y is vertical, and so is the ROW. addObject(new Wall(), col, row); // note the SWAPHere is the whole lesson in one line, and it is worth staring at.
The array is `[row][col]`. The world wants `(x, y)`.
x is the horizontal one, and the horizontal index is COL. So x comes from col. y is the vertical one, and the vertical index is ROW. So y comes from row.
The two swap places. `map[row][col]` becomes `addObject(a, col, row)`. Getting this backwards is the transposed map from 6.2 arriving by a different route.
-
Step 2
On your screen A nested loop placing a wall for every 1 in the map, with the swap visible in the addObject call.
for (int row = 0; row < map.length; row++) { for (int col = 0; col < map[row].length; col++) { if (map[row][col] == 1) { addObject(new Wall(), col, row); } } }And there it is, the two orders side by side in the same block.
`map[row][col]` reads the cell. `addObject(..., col, row)` places the actor.
This is the whole tile-map renderer, and the next lesson only adds more tile types.
-
Step 3
On your screen An actor standing on a cell, with its world position being used to index back into the map.
// The other direction: which cell am I standing on? int myRow = getY(); int myCol = getX(); int cell = map[myRow][myCol];Going back the other way, which is how a player checks what it is standing on.
Same swap in reverse: the actor's y is the row, its x is the column.
This works directly because the world uses one cell per grid square. If the cell size were larger you would divide by it first, which is exactly what cell size meant in 3.5.
-
Step 4
On your screen A world constructed to exactly match the dimensions of the map array.
public MazeWorld() { // columns become width, rows become height super(map[0].length, map.length, 32); prepare(); }Size the world FROM the map and the two can never disagree.
The swap appears here too. `super(width, height, ...)` wants width first, and width is the number of COLUMNS. Height is the number of ROWS.
Hardcode 20 by 15 and then edit the map, and you get walls placed outside the world or an empty band down one side. Deriving it means the world is always exactly right.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea `addObject(a, row, col)` is right because row comes first in the array.
What is actually true The world wants x first, and x is the COLUMN. They swap.
Why it matters The most common bug in this unit, and on a square map it renders transposed with no error at all.
Common wrong idea Rows are the width of the world.
What is actually true COLUMNS are the width. Rows are the height.
Why it matters Getting this wrong makes the world the wrong shape for the map it holds.
Common wrong idea The conversion needs complicated math.
What is actually true With one cell per grid square it is just a swap. Cell size only enters if you use pixel coordinates.
Why it matters Students overcomplicate this and introduce bugs that were not there.
Common wrong idea You can hardcode the world size and keep it in step by hand.
What is actually true Derive it from the map, or they drift apart the first time you edit the level.
Why it matters The same argument as `a.length` over a typed number in 5.3.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. A wall belongs at row 3, column 7. Which call places it?
-
2. Which array index does the world x correspond to?
-
3. An actor is at x 4, y 2. Which cell is it standing on?
-
4. A map has 8 rows and 12 columns. How should the world be created?
-
5. Why derive the world size from the map?
Fill in the code
Place a wall in the world for every 1 in the map. Get the coordinate order right when you call addObject.
if (map[row][col] == 1)
{
addObject(new Wall(), , );
}
Stuck? Open a hint for each blank
- Blank 1: The world wants x first, and x is the horizontal one.
- Blank 2: y is the vertical one.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. `map[row][col]` becomes which addObject call?
-
2. The width of a world built from a map is:
-
3. A map renders transposed and there is no error. What is worth checking?
-
4. Why is the conversion just a swap here?
The short version
- x comes from COL. y comes from ROW. They swap.
- `map[row][col]` reads; `addObject(a, col, row)` places.
- World width is `map[0].length`; height is `map.length`.
- Derive the world size from the map so they cannot disagree.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]