Browse Source

Fix displacement for centered tilemaps

- Fixes tile and collision placement for tilemaps with
  `tile_origin` set to `TILE_ORIGIN_CENTER`.
- Also fixes a bug in the offset computation for collision shapes
  with `flip_v` flag set to true if `tile_origin` is
  `TILE_ORIGIN_BOTTOM_LEFT`.
Matthias Hoelzl 7 years ago
parent
commit
2beadf7ac0
2 changed files with 19 additions and 18 deletions
  1. 6 7
      editor/plugins/tile_map_editor_plugin.cpp
  2. 13 11
      scene/2d/tile_map.cpp

+ 6 - 7
editor/plugins/tile_map_editor_plugin.cpp

@@ -565,20 +565,19 @@ void TileMapEditor::_draw_cell(int p_cell, const Point2i &p_point, bool p_flip_h
 		}
 		}
 
 
 	} else if (node->get_tile_origin() == TileMap::TILE_ORIGIN_CENTER) {
 	} else if (node->get_tile_origin() == TileMap::TILE_ORIGIN_CENTER) {
-		rect.position += node->get_cell_size() / 2;
-		Vector2 s = r.size;
+		Size2 cell_size = node->get_cell_size();
 
 
-		Vector2 center = (s / 2) - tile_ofs;
+		rect.position += tile_ofs;
 
 
 		if (p_flip_h)
 		if (p_flip_h)
-			rect.position.x -= s.x - center.x;
+			rect.position.x -= cell_size.x / 2;
 		else
 		else
-			rect.position.x -= center.x;
+			rect.position.x += cell_size.x / 2;
 
 
 		if (p_flip_v)
 		if (p_flip_v)
-			rect.position.y -= s.y - center.y;
+			rect.position.y -= cell_size.y / 2;
 		else
 		else
-			rect.position.y -= center.y;
+			rect.position.y += cell_size.y / 2;
 	}
 	}
 
 
 	rect.position = p_xform.xform(rect.position);
 	rect.position = p_xform.xform(rect.position);

+ 13 - 11
scene/2d/tile_map.cpp

@@ -215,6 +215,9 @@ void TileMap::_fix_cell_transform(Transform2D &xform, const Cell &p_cell, const
 
 
 	if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT)
 	if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT)
 		offset.y += cell_size.y;
 		offset.y += cell_size.y;
+	else if (tile_origin == TILE_ORIGIN_CENTER) {
+		offset += cell_size / 2;
+	}
 
 
 	if (s.y > s.x) {
 	if (s.y > s.x) {
 		if ((p_cell.flip_h && (p_cell.flip_v || p_cell.transpose)) || (p_cell.flip_v && !p_cell.transpose))
 		if ((p_cell.flip_h && (p_cell.flip_v || p_cell.transpose)) || (p_cell.flip_v && !p_cell.transpose))
@@ -235,6 +238,8 @@ void TileMap::_fix_cell_transform(Transform2D &xform, const Cell &p_cell, const
 		xform.elements[1].x = -xform.elements[1].x;
 		xform.elements[1].x = -xform.elements[1].x;
 		if (tile_origin == TILE_ORIGIN_TOP_LEFT || tile_origin == TILE_ORIGIN_BOTTOM_LEFT)
 		if (tile_origin == TILE_ORIGIN_TOP_LEFT || tile_origin == TILE_ORIGIN_BOTTOM_LEFT)
 			offset.x = s.x - offset.x;
 			offset.x = s.x - offset.x;
+		else if (tile_origin == TILE_ORIGIN_CENTER)
+			offset.x = s.x - offset.x / 2;
 	}
 	}
 	if (p_cell.flip_v) {
 	if (p_cell.flip_v) {
 		xform.elements[0].y = -xform.elements[0].y;
 		xform.elements[0].y = -xform.elements[0].y;
@@ -242,10 +247,9 @@ void TileMap::_fix_cell_transform(Transform2D &xform, const Cell &p_cell, const
 		if (tile_origin == TILE_ORIGIN_TOP_LEFT)
 		if (tile_origin == TILE_ORIGIN_TOP_LEFT)
 			offset.y = s.y - offset.y;
 			offset.y = s.y - offset.y;
 		else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
 		else if (tile_origin == TILE_ORIGIN_BOTTOM_LEFT) {
-			if (p_cell.transpose)
-				offset.y += s.y;
-			else
-				offset.y -= s.y;
+			offset.y += s.y;
+		} else if (tile_origin == TILE_ORIGIN_CENTER) {
+			offset.y += s.y;
 		}
 		}
 	}
 	}
 	xform.elements[2].x += offset.x;
 	xform.elements[2].x += offset.x;
@@ -429,20 +433,18 @@ void TileMap::_update_dirty_quadrants() {
 				}
 				}
 
 
 			} else if (tile_origin == TILE_ORIGIN_CENTER) {
 			} else if (tile_origin == TILE_ORIGIN_CENTER) {
-				rect.position += tcenter;
 
 
-				Vector2 center = (s / 2) - tile_ofs;
-				center_ofs = tcenter - (s / 2);
+				rect.position += tile_ofs;
 
 
 				if (c.flip_h)
 				if (c.flip_h)
-					rect.position.x -= s.x - center.x;
+					rect.position.x -= cell_size.x / 2;
 				else
 				else
-					rect.position.x -= center.x;
+					rect.position.x += cell_size.x / 2;
 
 
 				if (c.flip_v)
 				if (c.flip_v)
-					rect.position.y -= s.y - center.y;
+					rect.position.y -= cell_size.y / 2;
 				else
 				else
-					rect.position.y -= center.y;
+					rect.position.y += cell_size.y / 2;
 			}
 			}
 
 
 			Ref<Texture> normal_map = tile_set->tile_get_normal_map(c.id);
 			Ref<Texture> normal_map = tile_set->tile_get_normal_map(c.id);