Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion crates/bevy_sprite_render/src/tilemap_chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bevy_ecs::{
world::DeferredWorld,
};
use bevy_image::Image;
use bevy_math::{primitives::Rectangle, UVec2};
use bevy_math::{primitives::Rectangle, IVec2, UVec2, Vec2};
use bevy_mesh::{Mesh, Mesh2d};
use bevy_platform::collections::HashMap;
use bevy_reflect::{prelude::*, Reflect};
Expand Down Expand Up @@ -83,6 +83,29 @@ impl TilemapChunk {
0.,
)
}

pub fn calculate_tile_pos(&self, t: Vec2) -> IVec2 {
IVec2::new(
(
// display position
t.x
Comment on lines +90 to +91
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why these two lines are indented?

Suggested change
// display position
t.x
// display position
t.x

// divided by display size for a tile
/ self.tile_display_size.x as f32
// minus 3/2 to reverse the center correction
- 3. / 2.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where did 3/2 come from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has to be done to prevent an off-by-one error

// plus 1/2 the tilechunk size
+ self.chunk_size.x as f32 / 2.
)
.floor() as i32,
// display position
(t.y
// divided by display size for a tile, negated to reverse the y-flip
/ -(self.tile_display_size.y as f32)
// plus 1/2 the tilechunk size
+ self.chunk_size.y as f32 / 2.)
.floor() as i32,
)
}
}

/// Data for a single tile in the tilemap chunk.
Expand Down