@@ -45,10 +45,10 @@ public Vec3 getClosestTileFromWorld(@NonNull Vec3 worldPos) {
45
45
}
46
46
47
47
// Translate the index to the grid coordinates
48
- int x = closestIndex % (int ) width ;
49
- int y = closestIndex / (int ) width ;
48
+ int x = closestIndex / (int ) width ;
49
+ int y = closestIndex % (int ) width ;
50
50
51
- return new Vec3 (x , y , 0 );
51
+ return getTile (x , y );
52
52
}
53
53
54
54
/**
@@ -74,7 +74,7 @@ public ArrayList<Vec3> gridToWorld() {
74
74
* @return the world coordinate of the center of the tile
75
75
*/
76
76
public Vec3 getWorldTileCenter (int x , int y ) {
77
- return new Vec3 (x + TILE_SIZE / 2 , tiles [x ][ y ], y + TILE_SIZE / 2 );
77
+ return new Vec3 (x + TILE_SIZE / 2 , tiles [y ][ x ], y + TILE_SIZE / 2 );
78
78
}
79
79
80
80
/**
@@ -92,32 +92,44 @@ public float getTileHeight(int x, int y) {
92
92
* Returns the height of the tile at the given grid coordinates.
93
93
*
94
94
* @param pos the grid coordinates of the tile, not world coordinates!
95
+ * The y coordinate is the height of the tile.
96
+ * The y coordinate is ignored in this method.
97
+ * The z coordinate of the given Vec3 is the y coordinate of the tile in the 2D map grid.
98
+ * The x coordinate of the given Vec3 is the x coordinate of the tile in the 2D map grid.
95
99
* @return the height of the tile
96
100
*/
97
101
public float getTileHeight (Vec3 pos ) {
98
- Vec3 getTile = getTile (pos );
99
- return getTileHeight ((int ) getTile .getX (), (int ) getTile .getY ());
102
+ return getTileHeight ((int ) pos .getX (), (int ) pos .getZ ());
100
103
}
101
104
102
105
/**
103
106
* Returns the tile at the given grid coordinates as a Vec3.
104
107
*
105
108
* @param x the x coordinate of the tile in the map grid (0-indexed)
106
109
* @param y the y coordinate of the tile in the map grid (0-indexed)
107
- * @return the tile as a Vec3 in grid coordinates. the z is always 0, as this is a 2D map .
110
+ * @return the tile as a Vec3 in grid coordinates. the y is the height of the tile .
108
111
*/
109
112
public Vec3 getTile (int x , int y ) {
110
- return new Vec3 (x , y , 0 );
113
+ // Check if the coordinates are valid
114
+ if (x < 0 || x >= width || y < 0 || y >= depth ) {
115
+ throw new IllegalArgumentException ("Invalid grid coordinates: (" + x + ", " + y + ")" );
116
+ }
117
+
118
+ return new Vec3 (x , tiles [y ][x ], y );
111
119
}
112
120
113
121
/**
114
122
* Returns the tile at the given grid coordinates as a Vec3.
115
123
*
116
124
* @param pos the grid coordinates of the tile, not world coordinates!
117
- * @return the tile as a Vec3 in grid coordinates. the z is always 0, as this is a 2D map.
125
+ * The y coordinate is the height of the tile.
126
+ * The y coordinate is ignored in this method.
127
+ * The z coordinate of the given Vec3 is the y coordinate of the tile in the 2D map grid.
128
+ * The x coordinate of the given Vec3 is the x coordinate of the tile in the 2D map grid.
129
+ * @return the tile as a Vec3 in grid coordinates. the y is always 0, as this is a 2D map.
118
130
*/
119
131
public Vec3 getTile (Vec3 pos ) {
120
- return getTile ((int ) pos .getX (), (int ) pos .getY ());
132
+ return getTile ((int ) pos .getX (), (int ) pos .getZ ());
121
133
}
122
134
123
135
@ Override
0 commit comments