Skip to content

Commit 7e9f632

Browse files
authored
fix: incorrect sprite size for aabb when sprite has rect and no custom_size (#12738)
# Objective Fixes #12736 ## Solution Use sprite rect to calculate sprite size for aabb when custom_size is None
1 parent 221d925 commit 7e9f632

File tree

1 file changed

+61
-9
lines changed

1 file changed

+61
-9
lines changed

crates/bevy_sprite/src/lib.rs

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,18 @@ pub fn calculate_bounds_2d(
194194
}
195195
}
196196
for (entity, sprite, texture_handle, atlas) in &sprites_to_recalculate_aabb {
197-
if let Some(size) = sprite.custom_size.or_else(|| match atlas {
198-
// We default to the texture size for regular sprites
199-
None => images.get(texture_handle).map(|image| image.size_f32()),
200-
// We default to the drawn rect for atlas sprites
201-
Some(atlas) => atlas
202-
.texture_rect(&atlases)
203-
.map(|rect| rect.size().as_vec2()),
204-
}) {
197+
if let Some(size) = sprite
198+
.custom_size
199+
.or_else(|| sprite.rect.map(|rect| rect.size()))
200+
.or_else(|| match atlas {
201+
// We default to the texture size for regular sprites
202+
None => images.get(texture_handle).map(|image| image.size_f32()),
203+
// We default to the drawn rect for atlas sprites
204+
Some(atlas) => atlas
205+
.texture_rect(&atlases)
206+
.map(|rect| rect.size().as_vec2()),
207+
})
208+
{
205209
let aabb = Aabb {
206210
center: (-sprite.anchor.as_vec() * size).extend(0.0).into(),
207211
half_extents: (0.5 * size).extend(0.0).into(),
@@ -226,7 +230,7 @@ impl ExtractComponent for SpriteSource {
226230
#[cfg(test)]
227231
mod test {
228232

229-
use bevy_math::Vec2;
233+
use bevy_math::{Rect, Vec2, Vec3A};
230234
use bevy_utils::default;
231235

232236
use super::*;
@@ -336,4 +340,52 @@ mod test {
336340
// Check that the AABBs are not equal
337341
assert_ne!(first_aabb, second_aabb);
338342
}
343+
344+
#[test]
345+
fn calculate_bounds_2d_correct_aabb_for_sprite_with_custom_rect() {
346+
// Setup app
347+
let mut app = App::new();
348+
349+
// Add resources and get handle to image
350+
let mut image_assets = Assets::<Image>::default();
351+
let image_handle = image_assets.add(Image::default());
352+
app.insert_resource(image_assets);
353+
let mesh_assets = Assets::<Mesh>::default();
354+
app.insert_resource(mesh_assets);
355+
let texture_atlas_assets = Assets::<TextureAtlasLayout>::default();
356+
app.insert_resource(texture_atlas_assets);
357+
358+
// Add system
359+
app.add_systems(Update, calculate_bounds_2d);
360+
361+
// Add entities
362+
let entity = app
363+
.world_mut()
364+
.spawn((
365+
Sprite {
366+
rect: Some(Rect::new(0., 0., 0.5, 1.)),
367+
anchor: Anchor::TopRight,
368+
..default()
369+
},
370+
image_handle,
371+
))
372+
.id();
373+
374+
// Create AABB
375+
app.update();
376+
377+
// Get the AABB
378+
let aabb = *app
379+
.world_mut()
380+
.get_entity(entity)
381+
.expect("Could not find entity")
382+
.get::<Aabb>()
383+
.expect("Could not find AABB");
384+
385+
// Verify that the AABB is at the expected position
386+
assert_eq!(aabb.center, Vec3A::new(-0.25, -0.5, 0.));
387+
388+
// Verify that the AABB has the expected size
389+
assert_eq!(aabb.half_extents, Vec3A::new(0.25, 0.5, 0.));
390+
}
339391
}

0 commit comments

Comments
 (0)