Skip to content

More glam constants for accessing neighbors #21333

@ChristopherBiscardi

Description

@ChristopherBiscardi

What problem does this solve or what need does it fill?

Grids often have x,y positions. I'll use IVec2 here as an example.

When trying to access neighbor cells, a pattern I use is the IVec2 constants:

A "cross" set of neighbors then, is:

const cross: [IVec2;4] = [IVec2::X, IVec2::Y, IVec2::NEG_X, IVEC2::NEG_Y];

but you can imagine similar for diagonals and 3x3 neighbors including both cross and diagonals.

getting say, the entities from some grid that have valid positions (ie: aren't outside of a grid) can be done with a filter_map.

let valid_positions = cross.iter().filter_map(|offset| {
    grid.get(current_position + offset)
}.collect()

What solution would you like?

A collection of additional constants for these sets of neighbors (feel free to bikeshed names or suggest additional common patterns).

  • CROSS (or CARDINALS, or NSEW, etc)
  • DIAGONALS
  • ALL_NEIGHBORS (ie: not the current position in a 3x3 grid around the current tile)

AXES is an example of a combination that already exists.

What alternative(s) have you considered?

  • Just do it manually, which is what I've been doing. But this means every Grid API tends to recreate these access patterns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MathFundamental domain-agnostic mathematical operationsC-FeatureA new feature, making something new possibleD-StraightforwardSimple bug fixes and API improvements, docs, test and examplesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!X-UncontroversialThis work is generally agreed upon

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions