Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Fix anchor handling in sprite backend#243

Merged
aevyrie merged 5 commits into
aevyrie:mainfrom
nametable:main
Aug 23, 2023
Merged

Fix anchor handling in sprite backend#243
aevyrie merged 5 commits into
aevyrie:mainfrom
nametable:main

Conversation

@nametable

Copy link
Copy Markdown
Contributor

There is a problem in the sprite backend with handling of the configured anchor. There seems to be a discrepancy in the values of what the Anchor values mean and computing the center of the rect.

For instance Anchor::TopRight is equivalent to Anchor::Custom(Vec2::new(0.5, 0.5)). This means that the "center" is shifted down and left by 0.5 of the extent. Essentially the anchor value handles cutting the extent in half, thus, we need to not divide by 2.0 for the center calculation.

Sorry if I am explaining it badly, I have some example code that I wrote for testing, here:

//! Tests that sprite anchors are correctly handled.

use bevy::prelude::*;
use bevy_mod_picking::prelude::*;
use bevy::sprite::Anchor;

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins.set(low_latency_window_plugin()),
            DefaultPickingPlugins,
        ))
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    let mut anchor_index = 0;

    for anchor in [
        Anchor::TopLeft,
        Anchor::TopCenter,
        Anchor::TopRight,
        Anchor::CenterLeft,
        Anchor::Center,
        Anchor::CenterRight,
        Anchor::BottomLeft,
        Anchor::BottomCenter,
        Anchor::BottomRight,
        Anchor::Custom(Vec2::new(0.5, 0.5)),
    ] {
        // spawn black square behind sprite to show anchor point
        commands.spawn(SpriteBundle {
            sprite: Sprite {
                custom_size: Some(Vec2::new(128.0, 128.0)),
                color: Color::BLACK,
                ..default()
            },
            transform: Transform::from_xyz(
                (anchor_index % 3) as f32 * 256.0 - 256.0,
                (anchor_index / 3) as f32 * 256.0 - 256.0,
                -1.0,
            ),
            ..default()
        });

        commands.spawn(SpriteBundle {
            sprite: Sprite { 
                custom_size: Some(Vec2::new(128.0, 128.0)),
                color: Color::RED,
                anchor, ..default() },

            // 3x3 grid of anchor examples by changing tranform
            transform: Transform::from_xyz(
                (anchor_index % 3) as f32 * 256.0 - 256.0,
                (anchor_index / 3) as f32 * 256.0 - 256.0,
                0.0,
            ),
            // texture: asset_server.load("images/boovy.png"),
            ..default()
        });
        anchor_index += 1;
    }
}

@aevyrie

aevyrie commented Aug 23, 2023

Copy link
Copy Markdown
Owner

Thank you!

@aevyrie aevyrie merged commit dc631af into aevyrie:main Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants