bevy_svg icon indicating copy to clipboard operation
bevy_svg copied to clipboard

Centred rotation rotating around the TopLeft

Open samuelwatsonofficial opened this issue 1 year ago • 4 comments

So my previous issue with the svg flicking from centred to topleft are gone but the svg acts as though it's origin is the top left rather than the centre. image image image if you want to check out the whole project where this is an issue look at https://github.com/samuelwatsonofficial/Scarlet/tree/main however the code concerned here is in character.rs:

#just for reference this code will not compile on it's own

let svg = asset_server.load("shapes/761.svg");
    commands
        .spawn(
            (
                RigidBody::Dynamic,
                GravityScale(5.),
                Collider::ball(10.),
                Velocity 
                {
                    linvel: Vec2::new(2.,2.),
                    angvel: 0.0,
                },
                Svg2dBundle
                {
                    svg,
                    origin: Origin::Center, #the rotation should be relative to the centre not the top left
                    transform: Transform
                    {
                        scale: Vec3::new(4.0, 4.0, 1.0),
                        translation: Vec3::new(0.0,3.0,0.0),
                        ..Default::default()
                    },
                    ..Default::default()
                },
                Character,
            ));

I'm fairly new to gamedev in general and very new to bevy so it's possible I've made an oversight but I've checked rapier and there should be no issues with centre of rotation on that end. I hope it's not too much effort to fix this, so far this plugin has been fantastic thanks for making it!

samuelwatsonofficial avatar Oct 06 '23 15:10 samuelwatsonofficial

Hm... ok, that should not happen. The code snippet looks fine so far. I'll hopefully find some time on the weekend or next week. I'll take a look then. 🙂 How did you resolve the issue with the flickering?

Weasy666 avatar Oct 09 '23 06:10 Weasy666

Fantastic, there's no rush because my project isn't even close to finished yet but that would be amazing of you. As for the flickering it just seemed to disappear as soon as I made a character controller, I'll see if I can test the old versions to find when it stopped.

samuelwatsonofficial avatar Oct 09 '23 06:10 samuelwatsonofficial

I just did a check on the old code I made and the flickering bug only occurs when there's nothing modifying the transform of the rapier rigidbody the SVG is attached to, in my case adding in a gravity scale was enough to completely fix this issue. It could be something to do with rapier2d knowing at compile time that nothing with modify the transform so there are optimisations that are causing unexpected behavior, but that's just a guess. Here are the two sample pieces of code to compare.

#No flickering

pub fn spawn_character(mut commands: Commands, asset_server: Res<AssetServer>)
{
    println!("player exists now!");
    let svg = asset_server.load("shapes/761.svg");
    commands
        .spawn(
            (
                RigidBody::Dynamic,
                GravityScale(5.00),
                Collider::ball(10.),
                Svg2dBundle
                {
                    svg,
                    origin: Origin::Center,
                    transform: Transform
                    {
                        scale: Vec3::new(4.0, 4.0, 1.0),
                        translation: Vec3::new(0.0,3.0,0.0),
                        ..Default::default()
                    },
                    ..Default::default()
                }
            ));

}

#Flickering (notice the lack of a gravityscale)

pub fn spawn_character(mut commands: Commands, asset_server: Res<AssetServer>)
{
    println!("player exists now!");
    let svg = asset_server.load("shapes/761.svg");
    commands
        .spawn(
            (
                RigidBody::Dynamic,
                Collider::ball(10.),
                Svg2dBundle
                {
                    svg,
                    origin: Origin::Center,
                    transform: Transform
                    {
                        scale: Vec3::new(4.0, 4.0, 1.0),
                        translation: Vec3::new(0.0,3.0,0.0),
                        ..Default::default()
                    },
                    ..Default::default()
                }
            ));

}

samuelwatsonofficial avatar Oct 10 '23 15:10 samuelwatsonofficial

Hm... i have no experience with rapier. Does rapier modify the transform of the entity it is attached to?

Weasy666 avatar Nov 05 '23 15:11 Weasy666

This should hopefully be fixed by #41, so i'll close this issue. If the problem still appears, please let me know.

Weasy666 avatar Jul 28 '24 17:07 Weasy666