Skip to content

fix(tilemap): anchor tile objects at their bottom-left corner - #2

Merged
instinkt900 merged 1 commit into
mainfrom
fix/tile-object-anchor
Sep 20, 2026
Merged

instinkt900 merged 1 commit into
mainfrom
fix/tile-object-anchor

Conversation

@instinkt900

Copy link
Copy Markdown
Owner

Problem

Tiled anchors a tile object (an object with a gid) at its bottom-left corner. A plain
rectangle or ellipse object is anchored top-left. drawObjectLayer passed the object's position
straight through as the top-left:

drawTileImage(*tileset, resolvedId, object.tile, object.position + offset, object.rotation, scale);

So every tile object drew one full height below where the editor showed it. The error scales with
the object, so small decoration looks roughly right while anything large is plainly misplaced.

Seen in TinCan: a decoration object placed at y=5712 with a drawn height of 192 should render from
y=5520, and rendered from y=5712 — 192px low, sitting well below the ground tiles it was aligned to.

Fix

Shift the draw position up by the drawn height:

FloatVec2 topLeft = object.position + offset;
topLeft.y -= static_cast<float>(sourceRect.h()) * scale.y;

Using sourceRect.h() * scale.y rather than the tile's natural height means a resized tile object
moves by the height the editor actually shows.

Tests

Renderer: draws tile objects from object layers, skipping shapes asserted the old position, so it
was pinning the bug in place — it now expects y=-8 for a 16px tile placed at y=8.

Added Renderer: a resized tile object is anchored at its scaled bottom-left to cover the scaled
case, which needed a scale field on the mock's DrawCall (the mock recorded position, pivot and
rotation but not scale).

Not covered here

Tiled also rotates a tile object about that same bottom-left anchor, while drawTileImage pivots
about the tile centre (tile_map_renderer.cpp:72-75). A rotated tile object is still misplaced
after this change. That sits in the flip-flag composition and is left for its own change.

Downstream

Any map that was authored against the old behaviour — by nudging tile objects up to compensate —
will need those objects moved back.

Tiled anchors a tile object at its bottom-left corner, where a plain
rectangle object is anchored top-left. The renderer passed the object
position straight through as the top-left, so every tile object drew a
full height below where the editor showed it, and the taller the
object the more obvious the gap.

Shift the draw position up by the drawn height, which follows the
object's size rather than the tile's natural height so resized objects
move by what the editor shows.
@coderabbitai

coderabbitai Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 000182a0-5cc5-44df-bf85-c900e4ffc919


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@instinkt900
instinkt900 merged commit 03a23ff into main Sep 20, 2026
11 checks passed
@instinkt900
instinkt900 deleted the fix/tile-object-anchor branch September 20, 2026 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant