Repository navigation
Draw HexGrid agents at the centre of their hexagon - #3952
Merged
Merged
Conversation
Since mesa#3268, HexGrid stores the pixel centre of each hexagon in cell.position. The drawer already uses it for the hexagons, but SpaceRenderer._map_coordinates still treated the agent positions as grid coordinates and converted them again. Agents ended up shifted away from their cell, and some were drawn outside the plot. Use the positions as they are, like the other space types. Replace the old test, which only checked that the positions changed, with one that checks each agent is drawn on its own hexagon for both backends. Fixes mesa#3949
|
Performance benchmarks:
|
This was referenced Oct 20, 2026
|
Related work: #4043 now covers the legacy rendering issue noted in the description, and #3959 addresses the HexGrid row layout. Using Automated triage note, written by an AI model for the Mesa maintainers. It can be wrong, and it doesn't make decisions: maintainers do. If it missed the point, say so here. |
Member
|
Could you add a before and after screenshot? Otherwise this looks fine. |
Contributor
Author
quaquel
approved these changes
Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Pre-PR Checklist
Summary
With
SpaceRenderer, agents on aHexGridare not drawn inside their own hexagon. They get pushed up and to the right, more so the further they are from the origin, and some end up outside the plot. This PR puts every agent back at the centre of its hexagon.Bug / Issue
Fixes #3949
Since #3268,
HexGridstores the real (pixel) centre of each hexagon incell.position. The hexagons are drawn from that, and agent positions come from it too. But_map_coordinatesstill thought it was getting grid coordinates, so it converted them a second time:flowchart LR A["cell (2, 1)"] --> B["cell.position<br/>already the hexagon centre"] B --> C["_map_coordinates<br/>applies hex spacing again"] C --> D[agent drawn in the wrong place] B --> E[hexagon drawn here]Implementation
HexGridnow goes through the same branch as the other spaces in_map_coordinates, so its positions are used as they are. The old hex conversion is removed.For example, an agent in cell
(0, 1):cell.position)Testing
test_map_coordinatesonly asserted that the output was different from the input, which is how this slipped through. It now expects the positions to be unchanged.test_hexgrid_agents_drawn_at_hexagon_centrefor both backends. It places agents on even and odd rows and checks each one lands on the centre of the hexagon the drawer draws for its cell. It fails onmainand passes with this change.pytest tests/ -Werror(877 passed), andpre-commitpasses.Additional Notes
The legacy
draw_spacepath inmpl_space_drawing.pyhas the same double conversion, and its hexagon mesh also shifts even rows whileHexGridshifts odd rows. Fixing that means changing how the legacy mesh is drawn, so I kept it out of this PR to keep it small. Happy to open a follow-up if that's useful.