Skip to content

How to get the actual pixel size of a transformed sprite? #8919

Closed Answered by awhillas
awhillas asked this question in Q&A
Discussion options

You must be logged in to vote

from but the gist is you can pull the scaling factor from the transform.

fn print_sprite_bounding_boxes(
    mut sprite_query: Query<(&Transform, &Handle<Image>), With<Sprite>>,
    assets: Res<Assets<Image>>,
) {
    for (transform, image_handle) in sprite_query.iter_mut() {
        let image_dimensions = assets.get(image_handle).unwrap().size();
        let scaled_image_dimension = image_dimensions * transform.scale.truncate();
        let bounding_box = Rect::from_center_size(transform.translation.truncate(), scaled_image_dimension);

        println!("bounding_box: {:?}", bounding_box);
    }
}

I guess Reddit wins between Stack Overflow and here.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by NiklasEi
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants