@@ -194,14 +194,18 @@ pub fn calculate_bounds_2d(
194
194
}
195
195
}
196
196
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
+ {
205
209
let aabb = Aabb {
206
210
center : ( -sprite. anchor . as_vec ( ) * size) . extend ( 0.0 ) . into ( ) ,
207
211
half_extents : ( 0.5 * size) . extend ( 0.0 ) . into ( ) ,
@@ -226,7 +230,7 @@ impl ExtractComponent for SpriteSource {
226
230
#[ cfg( test) ]
227
231
mod test {
228
232
229
- use bevy_math:: Vec2 ;
233
+ use bevy_math:: { Rect , Vec2 , Vec3A } ;
230
234
use bevy_utils:: default;
231
235
232
236
use super :: * ;
@@ -336,4 +340,52 @@ mod test {
336
340
// Check that the AABBs are not equal
337
341
assert_ne ! ( first_aabb, second_aabb) ;
338
342
}
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
+ }
339
391
}
0 commit comments