Skip to content

Commit

Permalink
Godot export: Fixed the positioning of tile physics shapes (#3862)
Browse files Browse the repository at this point in the history
The exporter was incorrectly using the collision object's X and Y 
position as a part of the tile's center. Fixed the calculation for both
rectangles and polygons.
  • Loading branch information
ryanpetrie committed Jan 30, 2024
1 parent a842ee2 commit b236017
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Added --project command-line parameter for use when exporting (#3797)
* Scripting: Added API for working with worlds (#3539)
* JSON format: Fixed tile order when loading a tileset using the old format
* Godot export: Fixed positioning of tile collision shapes (by Ryan Petrie, #3862)
* tmxrasterizer: Added --hide-object and --show-object arguments (by Lars Luz, #3819)
* tmxrasterizer: Added --frames and --frame-duration arguments to export animated maps as multiple images (#3868)
* tmxviewer: Added support for viewing JSON maps (#3866)
Expand Down
17 changes: 8 additions & 9 deletions src/plugins/tscn/tscnplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ static bool exportTileCollisions(QFileDevice *device, const Tile *tile,
if (const auto objectGroup = tile->objectGroup()) {
int polygonId = 0;

const auto centerX = tile->width() / 2;
const auto centerY = tile->height() / 2;

for (const auto object : objectGroup->objects()) {
const auto shape = object->shape();

Expand All @@ -435,9 +438,6 @@ static bool exportTileCollisions(QFileDevice *device, const Tile *tile,

foundCollisions = true;

const auto centerX = tile->width() / 2 - object->x();
const auto centerY = tile->height() / 2 - object->y();

device->write(formatByteString(
"%1/physics_layer_0/polygon_%2/points = PackedVector2Array(",
tileName, polygonId));
Expand All @@ -446,8 +446,8 @@ static bool exportTileCollisions(QFileDevice *device, const Tile *tile,
case MapObject::Rectangle: {
auto x1 = object->x() - centerX;
auto y1 = object->y() - centerY;
auto x2 = object->width() - centerX;
auto y2 = object->height() - centerY;
auto x2 = object->x() + object->width() - centerX;
auto y2 = object->y() + object->height() - centerY;

flipState(x1, y1, flippedState);
flipState(x2, y2, flippedState);
Expand All @@ -458,13 +458,12 @@ static bool exportTileCollisions(QFileDevice *device, const Tile *tile,
break;
}
case MapObject::Polygon: {
auto polygon = object->polygon().toPolygon();
bool first = true;
for (auto point : polygon) {
for (auto point : object->polygon()) {
if (!first)
device->write(", ");
auto x = point.x() - centerX;
auto y = point.y() - centerY;
auto x = object->x() + point.x() - centerX;
auto y = object->y() + point.y() - centerY;
flipState(x, y, flippedState);
device->write(formatByteString("%1, %2", x, y));
first = false;
Expand Down

0 comments on commit b236017

Please sign in to comment.