Skip to content

world_to_grid does not convert negative positions to the grid cell correctly #30

@blubbers122

Description

@blubbers122

Love the tutorial!

One thing I noticed:
current implementation of Grid.world_to_grid doesn't round down towards negative infinity

example: if world_pos is (-15, -15), (-15, 15), (15, -15), or (15, 15), we get a grid cell of (0,0) incorrectly for 3 of these

current

static func world_to_grid(world_pos: Vector2i) -> Vector2i:
	var grid_pos: Vector2i = world_pos / tile_size
	return grid_pos

correction

static func world_to_grid(world_pos: Vector2i) -> Vector2i:
	var grid_pos: Vector2i = (Vector2(world_pos) / Vector2(tile_size)).floor()
	return grid_pos

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions